Skip to content

Instantly share code, notes, and snippets.

Created March 21, 2020 18:12
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
def contribute_to_class(self, cls, name, private_only=False):
Register the field with the model class it belongs to.
If private_only is True, create a separate instance of this field
for every subclass of cls, even if cls is not an abstract model.
self.model = cls
cls._meta.add_field(self, private=private_only)
if self.column:
# Don't override classmethods with the descriptor. This means that
# if you have a classmethod and a field with the same name, then
# such fields can't be deferred (we don't have a check for this).
print(f"getattr(cls={cls}, self={self}), column={self.column}")
#print(f"cls.__dict__={cls.__dict__}, {self.attname}")
if not getattr(cls, self.attname, None): # <-- __get__ called invoked when self.attname is used!
setattr(cls, self.attname, self.descriptor_class(self))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment