How to avoid django "clashes with related m2m field" error?
I have a lot of models with voting functionality, so I created a structure like this:
class Voteable(models.Model):
likes_balance = models.IntegerField(default=0, editable=False)
votes = models.ManyToManyField(User, blank=True, editable=False)
likes = models.ManyToManyField(User, blank=True, editable=False)
class Meta:
abstract = True
class Item(Voteable):
title = models.CharField(max_length=20, db_index=True)
description = models.TextField(max_length=1000)
contact_user = models.ForeignKey(User, null=True, blank=True)
class Meta:
abstract = True
class Movie(Item):
cover = models.ImageField(upload_to='images/covers/')
class Car(Item):
seller = models.CharField(max_length=50)
When I try to create tables with "python manage.py syncdb" I开发者_Go百科 get error message:
Accessor for m2m field 'likes' clashes with related field 'User.movie_set'. Add a related_name argument to the definition for 'likes'.
Of cause I have much more fields in Item class, so don't want to copy all of them to all subclasses and just set related_name like suggested in error.
Any suggestions how to deal with it?
I found a solution in Django documention.
It's possible to write in abstract models things like this:related_name="%(app_label)s_%(class)s_related"
Normally if you add a related_name as suggested in your M2M definition, it should work :
class Voteable(models.Model):
likes_balance = models.IntegerField(default=0, editable=False)
votes = models.ManyToManyField(User, blank=True, editable=False, related_name='votes')
likes = models.ManyToManyField(User, blank=True, editable=False, related_name='likes')
class Meta:
abstract = True
It's because by not doing so, Django will add two user_id in the Voteable table, resulting in a clash because there is twice the same column name. Adding a related_name force Django to use the given related_name instead of the {Foreign Table Name}_id column name.
Hope this helps.
精彩评论