How to add methods to a QuerySet?

1

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?

    
asked by anonymous 09.02.2018 / 13:15

1 answer

1

You can extend Model Manager and add a custom QuerySet method. Here is an example below where I added a filter for companies located in the Southeast and for SME companies:

class EmpresaQuerySet(models.QuerySet):
    def sudeste(self):
        return self.filter(uf__in=['ES', 'MG', 'SP', 'RJ'])

    def pme(self):
        '''Pequena e média empresa'''
        return self.filter(tipo='PME')

class Empresa(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)

    objects = EmpresaQuerySet.as_manager()

With this you can execute queries like this:

empresas = Empresa.objects.sudeste().order_by('nome_fantasia')

Or merge the filters:

empresas = Empresa.objects.pme().sudeste().filter(nome_fantasia__startswith='G')

More details on official Django documentation .

    
10.02.2018 / 16:40