django - Django ORM 过滤器提供了一个 QuerySet 对象,我无法从中检索数据
问题描述
-----------------------------Models-----------------------------
class Pattern(models.Model):
name = models.CharField(_("Pattern Name"), unique=True, max_length=32)
created_on = models.DateTimeField(_("Created On"), editable=False, auto_now_add=True)
class Base(models.Model):
l_shoulder = models.ImageField(_("Left Shoulder"), upload_to='left_shoulders/')
r_shoulder = models.ImageField(_("Right Shoulder"), upload_to='right_shoulders/')
l_front = models.ImageField(_("Left Front"), upload_to='left_fronts/')
r_front = models.ImageField(_("Right Front"), upload_to='right_fronts/')
l_collar_base = models.ImageField(_("Left Collar Base"), upload_to='left_collor_bases/')
r_collar_base = models.ImageField(_("Right Collar Base"), upload_to='right_color_bases/')
yoke_bottom = models.ImageField(_("Bottom Yoke"), upload_to='neck_bottoms/')
yoke_top = models.ImageField(_("Top Yoke"), upload_to='neck_tops/')
placket = models.ImageField(_("Placket"), upload_to='plackets/')
pattern = models.OneToOneField(Pattern, on_delete=models.CASCADE)
class Collar(models.Model):
CATAGORY_CHOICES = (
('RR', 'Regular'),
('BR', 'Big Round'),
('CA', 'Cut Away'),
('DB', 'Dual Button'),
('PH', 'Pin Hole'),
('SW', 'Semi Wide'),
('RB', 'Round Button Down'),
('SP', 'Short Point'),
('SS', 'Stand'),
('WS', 'Wide Spread'),
)
inner = models.ImageField(_("Inner Collar"), upload_to="inner_collars/")
upper = models.ImageField(_("Upper Collar"), upload_to="upper_collars/")
outer_r = models.ImageField(_("Outer Right Collar"), upload_to="outer_right_collars/")
outer_l = models.ImageField(_("Outer Left Collar"), upload_to="outer_left_collars/")
catagory = models.CharField(max_length=2, choices=CATAGORY_CHOICES)
pattern = models.ForeignKey(Pattern, related_name="collar", on_delete=models.CASCADE)
-----------------------------Views-----------------------------
def design(request):
pattern = Pattern.objects.get(name="p1")
collar = pattern.collar.all().filter(catagory="RR")
cont = {
"base": pattern.base,
"collar": collar,
}
return render(request, 'shirts/shirtdesign.html', context=cont)
所以这是我试图访问衣领属性的模板,希望这能帮助你更好地理解我的问题......
-----------------------------模板-------------------- -------- {% 扩展'base.html' %}
{% block title %}
Design
{% endblock title %}
{% block content %}
Design Your Shirt Here...
<div style="height:500px; width:400px;">
<img style="position:absolute; display:inline; width:300px; height:auto; margin-left:-150px; z-index:0;" src="{{ base.l_shoulder.url }}">
<img style="position:absolute; display:inline; width:300px; height:auto; margin-left:-150px; z-index:0;" src="{{ base.r_shoulder.url }}">
<img style="position:absolute; display:inline; width:300px; height:auto; margin-left:-150px; z-index:0;" src="{{ base.l_front.url }}">
<img style="position:absolute; display:inline; width:300px; height:auto; margin-left:-150px; z-index:0;" src="{{ base.r_front.url }}">
<img style="position:absolute; display:inline; width:300px; height:auto; margin-left:-150px; z-index:0;" src="{{ base.l_collar_base.url }}">
<img style="position:absolute; display:inline; width:300px; height:auto; margin-left:-150px; z-index:0;" src="{{ base.r_collar_base.url }}">
<img style="position:absolute; display:inline; width:300px; height:auto; margin-left:-150px; z-index:0;" src="{{ base.yoke_top.url }}">
<img style="position:absolute; display:inline; width:300px; height:auto; margin-left:-150px; z-index:0;" src="{{ base.yoke_bottom.url }}">
<img style="position:absolute; display:inline; width:300px; height:auto; margin-left:-150px; z-index:0;" src="{{ base.placket.url }}">
<img style="position:absolute; display:inline; width:300px; height:auto; margin-left:-150px; z-index:0;" src="{{ collar.inner.url }}">
<img style="position:absolute; display:inline; width:300px; height:auto; margin-left:-150px; z-index:0;" src="{{ collar.upper.url }}">
<img style="position:absolute; display:inline; width:300px; height:auto; margin-left:-150px; z-index:0;" src="{{ collar.outer_r.url }}">
<img style="position:absolute; display:inline; width:300px; height:auto; margin-left:-150px; z-index:0;" src="{{ collar.outer_l.url }}">
<img style="position:absolute; display:inline; width:300px; height:auto; margin-left:-150px; z-index:0;" src="{{ collar.button.url }}">
</div>
{% endblock content %}
在模板中,我无法通过{{ collar.inner.url }}
或
{{ collar.upper.url }}
等方式检索图像网址。我不确定出了什么问题。当我尝试在视图中打印衣领print(collar)
时<QuerySet [<Collar: Collar object (1)>]>
。我不确定如何处理QuerySet对象来获取所需的数据。任何帮助将非常感激...
解决方案
collar = pattern.collar.all().filter(catagory="RR")
返回查询集而不是记录。要打印collar
你应该把它放在模板中的forloop中,比如
{% for collr in collar %}
{{ collr.inner.url }}
{%endfor%}
或者只是将衣领作为单个记录传递给模板,使用collar = pattern.collar.filter(catagory="RR").first()
推荐阅读
- numpy - 如何有效地过滤 numpy 数组,使其包含第二个数组中包含的行
- azure - 有没有更好的本地读取和全局写入的方法?(设计分布式系统)
- azure-data-factory - 无法将 ADF polybase 文件从 Azure 存储到 Synapse 池(SQL 数据仓库)
- javascript - Node.js 等待 fs.mkdir() 完成后再继续?
- html - 如何隐藏不适合网格的项目?
- sql - 雅典娜 sql date_diff
- powershell - 无法发布到 Azure devops Nuget
- php - 在另一个 php 页面中收到时,发送的 JSON 数组为空
- python - Tensorflow 数据集:有没有办法只修改一定比例的标签?
- excel - 根据另一个工作表中的值在多个单元格中绘制圆圈