Django 'tuple'对象没有属性'保存'

f4t66c6m  于 2023-08-08  发布在  Go
关注(0)|答案(1)|浏览(121)

我有这个模型CustomerPurchaseOrderDetail,Product,Customer,我只是想如果CustomerPurchaseOrderDetail模型中存在相同的productID和相同的CustomerID,如果CustomerPurchaseOrderDetail中存在相同的productID和相同的CustomerID,则数量将增加1。

userID = request.POST.get("userID")
client = Customer(id=userID)
vegetables_id = request.POST.get("id")
quantity = request.POST.get("quantity")
v = Product(id=vegetables_id)
price = request.POST.get("price")
discount = request.POST.get("discount_price")
insert = CustomerPurchaseOrderDetail.objects.get_or_create(
    profile=client,
    product = v,
    quantity=quantity,
    unitprice=price,
    discounted_amount = discount,
    discounted_unitprice = discount,
)
insert.save()

字符串
这是我的模特

class CustomerPurchaseOrderDetail(models.Model):
    profile = models.ForeignKey(Customer, 
                                on_delete=models.SET_NULL, null=True, blank=True,
                                verbose_name="Client Account")
    product = models.ForeignKey(Product, 
                                                on_delete=models.SET_NULL, null=True, blank=True,
                                                verbose_name="Product")
    quantity = models.FloatField(max_length=500, null=True, blank=True, default=1)

class Product(models.Model):
      product = models.CharField(max_length=500)

class Customer(models.Model):
    user = models.OneToOneField(User, related_name="profile", on_delete=models.CASCADE)
    firstname = models.CharField(max_length=500, blank=True)
    lastname = models.CharField(max_length=500, blank=True)
    contactNumber = models.CharField(max_length=500, blank=True)
    email = models.CharField(max_length=500, blank=True)


这是我的错


的数据
这是我的完整追踪

Environment:

Request Method: POST
Request URL: http://127.0.0.1:8000/batchaddtocart/

Django Version: 2.2.4
Python Version: 3.7.4
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.humanize',
 'customAdmin',
 'sweetify']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Traceback:

File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\User\Desktop\LastProject\OnlinePalengke\customAdmin\views.py" in batchaddtocart
  939.     insert.save()

Exception Type: AttributeError at /batchaddtocart/
Exception Value: 'tuple' object has no attribute 'save'

pxy2qtax

pxy2qtax1#

.get_or_create(…)[Django-doc]返回一个二元组:第一个元素是你检索或创建的项,第二个元素是布尔值,表示它是否被创建。你可以使用iterable unpacking来解包对象:

insert, __ = CustomerPurchaseOrderDetail.objects.get_or_create(
    profile=client,
    product = v,
    quantity=quantity,
    unitprice=price,
    discounted_amount = discount,
    discounted_unitprice = discount,
)

字符串
但是调用.save()没有多大意义,因为.get_or_create(…)已经将对象保存到数据库中。

编辑:可以通过以下方式增加数量:

insert, created = CustomerPurchaseOrderDetail.objects.get_or_create(
    profile=client,
    product = v,
    unitprice=price,
    discounted_amount = discount,
    discounted_unitprice = discount,
    defaults=dict(quantity=quantity)
)

if not created:
    insert.quantity += quantity
    insert.save()

相关问题