From 0439440f4040f099781a359c9ac49314ff2e3cb2 Mon Sep 17 00:00:00 2001 From: Cy Date: Sat, 30 May 2020 06:55:04 +0000 Subject: [PATCH] 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. --- brutaldon/models.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/brutaldon/models.py b/brutaldon/models.py index a0c3d7a..33847a1 100644 --- a/brutaldon/models.py +++ b/brutaldon/models.py @@ -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