即使保存更新,Django 2 也会丢失模型的更改

我正在使用 Python(3.7) 和 Django(2.1) 开发一个项目,在该项目中我正在更新模型并保存它,之后如果我首先检查列表视图,它会正确显示更新,但是一旦我打开详细视图它丢失了更新并返回到以前的状态。


这是我尝试过的:


来自models.py:


class Order(models.Model):

    status_choices = (

        ('Active', 'Active'),

        ('Completed', 'Completed'),

        ('Late', 'Late'),

        ('Short', 'Short'),

        ('Canceled', 'Canceled'),

        ('Submitted', 'Submitted')

    )

    delivery_status_choices = (

        ('Accepted', 'Accepted'),

        ('Rejected', 'Rejected')

    )

    gig = models.ForeignKey('Gig', on_delete=models.CASCADE)

    seller = models.ForeignKey(User, on_delete=models.CASCADE, related_name='selling')

    buyer = models.ForeignKey(User, on_delete=models.CASCADE, related_name='buying')

    created_at = models.DateTimeField(auto_now=timezone.now())

    charge_id = models.CharField(max_length=234)

    days = models.IntegerField(blank=False)

    status = models.CharField(max_length=255, choices=status_choices)

    delivery = models.FileField(upload_to=content_file_name, blank=True)


    def __str__(self):

        return f'{self.buyer} order from {self.seller}'

来自template.html:


{%  if not order.status == 'Completed' and not order.status == 'Submitted' and not order.status == 'Canceled' %}

     {% if order.buyer.username == user.username %}

           <form method="post" action="{% url 'order-cancel' %}">

                 {% csrf_token %}

                 <input type="text" name="id" value="{{ order.id }}" hidden />

                 <button type="submit" class="btn btn-primary align-content-center">Cancel the Order</button>

             </form>

      {% elif order.status == 'Canceled' %}

             <p> Your order has been canceled already!</p>

      {% endif %}

    {% else %}

        <p> You can't cancel this order now.</p>

 {% endif %}


我正在尝试更新statusof order,当我提交order-cancel查看请求时,首先将其另存status为已取消,当我加载detailed(单个对象页面)时,它将更新status后的 o恢复一次。


这里有什么问题?


DIEA
浏览 126回答 1
1回答

倚天杖

您将使用merge()函数,然后最终提交会话。db.session.merge(order)db.session.commit()绕过它的两种方法。解决方案 2 永远是我的最佳选择1.) 使用对象查询获取方法@login_required()def order_cancel(request):&nbsp; &nbsp; if request.method == 'POST':&nbsp; &nbsp; &nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; order_id = request.POST.get('id')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(order_id)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; order = Order.objects.get(id=order_id)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; charge_id = order.charge_id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; refund = stripe.Refund.create(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; charge=charge_id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; order.status = 'Canceled'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db.session.merge(order)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db.session.commit()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(order.status)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('success')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return HttpResponseRedirect(reverse_lazy('buying'))&nbsp; &nbsp; &nbsp; &nbsp; except Order.DoesNotExist:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return redirect('home')2.) 使用对象查询 filter_by 方法@login_required()def order_cancel(request):&nbsp; &nbsp; if request.method == 'POST':&nbsp; &nbsp; &nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; order_id = request.POST.get('id')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(order_id)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; order = Order.query.filter_by(id=order_id).first()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; charge_id = order.charge_id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; refund = stripe.Refund.create(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; charge=charge_id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; order.status = 'Canceled'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db.session.merge(order)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db.session.commit()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(order.status)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('success')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return HttpResponseRedirect(reverse_lazy('buying'))&nbsp; &nbsp; &nbsp; &nbsp; except Order.DoesNotExist:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return redirect('home')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python