When I want to define hints for the @property on a model class, the model is not yet defined so Python (tested with 3.8) complains:
NameError: name 'Person' is not defined
|
from django_virtual_models import hints |
|
|
|
class Person(models.Model): |
|
name = models.CharField(max_length=255) |
|
|
|
@property # <--- property must come before |
|
@hints.no_deferred_fields() |
|
def name_title_case(self): |
|
return self.name.title() |
|
|
|
# method w/o params, DRF supports that too: |
|
def has_won_any_award(self: Annotated[Person, hints.Virtual("awards")]): |
|
return len(person.awards) > 0 |
|
|
|
class PersonSerializer(v.VirtualModelSerializer): |
|
class Meta: |
|
model = Person |
|
virtual_model = VirtualPerson |
|
fields = ["name_title_case", "has_won_any_award"] # <--- this works |
|
``` |
When I want to define hints for the
@propertyon a model class, the model is not yet defined so Python (tested with 3.8) complains:django-virtual-models/docs/tutorial.md
Lines 571 to 590 in 2eda0c0