首页 > 解决方案 > 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对象来获取所需的数据。任何帮助将非常感激...

标签: djangodjango-modelsdjango-viewsdjango-orm

解决方案


collar = pattern.collar.all().filter(catagory="RR")返回查询集而不是记录。要打印collar你应该把它放在模板中的forloop中,比如

{% for collr in collar %}
 {{ collr.inner.url }} 
{%endfor%}

或者只是将衣领作为单个记录传递给模板,使用collar = pattern.collar.filter(catagory="RR").first()


推荐阅读