How to add methods to a Queryset?

Asked

Viewed 102 times

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?

1 answer

1


You can extend the Model Manager and add a method of Queryset customized. See 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 that you can run darlings in this way:

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 documentation of Django.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.