Skip to content

Instantly share code, notes, and snippets.

@ychennay
Created March 21, 2020 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ychennay/1740a20f4094e7c0d44ee228fa4846b0 to your computer and use it in GitHub Desktop.
Save ychennay/1740a20f4094e7c0d44ee228fa4846b0 to your computer and use it in GitHub Desktop.
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.set_attributes_from_name(name)
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