如何使用Django模板从模型的父对象中获取字段?

huangapple go评论69阅读模式
英文:

How do I use django templates to get a field from the parent object of a model?

问题

以下是要翻译的内容:

"我正在尝试创建一个界面,让学生可以根据共同的兴趣与班级中的其他学生匹配,以进行小组项目。我在Django视图中有一个查询集(queryset),然后我可以使用select_related获取父对象,并在Anaconda Prompt中打印字段(在本地服务器上运行时),但似乎无法在模板中显示该字段...我漏掉了什么?

这是我的模型文件的相关部分:

class Profile(models.Model):
    first_nm = models.CharField(max_length=100)
    last_nm = models.CharField(max_length=100)

class Potential(models.Model):
    user1 = models.ForeignKey(Profile, related_name='user1', on_delete=models.CASCADE)
    user2 = models.ForeignKey(Profile, related_name='user2', on_delete=models.CASCADE)

这是我的视图的相关部分:

def matches(request):
    my_user = request.user
    my_id = Profile.objects.get(slug=my_user)
    the_matches = Potential.objects.filter(Q(user1=my_id) | Q(user2=my_id)).all().select_related('user1','user2')
    print(the_matches.values())
    print(the_matches.values('user2__first_nm'))
    return render(request,'projects/matches_list.html', { 'matches':the_matches})

这是模板的相关部分:

{% for match in matches %}
     <div class="matches">
       <p>{{ match.user1.first_nm }}</p>
       <p>{{ match.user2.first_nm }}</p>
     </div>
{% endfor %}

在上面的模板中,为什么这不起作用?

提前感谢您的帮助!

我尝试使用如上所示的语法,例如:

<p>{{ match.user2.first_nm }}</p>

并且当仅在控制台中打印时,我得到

<QuerySet [{'user2__first_nm': 'test_nm'}]>

从上面视图中的倒数第二行,但当我在本地运行时,HTML显示为空的p标签。我做错了什么?

谢谢!


<details>
<summary>英文:</summary>

I&#39;m trying to create an interface where students can match with other students in the class for a group project based on shared interests and I have a queryset in a django view and from there I am able to use select_related to get the parent object and even print the fields in Anaconda Prompt (when running on local server) but I can&#39;t seem to get the field to show up in a template...what am I missing?

Here is the relevant part of my models file:

class Profile(models.Model):
first_nm = models.CharField(max_length=100)
last_nm = models.CharField(max_length=100)

class Potential(models.Model):
user1 = models.ForeignKey(Profile, related_name='user1', on_delete=models.CASCADE)
user2 = models.ForeignKey(Profile, related_name='user2', on_delete=models.CASCADE)


Here is the relevant part of my view:

def matches(request):
my_user = request.user
my_id = Profile.objects.get(slug=my_user)
the_matches = Potential.objects.filter(Q(user1=my_id) | Q(user2=my_id)).all().select_related('user1','user2')
print(the_matches.values())
print(the_matches.values('user2__first_nm'))
return render(request,'projects/matches_list.html', { 'matches':the_matches})


Here is the relevant part of the template:

{% for match in matches %}
<div class="matches">
<p>{{ match.user1__first_nm }}</p>
<p>{{ match.user2__first_nm }}</p>
</div>
{% endfor %}

In the template above, why does this not work?

Thanks in advance!


I have tried using the syntax above like:

<p>{{ match.user2__first_nm }}</p>

and when just printing to the console I get 
&gt; &lt;QuerySet [{&#39;user2__first_nm&#39;: &#39;test_nm&#39;}]&gt;

from the second to last line in the view above but when I run locally, the html shows up with p tag with no contents.  What am I doing wrong?

Thanks!

</details>


# 答案1
**得分**: 1

在模板中不要使用`__`,而是使用`.`。

`__` 仅在构建查询集时使用。在您的模板中,您正在访问`Profile`对象,因此要从该对象中获取属性,您需要使用语法 `object.attribute`。

您的代码将变为:

```python
{% for match in matches %}
     &lt;div class=&quot;matches&quot;&gt;
       &lt;p&gt;{{ match.user1.first_nm }}&lt;/p&gt;
       &lt;p&gt;{{ match.user2.first_nm }}&lt;/p&gt;
     &lt;/div&gt;
{% endfor %}
英文:

Do not use __ in template, but use .

__ is only used when constructing QuerySets. In your template, you're accessing the Profile object, so to get an attribute from that object you use the syntax object.attribute.

Your code becomes:

{% for match in matches %}
     &lt;div class=&quot;matches&quot;&gt;
       &lt;p&gt;{{ match.user1.first_nm }}&lt;/p&gt;
       &lt;p&gt;{{ match.user2.first_nm }}&lt;/p&gt;
     &lt;/div&gt;
{% endfor %}

huangapple
  • 本文由 发表于 2023年1月6日 14:47:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定