从ManytoManyFIeld

问题描述

对于Django和模型来说这是相当新的东西,我正在尝试创建一个愿望清单,人们可以将其添加到愿望清单,然后通过单击愿望清单的链接来查看其愿望清单中的内容。我为愿望清单创建了一个单独的模型,该模型具有用户的外部字段,然后针对要添加到愿望清单中的项目设置了多对多字段。现在,我正在尝试使用Django中的管理员视图查看为用户创建的愿望清单。

我遇到的问题是,当我尝试将其愿望清单打印到模板时,页面上会出现以下内容

<QuerySet [<listings: item: Nimbus,description:this is the nimbus something at a price of 300 and url optional(https://www.google.com/ with a category of )>,<listings: item: broom,description:this is a broom stick at a price of 223 and url optional(www.youtube.com with a category of broom)>,<listings: item: wand,description:this is a wand at a price of 3020 and url optional(www.twitter.com with a category of sales)>]>

我理想中想要的是将查询集拆分为这样,以便当我根据用户迭代项目时,两个列表及其信息将位于html页面的单独行上。我知道它必须来自我在模型本身上设置的字符串表示形式,但不知道如何设法实现这一点。我尝试.all,.values,.values_list,.prefetch_related,当我转到页面或无法迭代时仍会得到相同的结果

如果有什么好处,那就是可以访问商品,说明和价格,并将其打印到愿望清单中每个商品的页面上。

这可能是可行的,还是我的方法错误的,并且应该将愿望清单添加到其他形式之一中,但是我认为这应该可以工作。不知道我是不是已经关闭或者我的一个文件中缺少某些东西,或者需要创建一个新的单独视图。

下面的代码

models.py

class User(AbstractUser):
    pass

class listings(models.Model):
    item = models.CharField(max_length=100)
    description = models.CharField(max_length=100)
    price = models.IntegerField()
    url = models.CharField(max_length=100,blank=True)
    category = models.CharField(max_length=100,blank=True)

    def __str__(self):
        return f"item: {self.item},description:{self.description} at a price of {self.price} and url optional({self.url} with a category of {self.category})"
        

class bids(models.Model):
    desired = models.ForeignKey(listings,on_delete=models.CASCADE,related_name="desired",null=True)
    bid = models.IntegerField()

    def __str__(self):
        return f"{self.desired} Bid: {self.bid}"


class comments(models.Model):
    
    information = models.ForeignKey(listings,related_name ="Review",null=True)
    title = models.CharField(max_length=100)
    comment = models.CharField(max_length=100)

    def __str__(self):
        return f"title {self.title} Comment: {self.comment}"

class wl(models.Model):
    user = models.ForeignKey(User,related_name="users")
    product = models.ManyToManyField(listings,related_name="item_wished")

    def __str__(self):
        return f"{self.product.all()}"

views.py


def watch_list(request):


    user = wl.objects.filter(user_id = request.user.id).prefetch_related('product')
    
    return render(request,'auctions/wishlist.html',{
        'wishes': user
    })


html模板

{% extends "auctions/layout.html" %}

{% block body %}
    <h2>Active Listings</h2>
    {% for i in wishes.all %}

    
        <li> {{ i }} <li>

    {% endfor %}
{% endblock %}


解决方法

您的代码似乎正常。尝试使用结构(可能是HTML表)对模板进行一些调整:

{% extends "auctions/layout.html" %}

{% block body %}
    <h2>Active Listings</h2>

    <table>
    {% for w in wishes %}
        <tr>
            <td>{{ w.pk }}</td>
            <td>{{ w.product.item }}</td>
            <td>{{ w.product.description }}</td>
            <td>{{ w.product.price }}</td>
        </tr>
    {% endfor %}
    </table>
{% endblock %}
,

查询将返回所有wl对象。使用:

{% for w in wishes %}

您遍历所有wl对象。每个“ w”都有一个“ product”字段,该字段具有多对多关系-它包含多个对象,需要再次进行迭代:

{% for each_wl in wishes %}
     {% for each_product in each_wl %}
           {{ each_product.item }}

另外,将变量/类命名为更详细的名称。