使用来自不同模型模板的kwarg在Django中创建CreateView

时间:2018-07-24 09:34:09

标签: django django-models django-forms django-templates django-views

我无法使用CreateView创建对象SELECT DIS.iDataItemID, DIS.iDataItemCurrentStatusID, COUNT(*) , DI.iDataTypeID, DI.iEmployeeID, DI.iEmployerID FROM DataItem DI INNER JOIN DataItemStatus DIS ON DI.iDataItemID = DIS.iDataItemID Where iDataItemCurrentStatusID = 1 GROUP BY DIS.iDataItemID, DIS.iDataItemCurrentStatusID, DI.iDataTypeID, DI.iEmployeeID, DI.iEmployerID ,我不确定自己做错了什么。下面是一个简短的介绍

简介:“我的订单历史记录”页面上,用户可以看到他/她购买的所有商品。在“订单历史记录”页面中,我有一个按钮,可让买方为卖方留下评论。下面是按钮

review

从这里我获得商品item.id和卖家用户名

<a href="{% url 'accounts:review' username=item.made_by pk=item.pk %}">
   <button class="text-success">Leave Review</button>
</a> 

当我单击留下评论时,该URL在地址栏中正确表达。然后显示一个表格。我在其中输入{% for item in order.items_in_this_order.all %} <!--models below --> url(r'^(?P<username>[-\w]+)/(?P<pk>\d+)/review/$', views.ReviewCreate.as_view(), name='review'), {% endfor %} feedbackratingsfeedback_image的其他字段应该在views.py中进行。在我填写表格并点击提交后。网址仍然正确。但出现以下错误

feedback_video

下面是 views.py ,它位于帐户应用内

IntegrityError at /accounts/nikhil/10/review/
NOT NULL constraint failed: accounts_review.item_id
Request Method: POST
Request URL:    http://127.0.0.1:8000/accounts/nikhil/10/review/
Django Version: 1.11
Exception Type: IntegrityError
Exception Value:    
NOT NULL constraint failed: accounts_review.item_id

下面是审阅模型

models.py
class ReviewCreate(LoginRequiredMixin, CreateView):
    model = Review
    form_class = ReviewCreateForm

    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.review_from = self.request.user
        print(self.object.review_from) #This prints
        self.item = OrderItem.objects.get(pk=self.kwargs.get('pk'))
        print(self.item) #This prints
        self.object.review_for = User.objects.get(username=self.kwargs.get('username'))
        print(self.object.review_for) #This prints
        self.object.save()
        return super().form_valid(form)

以下是 OrderItem models.py ,以防万一

class Review (models.Model):
    review_from = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, related_name='review_from')
    review_for = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, related_name='review_for')
    item = models.ForeignKey(OrderItem, related_name='items')
    created = models.DateTimeField(auto_now_add=True)
    feedback = models.TextField(blank=True, null=True)
    feedback_image = models.ImageField(blank=True, null=True)
    feedback_video = models.FileField(blank=True, null=True)
    rating_choices = (
        ('1', 'One'), ('2', 'Two'), ('3', 'Three'), ('4', 'Four'), ('5', 'Five'),
        ('6', 'Six'), ('7', 'Seven'), ('8', 'Eight'), ('9', 'Nine'), ('10', 'Ten')
    )
    ratings = models.CharField(max_length=2, choices=rating_choices)

    def __str__(self):
        return 'Review from {} to {} for {}'.format(self.review_from, self.review_for, self.item.product)

1 个答案:

答案 0 :(得分:2)

您的form_valid方法很奇怪。除其他外,您可以将项目设置为self.item,该项目在任何地方都不会使用,当然也不会在创建的实例上进行设置。此外,您可以直接在此处创建并保存对象,然后调用超类方法,无需添加任何内容即可再次执行该操作。

相反,您应该在form.instance上设置所有这些属性,然后让super方法进行保存:

def form_valid(self, form):
    form.instance.review_from = self.request.user
    form.instance.item = OrderItem.objects.get(pk=self.kwargs.get('pk'))
    form.instance.review_for = User.objects.get(username=self.kwargs.get('username'))
    return super().form_valid(form)