-
-
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
This file contains hidden or 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 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