Created
January 18, 2016 16:47
-
-
Save fandrefh/5bb9b114e6a96917d22a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Classe que herda de Manager e customiza o comportamento de objects adicionando um método de busca | |
class RestauranteProdutoManager(models.Manager): | |
def buscar(self, query): | |
return self.get_queryset().filter(models.Q(restaurante__icontains=query) | models.Q(produto__produto__icontains=query)) | |
class Restaurante(models.Model): | |
restaurante = models.CharField(u'Restaurante', max_length=255) | |
endereco = models.CharField(u'Endereço', max_length=255) | |
numero = models.CharField(u'Número', max_length=20) | |
cep = models.CharField(u'CEP', max_length=10, help_text='Formato: 00.000-000', blank=True, null=True) | |
bairro = models.CharField(u'Bairro', max_length=50) | |
cidade = models.CharField(u'Cidade', max_length=50) | |
estado = models.CharField(u'Estado', max_length=2, choices=STATE_CHOICES) | |
gerente = models.CharField(u'Responsável/Gerente', max_length=255, blank=True, null=True) | |
telefone = models.CharField(u'Telefone', max_length=14, help_text='(00)00000-0000') | |
produto = models.ManyToManyField('Produto', null=True, related_name='cadarpio') | |
funcionamento = models.ManyToManyField('Funcionamento', null=True, related_name='restaurante') | |
horario_inicio = models.TimeField(u'Abre') | |
horario_fim = models.TimeField(u'Fecha') | |
dt_cadastro = models.DateTimeField(auto_now_add=True) | |
#Aqui é o manager customizado | |
objects = RestauranteProdutoManager() | |
def __str__(self): | |
return self.restaurante |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment