【问题标题】:Django form not saving with ModelChoiceField - ForeignKeyDjango 表单不使用 ModelChoiceField 保存 - ForeignKey
【发布时间】:2016-07-07 03:27:06
【问题描述】:

我的网站上有多个可以工作并将信息保存到我的 PostgreSQL 数据库的表单。 我正在尝试创建一个表单来保存我的集合模型的信息:

class Set(models.Model):
    settitle = models.CharField("Title", max_length=50)
    setdescrip = models.CharField("Description", max_length=50)
    action = models.ForeignKey(Action)
    actorder = models.IntegerField("Order number")

设置表单如下所示。我正在使用 ModelChoiceField 从动作模型中提取动作名称字段列表,这在表单上显示为选择下拉列表

class SetForm(ModelForm):

    class Meta:
        model = Set
        fields = ['settitle', 'setdescrip', 'action', 'actorder']
    action = forms.ModelChoiceField(queryset = Action.objects.values_list('name', flat=True), to_field_name="id")

createset 的视图如下:

def createset(request):
    if not request.user.is_authenticated():
        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
    elif request.method == "GET":
        #create the object - Setform 
        form = SetForm;
        #pass into it 
        return render(request,'app/createForm.html', { 'form':form })
    elif "cancel" in request.POST:
        return HttpResponseRedirect('/actions')
    elif request.method == "POST":
    # take all of the user data entered to create a new set instance in the table
        form = SetForm(request.POST, request.FILES)
        if  form.is_valid():
            form.save()
            return HttpResponseRedirect('/actions')
        else:
            form = SetForm()
            return render(request,'app/createForm.html', {'form':form})

当表单被填写并有效并按下保存时,没有任何反应。没有错误,页面只是刷新为新表单。 如果我没有使用 (action = forms.ModelChoiceField(queryset = Action.objects.values_list('name', flat=True), to_field_name="id")) 在 forms.py 中设置操作字段,那么数据会保存,所以这很可能是我做错了什么。只是不确定是什么?

【问题讨论】:

    标签: django django-models django-forms save foreign-keys


    【解决方案1】:

    https://docs.djangoproject.com/en/stable/ref/forms/fields/#django.forms.ModelChoiceField.queryset

    queryset 属性应该是一个查询集。 values_list 返回一个列表。

    您应该只定义 Action 模型的 __str__ 方法,而不必重新定义表单中的 action 字段。

    如果已设置并且您想使用另一个标签,您可以继承 ModelChoiceField。

    将调用模型的__str__(Python 2 上的__unicode__)方法来生成对象的字符串表示形式,以用于字段的选择;提供自定义表示,子类ModelChoiceField 并覆盖label_from_instance。此方法将接收模型对象,并应返回适合表示它的字符串。例如:

    from django.forms import ModelChoiceField
    
    class MyModelChoiceField(ModelChoiceField):
        def label_from_instance(self, obj):
            return "My Object #%i" % obj.id
    

    因此,在您的情况下,设置Action 模型的__str__ 方法,并删除表单中的action = forms.ModelChoiceField(...) 行:

    class Action(models.Model):
        def __str__(self):
            return self.name
    
    class SetForm(ModelForm):
    
        class Meta:
            model = Set
            fields = ['settitle', 'setdescrip', 'action', 'actorder']
    

    或者定义一个自定义的 ModelChoiceField:

    class MyModelChoiceField(forms.ModelChoiceField):
        def label_from_instance(self, obj):
            return obj.name
    
    class SetForm(ModelForm):
    
        class Meta:
            model = Set
            fields = ['settitle', 'setdescrip', 'action', 'actorder']
    
        action = MyModelChoiceField(Action.objects.all())
    

    【讨论】:

      猜你喜欢
      • 2010-10-11
      • 1970-01-01
      • 2012-07-23
      • 1970-01-01
      • 1970-01-01
      • 2020-11-26
      • 1970-01-01
      • 2011-08-11
      • 2020-01-14
      相关资源
      最近更新 更多