Django/South: What is the proper way to set related_name arguments for the intermediate model on a ManyToMany to self relation?

≯℡__Kan透↙ 提交于 2019-12-24 10:56:42

问题


This is related to a question I asked yesterday about migrating a change from a ForeignKey to self to a ManyToManyField to self, however because I'm only prototyping an app for the time being/due to time constraints, I've decided to just drop the relevant tables and reset the migration history.

These are the relevant models/fields:

class Person(models.Model):
    nominator = models.ManyToManyField('self', symmetrical=False,                
            verbose_name=_('nominator'), through='Nomination', null=True,        
            blank=True)


class Nomination(models.Model):                                                  
    nominee = models.ForeignKey(Person)                                          
    nominator = models.ForeignKey(Person)

However, this doesn't even generate an initial migration:

$ ./manage.py schemamigration nominations --initial
CommandError: One or more models did not validate:
nominations.nomination: Accessor for field 'nominee' clashes with related field 'Person.nomination_set'. Add a related_name argument to the definition for 'nominee'.
nominations.nomination: Accessor for field 'nominator' clashes with related field 'Person.nomination_set'. Add a related_name argument to the definition for 'nominator'.

I followed the instructions to add a related_name argument to the nominator and nominee fields on the Nomination model, like so:

class Nomination(models.Model):                                                  
    nominee = models.ForeignKey(Person, related_name=_('nominator'))             
    nominator = models.ForeignKey(Person, related_name=_('nominee'))  

That gave me a different error:

$ ./manage.py schemamigration nominations --initial
CommandError: One or more models did not validate:
nominations.nomination: Accessor for field 'nominee' clashes with m2m field 'Person.nominator'. Add a related_name argument to the definition for 'nominee'.
nominations.nomination: Reverse query name for field 'nominee' clashes with m2m field 'Person.nominator'. Add a related_name argument to the definition for 'nominee'.

I'm not sure what to do from this point. I get the feeling that I've forgotten something the Person model, but I'm not sure what that could be since both the Django/South docs are not terribly forthcoming when it comes to this kind of relation.


回答1:


Just use different names other than the fields names. Something like person_nominator and person_nominee.

I move this comment to an answer so that the question is no longer without an answer.



来源:https://stackoverflow.com/questions/19568239/django-south-what-is-the-proper-way-to-set-related-name-arguments-for-the-inter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!