Foreign keys add magic members

Because when you add one field to a class, you clearly want to add two fields to a class, and there's no need to consider the field you never asked to add to be something other than a field.
This commit is contained in:
Cy 2020-05-30 06:55:04 +00:00 committed by Cy
parent 0b93eb78f4
commit 0439440f40
No known key found for this signature in database
GPG Key ID: F66D599380F88521
1 changed files with 6 additions and 4 deletions

View File

@ -29,14 +29,16 @@ class Theme(models.Model):
def __str__(self):
return self.name
from django.db.models.fields.related_descriptors import ForeignKeyDeferredAttribute
def set_fields(klass):
fields = {}
fields = []
for n in dir(klass):
assert n != "_fields"
v = getattr(klass, n)
if isinstance(v, models.Field):
fields.add(n)
if not hasattr(v, 'field'): continue
if not isinstance(v.field, models.Field): continue
if isinstance(v, ForeignKeyDeferredAttribute): continue
fields.append(n)
setattr(klass, '_fields', fields)
return klass