【发布时间】:2020-10-21 04:44:10
【问题描述】:
我有Customer和Products之间的关系,customer_id保存在Product模型表中,但我想根据customer在我的视图页面上显示产品,假设如果客户登录使用他的详细信息,然后他只能看到他的产品,但目前他可以看到所有产品。请告诉我我该怎么做。
这是我的models.py 文件...
class Customer(models.Model):
name=models.CharField(default=None)
user=models.OneToOneField(User, related_name='userwithCustomer', on_delete=models.CASCADE)
class Product(models.Model):
name=models.CharField(default=None)
customer=models.Foreignkey(Customer, related_name='customer_product', on_delete=models.CASCADE)
这是我的views.py 文件...
def display_data(request):
test_display = Product.objects.all()
context = {
'test_display': test_display
}
return render(request, 'page.html', context)
这是我的page.html 文件...这里我正在尝试根据登录的客户显示产品..
<p>{{test_display.count}}</p>
【问题讨论】:
标签: python django django-models django-views django-templates