46

I am working on an extending a current project and came cross this line of code:

MyGenericRelation().contribute_to_class(model, 'field_name')

The result of this code is a field with 'field_name' added to the 'model' and from what I gather, the objects inside the field are a list of type X (part of MyGenericRelation).

I wanted to understand how this contribute_to_class method works. Why do I get a list of X objects attached to 'field_name' and does it have to be generic relations?

Manu mathew
  • 859
  • 8
  • 25
  • 1
    Can you explain what you're trying to do and why you think you need to modify this code? What are you not able to achieve with normal generic relations? – Daniel Roseman Mar 01 '10 at 19:39

1 Answers1

56

Basically, for objects that have a contribute_to_class method, when adding them to a Model class, we call contribute_to_class instead of setattr: this allows objects to do additional book-keeping and/or modification of the class as they are added. Alex Gaynor has a good explanation here: http://lazypython.blogspot.com/2008/11/django-models-digging-little-deeper.html

viam0Zah
  • 25,949
  • 8
  • 77
  • 100
zellyn
  • 1,393
  • 1
  • 11
  • 16