Skip to content

Instantly share code, notes, and snippets.

@maxwellamaral
Forked from maxwellcc/models.py
Created September 6, 2022 12:38
Show Gist options
  • Save maxwellamaral/8e28b2a0fbe29e2146196fa048317631 to your computer and use it in GitHub Desktop.
Save maxwellamaral/8e28b2a0fbe29e2146196fa048317631 to your computer and use it in GitHub Desktop.
Como criar um modelo Django que herde de classe pai como proxy, com restrições na consulta
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
age = models.PositiveIntegerField()
def __str__(self):
return self.first_name
class Majority(models.Manager):
def get_queryset(self):
return super(Majority, self).get_queryset().filter(age__gte=18)
class MajorityPerson(Person):
class Meta:
proxy = True
objects = Majority()
def __str__(self):
return f'{self.first_name} {self.last_name}'
def clean(self):
if self.age < 18:
raise ValidationError('It cannot be under 18 years.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment