【问题标题】:Django add or update dates in databaseDjango 在数据库中添加或更新日期
【发布时间】:2021-03-06 15:49:38
【问题描述】:

我想创建一个具有一些功能的表单: 如果尚未使用 GTIN -> 在我的数据库中创建新产品 否则,如果 GTIN 已使用 -> 更新我的数据库中与此 GTIN 对应的 dateExpiry

我的代码如下:

form.py:

class productForm(ModelForm):
class Meta:
    model = Product
    fields = ['GTIN', 'name', 'price', 'expiryDate']

views.py:

 def index(request):
    if request.method == "POST":
        form = productForm(request.POST).save()
        return redirect('/cbPlus')
    else :
        form = productForm()

    template = loader.get_template('cbPlus/index.html')
    product = Product.objects.order_by('expiryDate')
    context = {
        'product' : product,
        'form' : form
    }
    return HttpResponse(template.render(context, request=request))

models.py:

class Product(models.Model):
   GTIN = models.BigIntegerField(unique=True)
   name = models.CharField(max_length=25)
   price = models.FloatField(max_length=25)
   expiryDate = models.DateTimeField(max_length=25)

【问题讨论】:

    标签: python django django-forms


    【解决方案1】:

    文本中没有问题,所以我猜您需要有关从表单创建产品的帮助。如果你需要在 POST 中添加这样的内容。

    form = productForm(request.POST)
    if not form.is_valid():
        ... # Handle form errors here
    product = form.save(commit=False)
    old_product = Product.objecte.filter(GTIN=product.GTIN).first()
    if old_product:
        old_product.expiryDate = product.expiryDate
        old_product.save()
    else:
        product.save()
    
    return redirect('/cbPlus')
    

    【讨论】:

      【解决方案2】:

      如果我的理解正确,您希望您的 POST 处理新产品或更新的产品。

      这是我要做的,使用一些伪代码:

      if request.method = "POST":
          if not Product.objects.filter(GTIN=request.GTIN).exists():
                new_product = Product(request.GTIN, ...)
                new_product.save()
          else:
              update_product = Product.objects.filter(GTIN=request.GTIN)
              update_product.name = request.name
              #update the remaining fields...
              update_product.save()
      

      【讨论】:

      • 好的,谢谢,但现在我得到了这个异常:'WSGIRequest' 对象没有属性'GTIN',你有什么想法吗?
      • 使用request.POST['GTIN'] 而不是request.GTIN
      【解决方案3】:

      通常,您应该将createupdate 放在不同的视图中。 update 使用应该传递给视图的 primary_key 检索已保存的对象。但就您而言,您可以执行以下操作:

      def index(request):
          product = Product.objects.filter(GTIN=request.POST['GTIN']).first()
          if product:
              # update
              form = productForm(request.POST, instance=product)
          else:
              # create
              form = productForm(request.POST)
          if form.is_valid():
              form.save()
              return redirect('/')  # Usually redirect after object create or update
          return render(request, 'index.html', {'form': form})
      

      【讨论】:

      • 'too many values to unpack (expected 2)' : 你知道如何解决这个异常吗?
      • 我需要查看更多的回溯。你能发布更多的错误吗?
      • 对不起,我的代码有错误,我刚刚更新了它。请尝试更新版本。将GTIN= 放入过滤器中。
      • 异常类型:MultiValueDictKeyError 异常值:'GTIN' product = Product.objects.filter(request.POST['GTIN']).first()
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-15
      • 2022-01-14
      • 2011-08-17
      • 1970-01-01
      • 2015-08-22
      • 1970-01-01
      相关资源
      最近更新 更多