generics.rst 721 B

123456789101112131415161718
  1. Generic Relations
  2. =================
  3. Generic relations' fields are be frozen, but unfortunately
  4. not the GenericForeignKey itself (see :ref:`orm-freezing` for a reason why).
  5. To add it back onto a model, add the import
  6. for generic at the top of the migration and then in the body of forwards() put::
  7. gfk = generic.GenericForeignKey()
  8. gfk.contribute_to_class(orm.FooModel, "object_link")
  9. This will add the GenericForeignKey onto the model as model.object_link.
  10. You can pass the optional content_type and id field names into the
  11. constructor as usual.
  12. Also, be careful when using ContentType; make sure to use the frozen
  13. orm['contenttypes.ContentType'], and don't import it directly,
  14. otherwise comparisons may fail.