如何在 Django 中创建查找?

发布于 2024-07-10 08:27:30 字数 257 浏览 9 评论 0原文

我有一个问题模型& 表单,此模型中的字段之一是 userid=ForeignKey(User),这在问题模型上运行得非常好,我能够从下拉列表中选择用户。

但是,当我想列出模型中的问题时,有点棘手,这是从用户表中查找用户名的最佳方式? 因为此时我无法使用下拉菜单!

我想要一个简单的东西,例如

问题标题 询问者:查找用户名

I have a Question model & Form, one of the fields in this model is userid=ForeignKey(User), this Works perfectly well on the Question Model, am able to pick the user from a drop down.

But kind a tricky when i want to list the question from the model, which is the best way to lookup the user name from the Users table? becouse at this point i cant have the dropdown!

I want to have a simple thing e.g.

Question Title
asked by:lookup user Name

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

网名女生简单气质 2024-07-17 08:27:31

您的字段名称(userid 而不是 user)让我认为您可能对 Django 的 ForeignKey 的行为感到困惑。

如果您定义这样的模型:

from django.contrib.auth.models import User
from django.db import models

class Question(models.Model):
    user = models.ForeignKey(User)
    title = models.CharField(max_length=100)
    ...

    def __unicode__(self):
        return self.title

然后将 Question 实例化为 question

>>> question.user # the `User` instance
<User: username>

>>> question.user_id # the user's primary key
1

看起来您可能期望 question.userid 成为用户的主键,而不是它实际的内容:User 实例本身。 当您访问 question.userid 时,会执行数据库查找,但它是由 Django 使用 question.userid_id 的值自动完成的。 我会将 userid 字段重命名为 user 以避免混淆。

这样一来,我认为您要做的就是列出问题及其相关用户。 如果是这种情况,请在模板中执行以下操作:

<ol>
{% for question in questions %}
    <li>{{ question }} asked by: {{ question.user }}</li>
{% endfor %}
</ol>

The name of your field (userid instead of user) makes me think that you may be confused about the behavior of Django's ForeignKey.

If you define a model like this:

from django.contrib.auth.models import User
from django.db import models

class Question(models.Model):
    user = models.ForeignKey(User)
    title = models.CharField(max_length=100)
    ...

    def __unicode__(self):
        return self.title

And then instantiate a Question as question:

>>> question.user # the `User` instance
<User: username>

>>> question.user_id # the user's primary key
1

It looks like you may be expecting question.userid to be the user's primary key, rather than what it actually is: the User instance itself. When you access question.userid, a database lookup is performed, but it's done automatically by Django using the value of question.userid_id. I would rename the userid field to user to avoid confusion.

With that out of the way, I think what you are trying to do is list the questions along with their associated users. If that's the case, do something like this in your template:

<ol>
{% for question in questions %}
    <li>{{ question }} asked by: {{ question.user }}</li>
{% endfor %}
</ol>
┾廆蒐ゝ 2024-07-17 08:27:31

我发现你的问题含糊不清。 如果您想获取与给定 user_name 的特定 User 实例相关的所有 Question 实例,您可以这样做:

questions = Question.objects.filter( userid__username='user_name' )

如果您已经有一个 User 实例(例如,保存在一个名为 user 的变量中),并且想要获取与其相关的所有 Question 对象,您可以获取那些有:

questions = user.question_set.all()

I find your question vague. If you want to fetch all Question instances that are related to a particular User instance given a user_name, you can do thus:

questions = Question.objects.filter( userid__username='user_name' )

If you already have a User instance (held, say, in a variable called user) and would like to fetch all Question objects related to it, you can get those with:

questions = user.question_set.all()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文