如何通过模型编写与自身具有多对多关系的 Django 模型
我想要一个与其自身具有 ManyToMany
关系的模型,我不知道如何编写它,但我会尝试编写一些代码来说明我想要做什么。
class Person(models.Model):
name = models.CharField()
occupation = models.CharField()
friends = models.ManyToManyField('self', through = PersonFriends)
我希望朋友们在 ManyToMany
字段中通过通过关系浏览我的模型
class PersonFriends(models.Model)
???
comment = models.CharField()
,如果另一个模型的名称是“Pet”,例如,我会在其中命名我的字段通过类 person
和 pet
并使它们成为模型。例如,ForeignKey(Person)
和 Pet
在我的 PersonFriends
模型中为两个人命名我的字段
-现在它们是同一模型的字段?
I want to have a model with a ManyToMany
relationship with itself, I don't know how to write this but I'l try to write some code to illustrate what I want to do.
class Person(models.Model):
name = models.CharField()
occupation = models.CharField()
friends = models.ManyToManyField('self', through = PersonFriends)
My Model that I want the friends to go through
class PersonFriends(models.Model)
???
comment = models.CharField()
In a ManyToMany
field with through relationship if the other model's name was "Pet" for example I'd name my fields in that through class person
and pet
and make them models. ForeignKey(Person)
and Pet
for example
What to I name my fields
in my PersonFriends
model for the two person-fields now that they are the same model?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以这样做:
You can do something like this:
一切都在 官方文档中进行了描述ManyToManyField.through_fields (您可以在那里搜索“递归关系”短语以快速找到您需要的内容):
对于 django 1.11,您必须指定 through 和(!) through_fields 参数:
Everything is described in the official docs for ManyToManyField.through_fields (you can search for 'recursive relationships' phrase there to quickly find what you need):
for django 1.11 you have to specify through and (!) through_fields arguments:
不假设友谊是对称的。因为巴斯光年可能是伍迪的朋友,但伍迪直到电影接近尾声才与巴斯光年成为朋友。您可以简化这两个模型,并且仍然具有合理的查找名称。如果是良好的友谊,您当然需要确保定义两个 PersonFriends。
这还有一个额外的好处,那就是对友谊的每个方向都有评论。即提利昂认为珊莎是一个可爱又聪明,但却迷失的女孩。而珊莎可能会认为提利昂是一个丑陋但聪明且心地善良的人。
Without assuming that friendships are symmetrical. Because Buzz Lightyear might be Woody's friend, but Woody isn't friends with Buzz Lightyear till near the end of the film. You can simplify both models and still have reasonable lookup names. You would of course need to make sure that you define two PersonFriends if it's a good friendship.
This has the added bonus of a comment for each direction of the friendship. i.e. Tyrion thinks Sansa is a lovely and intelligent, but lost girl. Whereas Sansa might think that Tyrion is an ugly but clever and kind-hearted kinda guy.
这是来自我的模型结构中与自身的多对多关系的数据库表结构。 Django 是这样定义的..
this is from db table structure of a ManyToMany relation to self from my Model structure. Django defines it like that..