I have a model defined in Django. I would like to add to this model a method that returns me part of a mounted query (a QuerySet ).
That is, a method I want to define a method that affects the results of the query when calling Model.objects
. My intention in doing this is to improve reuse, avoiding the constant repetition of complex queries.
Example:
class Empresas(models.Model):
nome_fantasia = models.CharField(max_length=455, blank=True, null=True)
uf = models.CharField(max_length=2, blank=True, null=True)
telefone = models.CharField(max_length=15, blank=True, null=True)
email = models.CharField(max_length=455, blank=True, null=True)
bairro = models.CharField(max_length=255, blank=True, null=True)
logradouro = models.CharField(max_length=455, blank=True, null=True)
numero = models.CharField(max_length=45, blank=True, null=True)
cep = models.CharField(max_length=10, blank=True, null=True)
municipio = models.CharField(max_length=255, blank=True, null=True)
razao_social = models.CharField(max_length=455, blank=True, null=True)
cnpj = models.CharField(unique=True, max_length=19)
tipo = models.CharField(max_length=100, blank=True, null=True)
latitude = models.FloatField(blank=True, null=True)
longitude = models.FloatField(blank=True, null=True)
def metodo_especial_com_uma_query_padrao_especifica(self):
pass
How to do this in Django?