2024-04-25 23:31:04 发布
网友
一对多关系。你知道吗
我想获取post的注释,其中两者位于名为post的同一个表中,但注释的数据位于parent列(父post)
parent
我来自PHP,使用laravel我只想$this->hasMany('post','parent');
$this->hasMany('post','parent');
但在django我该怎么做?你知道吗
返回该帖子的所有评论。必须显示职位和有一个属性的评论,并显示评论那里。你知道吗
你可以在这里看到:https://docs.djangoproject.com/en/dev/ref/models/relations/
“相关经理”是在一对多或多对多相关上下文中使用的经理。这种情况有两种:
外国关系的“另一面”。即:
from django.db import models class Reporter(models.Model): # ... pass class Article(models.Model): reporter = models.ForeignKey(Reporter)
在上面的示例中,下面的方法将在管理器reporter.article_set上可用。你知道吗
reporter.article_set
你的例子是post.post_set。如果不喜欢post_set,可以在ForeignKey中指定related_name:
post.post_set
post_set
related_name
parent = models.ForeignKey(Post, related_name='comments')
此时您可以查询:post.comments
post.comments
你可以在这里看到:https://docs.djangoproject.com/en/dev/ref/models/relations/
“相关经理”是在一对多或多对多相关上下文中使用的经理。这种情况有两种:
外国关系的“另一面”。即:
在上面的示例中,下面的方法将在管理器
reporter.article_set
上可用。你知道吗你的例子是
post.post_set
。如果不喜欢post_set
,可以在ForeignKey中指定related_name
:此时您可以查询:
post.comments
相关问题 更多 >
编程相关推荐