#django #django-models #django-views #django-templates
Вопрос:
Я новичок в django.я пытаюсь создать приложение для создания счетов-фактур с помощью django . где работник вводит идентификатор клиента и он принимает на странице счета-фактуры.в разделе счета сотрудников можно ввести код продукта и продукта и информацию о клиенте будут добавлены в счет объектной модели .поэтому я хочу, чтобы добавить все продукты по той же накладной ID или хотите фильтровать по ID клиента, который будет вам все счета-фактуры для клиента с идентификатором вот код models.py
from django.db import models
from account.models import Customer
from product.models import Product
class Invoice(models.Model):
products = models.ForeignKey(Product, on_delete=models.CASCADE, default=1)
customers = models.ForeignKey(Customer, on_delete=models.CASCADE, default=4)
date = models.DateTimeField(auto_now_add=True)
total = models.BigIntegerField(default=0)
quantity = models.IntegerField(default=0)
views.py
def billingform(request):
if request.method == "POST":
cid = request.POST["customerId"]
customer = Customer.objects.get(id=cid)
request.session['customerId'] = cid
return redirect('invoice')
return render(request, 'bform.html')
def invoice(request):
cid = request.session.get('customerId')
customer = Customer.objects.get(id=cid)
if request.method == "POST":
pd = request.POST["product_id"]
qt = int(request.POST["quantity"])
product = Product.objects.get(id=pd)
pd_price = int(product.selling_price)
total = pd_price*qt
pd_name = product.name
Invoice.objects.create(products = product,
quantity = qt, total=total, customers = customer)
cust_id = Invoice.objects.get(id, customers=customer.id)
product_name = cust_id.products
product_quantity = cust_id.quantity
product_total = cust_id.total
return render(request, 'invoice.html', { "total":product_total, "customer": customer,
"product_names": product_name,
"quantitys": product_quantity,})
else:
return render(request, 'invoice.html', {"customers": customer})
он показывает ошибку TypeError: не удается распаковать неитерабельный объект builtin_function_or_method
Комментарии:
1. Пожалуйста, обновите вопрос с помощью трассировки стека.
Ответ №1:
cust_id = Invoice.objects.get(id=customer.id)
Get: извлекает один объект и назначает его переменной. Так что это должно исправить вашу ошибку
Ответ №2:
Может быть, ошибка в этой строке..
cust_id = Invoice.objects.get(id, customers=customer.id)
Превратиться в
cust_id = Invoice.objects.get(id=customer.id, customers=customer.id)
И дайте мне знать, если это не работает, а затем покажите мне также свой файл шаблона..