Skip to content

Instantly share code, notes, and snippets.

@EBNull
Created September 28, 2012 17:43
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 EBNull/3801211 to your computer and use it in GitHub Desktop.
Save EBNull/3801211 to your computer and use it in GitHub Desktop.
Django reverse M2M field (for admin, etc)
#This class lets you add the 'other side' of a m2m to a django model.
#Why this is better than just using related_name:
# -Admin sees it as a real field, gives you the filter_horizontal widget if you ask
#
#Issues:
# -Probably breaks syncdb (table tries to be created twice)
#
#Non-issues:
# -Doesn't break south (custom introspection rule makes this field ignored)
#Initially found at
#https://code.djangoproject.com/ticket/897#comment:28
class ReverseManyToManyField(models.ManyToManyField):
pass
try:
import south
except ImportError:
pass
else:
from south.modelsinspector import add_ignored_fields
add_ignored_fields([".*\.ReverseManyToManyField$",])
##Example Use
#class Test1(models.Model):
# tests2 = models.ManyToManyField('Test2', blank=True)
#
#class Test2(models.Model):
# tests1 = models.ReverseManyToManyField(Test1, through=Test1.tests2.through, blank=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment