无法分配必须是一个实例。 Django的

时间:2018-01-22 04:58:42

标签: python django

我的下面提到的代码出现以下错误:

  

无法指定“'1”:“OrderInfo.ordertype”必须是“订单”   实例

我知道这意味着String必须是instance(可能是模型对象)

但是,我不知道为什么不允许String

这是我的代码。

Models.py

class OrderInfo(models.Model):
    post = models.OneToOneField(Post)
    ordertype = models.ForeignKey(Orders)
    company = models.CharField(max_length=50, blank=True, null=True)
    position = models.CharField(max_length=50, blank=True, null=True)

class Post(models.Model):
    board = models.ForeignKey(Board)
    writer = models.CharField(max_length=50, blank=True, null=True)
    writtendate = models.DateTimeField(blank=True, null=True)
    editeddate = models.DateTimeField(blank=True, null=True)
    subject = models.CharField(max_length=50, blank=True, null=True)
    text = RichTextField()
    hits = models.IntegerField(default=0)

class Orders(models.Model):
    order = models.CharField(max_length=50, blank=True, null=True)

Forms.py

class OrderEditForm(forms.ModelForm):
    qs = Orders.objects.all()
    list = [(order.id, order.order) for order in qs]

    ##### list's value is [('1', 'A'), ('2', 'B')]

    ordertype = forms.CharField(widget=forms.RadioSelect(choices=list))


    class Meta:
        model = OrderInfo
        fields = ('ordertype', 'company', 'position')

class PostForm(forms.ModelForm):

    class Meta:
        model = Post
        fields = ('subject', 'text')

    def __init__(self, *args, **kwargs):
        super(PostForm, self).__init__(*args, **kwargs)
        self.fields['subject'].required = True

views.py

def order_post_edit(request, board_id, post_id):    
    post = get_object_or_404(Post, id=post_id)
    if request.method == "POST":
        form = PostForm(request.POST, instance=post)
        orderform = OrderEditForm(request.POST, instance=post.orderinfo)

    if form.is_valid():
        print(request.POST['ordertype'])
        print(orderform.fields)
        post = form.save(commit=False)
        post.editeddate = timezone.now()
        post.save()
        orderform.save()

        return redirect('order_post', board_id=board_id, post_id=post.id)
else:
    form = PostForm(instance=post)
    orderform = OrderEditForm(instance=post.orderinfo)
return render(request, 'board/order/order_post_edit.html', {'form': form, 'orderform': orderform})

2 个答案:

答案 0 :(得分:0)

您的问题在这里:

ordertype = forms.CharField(widget=forms.RadioSelect(choices=list))

保存对象时,尝试保存为ordertype选择的字符,而不是该id的OrderType对象实例。 你可以尝试:

if form.is_valid():
        print(request.POST['ordertype'])
        print(orderform.fields)
        post = form.save(commit=False)
        post.editeddate = timezone.now()
        post.save()
        orderinfo = orderform.save(commit=False)
        orderinfo.ordertype = Orders.objects.get(pk=orderinfo.ordertype)
        orderinfo.save()

可能有更好的方法将数据放到表单上,以便在选中时将对象实例返回给您而不是id的字符串,但这是一个快速的解决方案。

希望你得到解决

答案 1 :(得分:0)

您必须使用ModelChoiceField

forms.py

class OrderEditForm(forms.ModelForm):
  queryset = Orders.objects.all()
  ordertype = forms.ModelChoiceField(queryset=queryset, widget=forms.RadioSelect())

  class Meta:
    model = OrderInfo
    fields = ('ordertype', 'company', 'position')

如果您使用Charfield,则经过验证的值将返回一个字符串。但是,django期待一个例子。如果我们使用ModelChoiceField,那么它将返回一个实例。所以,我们不会得到错误 Otherway:

如果您仍想使用CharField,请将字段名称更改为ordertype_id