标签云

微信群

扫码加入我们

WeChat QR Code

I want to create an object that contains 2 links to Users. For example:class GameClaim(models.Model):target = models.ForeignKey(User)claimer = models.ForeignKey(User)isAccepted = models.BooleanField()but I am getting the following errors when running the server:Accessor for field 'target' clashes with related field 'User.gameclaim_set'. Add a related_name argument to the definition for 'target'.Accessor for field 'claimer' clashes with related field 'User.gameclaim_set'. Add a related_name argument to the definition for 'claimer'.Can you please explain why I am getting the errors and how to fix them?


These error messages are really good. They already explain how to fix them. And reading up on **[related_name in the documentation]**(docs.djangoproject.com/en/dev/ref/models/fields/#arguments) will explain why they occur.

2019年07月24日35分18秒

Good answer, but I don't think you were successful in avoiding rudeness :P The "why" is not obvious unless you are aware of how django works internally.

2019年07月24日35分18秒

For someone just learning the framework, this would not be obvious.

2019年07月23日35分18秒

Thanks, the error message was not obvious to me either, but your explanation about the reverse relation was very helpful.

2019年07月24日35分18秒

Just because the Clash were a good band, doesn't make them a particularly descriptive error message ;)

2019年07月24日35分18秒

It should also be mentioned that if you do not need to use the reverse relations for all models. In some cases you might wish the model relation to be one way. In this case you use related_name='+'. This tells Django to create a one way relation and ignore the reverse relation.

2019年07月24日35分18秒