Created
September 28, 2012 17:43
-
-
Save EBNull/3801211 to your computer and use it in GitHub Desktop.
Django reverse M2M field (for admin, etc)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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