为什么我看不到Django的用户添加信息?

时间:2019-08-05 15:04:49

标签: django model details

我正在使用Django 2.2和PostgreSQL。我想显示用户已添加到详细信息页面的产品信息。我在“ StoreOtherInfo”模型中看到了该信息,但是在“产品”模型中却没有看到该信息。我该怎么办?

store / models.py

class StoreOtherInfo(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE,blank=True, null=True)
    phone = models.CharField(max_length=11)
    fax = models.CharField(max_length=11)
    province = models.CharField(max_length=11)
    district = models.CharField(max_length=11)
    neighborhood = models.CharField(max_length=11)

    def __str__(self):
        return self.user.username

products / models.py

class Product(models.Model):
    seller = models.ForeignKey(User, on_delete = models.CASCADE)
    product_name = models.CharField(max_length = 50)
    description = RichTextField()
    added_date = models.DateTimeField(auto_now_add=True)
    def __str__(self):
        return self.seller

store / views.py

from store.models import StoreOtherInfo
from products.models import Product

def neighbor_detail(request,username):

    neighbor_detail = get_object_or_404(User,username = username)
    neighbor_list = StoreOtherInfo.objects.all()
    product_list = Product.objects.all()
    return render(request, 'store/neighbor_detail.html', {'neighbor_detail':neighbor_detail, 'neighbor_list':neighbor_list, 'product_list':product_list})

templates / neighbor_detail.html

      <strong><p>{{neighbor_detail.first_name}} {{neighbor_detail.last_name}}</p></strong>
      <p>{{neighbor_detail.username}}</p>
      <p>{{neighbor_detail.email}}</p>
      <p>{{neighbor_detail.storeotherinfo.phone}}</p>
      <p>{{neighbor_detail.storeotherinfo.fax}}</p>
      <p>{{neighbor_detail.storeotherinfo.province}}</p>
      <p>{{neighbor_detail.storeotherinfo.district}}</p>
      <p>{{neighbor_detail.storeotherinfo.neighborhood}}</p>
      <p>{{neighbor_detail.product.product_name}}</p>
      <p>{{neighbor_detail.product.description}}</p>
      <p>{{neighbor_detail.product.added_date}}</p>

2 个答案:

答案 0 :(得分:0)

一个User可以有多个Product,因此您不能执行neighbor_detail.product,因为product上没有定义User。您需要使用{% for product in neighbor_detail.product_set.all %}遍历产品列表,然后才能显示每个product的属性。

阅读[this],以获得有关一对多关系的更多信息。

答案 1 :(得分:0)

根据您的产品型号:

seller = models.ForeignKey(User, on_delete = models.CASCADE)

用户模型和产品模型之间的关系是一对多的。这意味着每个用户(在本例中为卖方)可以拥有多个产品。 因此,当您尝试像在模板中一样访问User对象的Product对象:<p>{{neighbor_detail.product.product_name}}</p>时,由于neighbor_detail.product不是Product类的单个对象,它最终是一个归因错误,它是一个集合。 以此替换您的模板代码,希望您能了解发生了什么。

<strong><p>{{neighbor_detail.first_name}} {{neighbor_detail.last_name}}</p></strong>
<p>{{neighbor_detail.username}}</p>
<p>{{neighbor_detail.email}}</p>
<p>{{neighbor_detail.storeotherinfo.phone}}</p>
<p>{{neighbor_detail.storeotherinfo.fax}}</p>
<p>{{neighbor_detail.storeotherinfo.province}}</p>
<p>{{neighbor_detail.storeotherinfo.district}}</p>
<p>{{neighbor_detail.storeotherinfo.neighborhood}}</p>

{% for product in neighbor_detail.product_set.all %}
  {{product.product_name}}<br/>
  {{product.description}}<br/>
  {{product.added_date}}<br/>
{% endfor %}

请注意,product_set是django与与每个用户相关的产品相关联的默认名称。