Created
July 25, 2023 19:18
-
-
Save rg3915/ddb158458f52d0d12ac7fd66bb62ea3c to your computer and use it in GitHub Desktop.
Django Manager QuerySet ativo
This file contains 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
from django.db.models import TextChoices | |
from django.utils.translation import gettext | |
class TipoPessoaChoice(TextChoices): | |
F = "F", gettext("Pessoa Fisica") | |
J = "J", gettext("Pessoa Juridica") |
This file contains 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
from django.db.models import QuerySet, Manager | |
from meuapp.choices import TipoPessoaChoice | |
class InvalidArgumentTipoPessoaError(Exception): | |
""" | |
Raise when the argument provided is not an | |
option of TipoPessoaChoice | |
""" | |
pass | |
class ClienteQuerySet(QuerySet): | |
def ativo(self): | |
return self.filter(ativo=True) | |
def inativo(self): | |
return self.filter(ativo=False) | |
def filtrar_por_tipo_de_pessoa(self, tipo_de_pessoa): | |
if not tipo_de_pessoa or tipo_de_pessoa not in TipoPessoaChoice.values: | |
raise InvalidArgumentTipoPessoaError() | |
return self.filter(tipo_de_pessoa=tipo_de_pessoa) | |
def filtrar_por_pessoa_fisica_ativas(self): | |
qs = self.ativo().filtrar_por_tipo_de_pessoa( | |
tipo_de_pessoa=TipoPessoaChoice.F | |
) | |
return qs | |
class ClienteManager(Manager.from_queryset(ClienteQuerySet)): | |
# aqui só vai funções que não fazem sentido estar num queryset | |
# como create e coisas que não precisam ter dados previamente | |
# filtrados | |
pass |
This file contains 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
class ClienteManager(models.Manager.from_queryset(ClienteQuerySet)): | |
# aqui só vai funções que não fazem sentido estar num queryset | |
# como create e coisas que não precisam ter dados previamente | |
# filtrados | |
def get_queryset(self): | |
# isso forçará um filtro especifico | |
# em toda e qualquer query feita apartir | |
# deste manager. | |
qs = super().get_queryset() | |
return qs.filtrar_por_pessoa_fisica_ativas() |
This file contains 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
from django.db import models | |
from meuapp.choices import TipoPessoaChoice | |
from meuapp.managers import ClienteManager | |
class Cliente(models.Model): | |
ativo = models.BooleanField(default=True, null=False, blank=False) | |
tipo_pessoa = models.CharField(choices=TipoPessoaChoice.choices, null=False, blank=False) | |
objects = ClienteManager() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment