在模板django中显示产品名称

问题描述 投票:0回答:1

我想在其面板中显示所有用户订单,因此我有以下模型: 这是我在 django 中的产品模型,在我的订单模型中,我有 Productfk 字段,它是用户产品的 id。

class Product(models.Model):
    id= models.IntegerField(primary_key=True)
    activedate = models.DateField()
    name= models.CharField(max_length=256)
    description = models.TextField()
    
    #following u can set user owner for this row
    #owner =models.ForeignKey(to=User,on_delete=models.CASCADE)
    category = models.CharField(max_length=256)
    unit =models.CharField(max_length=50)
    active = models.BooleanField(default=False)
    
    unitprice = models.DecimalField(max_digits=18, decimal_places=0)
    quantity = models.FloatField()
    minorder = models.FloatField()
    maxorder = models.FloatField()
    readytopay = models.BooleanField(default=False)
    showquantity = models.BooleanField(default=False)
    lastupdate = models.DateField()
    def __str__(self):
        return self.name

以下是我的订单模型:

class Orders(models.Model):
    id = models.IntegerField(primary_key=True)
    customerfk = models.ForeignKey(to=User,on_delete=models.CASCADE)
    oxygenid = models.IntegerField()
    financialfk = models.IntegerField()
    orderdate = models.DateTimeField()
    productfk = models.IntegerField()
    unit = models.CharField(max_length=50)
    quantity = models.FloatField()
    unitprice = models.DecimalField(max_digits=18, decimal_places=0)
    discount = models.DecimalField(max_digits=18, decimal_places=0)
    totalprice = models.DecimalField(max_digits=18, decimal_places=0)
    onlinepayment = models.DecimalField(max_digits=18, decimal_places=0)
    customerdesc = models.TextField()
    companydesc = models.TextField()
    userip = models.CharField(max_length=20)
    status = models.CharField(max_length=50)
    creationdate = models.DateTimeField()
    
    def __str__(self):
        return self.status

这是我的订单视图

@login_required(login_url='/authentication/login')
def index(request):
    unit=Unit.objects.all()
    orderstatus=OrderStatus.objects.all()
    #order=Orders.objects.all()
    order =Orders.objects.select_related('customerfk')
    paginator = Paginator(order,20)
    page_number = request.GET.get('page')
    page_obj = Paginator.get_page(paginator,page_number)
    #currency = UserPreference.objects.get(user=request.user).currency
    context={
        'order':order,
        'orderstatus':orderstatus,
        'unit':unit,
        'page_obj':page_obj
    }
    return render(request,'orders/index.html',context)

我如何在每个订单的模板视图中显示我的产品名称

python django django-views django-templates
1个回答
0
投票

订单表中应该有产品关系,例如外键。之后,您可以在使用语法循环订单的同时访问 Django 模板中的产品名称。

{{ orderItem.product.name }}.

© www.soinside.com 2019 - 2024. All rights reserved.