Django Reverse One-To-One Key

Jon*_*ner 0 python django django-models django-views

我正在使用DjangoBB论坛系统,我试图添加一些功能.

我们在这里有一些不同的模型.我将包括重要的部分.

用户(来自django.contrib.auth.models){无重要代码显示}

个人资料和帖子(来自djangobb_forum.models):

class Profile(models.Model):
    user = AutoOneToOneField(User, related_name='forum_profile', verbose_name=_('User'))
    //AutoOneToOne is a slight, irrelevant modification of OneToOne, just pretend that its OneToOne for these purposes
    ...
    allowed_to_post = models.IntegerField(max_length=2, default=0)

class Post(models.Model):
    topic = models.ForeignKey(Topic, related_name='posts', verbose_name=_('Topic'))
    user = models.ForeignKey(User, related_name='posts', verbose_name=_('User'))
Run Code Online (Sandbox Code Playgroud)

我需要创建一个视图,显示与帖子关联的用户关联的配置文件的所有帖子,其allowed_to_post值小于5.我尝试了以下两种方法:

d_posts = Post.objects.filter(user__profile__allowed_to_post__lt = 5)
Run Code Online (Sandbox Code Playgroud)

d_users = User.objects.filter(profile__allowed_to_post__lt = 5)
d_posts = Post.objects.filter(user__in=d_users)
Run Code Online (Sandbox Code Playgroud)

两者都给出了信息:

Cannot resolve keyword 'profile' into field. Choices are: _message_set, ban_users, date_joined, email, first_name, forum, forum_profile, groups, id, is_active, is_staff, is_superuser, last_login, last_name, logentry, password, post, posts, posttracking, received_messages, registrationprofile, reported_by, reputations_from, reputations_to, sent_messages, subscriptions, topic, user_permissions, userassociation, username, zapped_by
Run Code Online (Sandbox Code Playgroud)

我不明白为什么它不起作用,因为即使使用OneToOne字段,也应该有反向关联.

如果您确实需要查看更多代码,请告诉我们.

S.L*_*ott 5

related_name='forum_profile'相关名称更改user__profile...user__forum_profile...

这是外键或一对一键字段的相关名称属性的要点.