表格

时间:2015-08-13 23:38:06

标签: python django forms field

我的表格完全正常 models.py:

class Location(models.Model):
    title = models.CharField(max_length=300)
    description = models.TextField(null=True, blank=True)
    address = models.TextField(null=True, blank=True)

class Review (models.Model):
    location = models.ForeignKey(Location)
    description = models.TextField(null=True, blank=True) 

views.py:

class Create(CreateView):
  model = coremodels.Review
  template_name = 'location/test.html'
  fields = '__all__'
  def form_valid(self, form):
    form.save()
    return HttpResponseRedirect('')

    return super(Create, self).form_valid(form)

HTML:

<form action="" method="post">{% csrf_token %}
    {{ form}} 
    <input type="submit" value="Create" />
</form>

当我打开网站时,我可以选择一个位置并对创建按钮进行审核。但是,现在我依赖于Location类中的预填充值。如果我希望用户可以直接创建说明以及位置标题(我不希望标题位于class Review),我已经尝试在文档中查找此内容但是无法用找不到任何东西。在某处我读到我可以创建两种不同的形式来处理不同的事情,但我不确定如何在class Create中合并所有这些。是否有类似model = coremodels.Review & coremodels.Location的内容,然后在html中我能做到

{{form.title}}
{{form.description}}  

我可以查找任何想法或搜索字词吗?

谢谢!

修改 好的,感谢Ruddra和this post,这是一个有效的解决方案。为了让它适合我,我必须编辑一点,

class SomeForm(forms.ModelForm):

   def __init__(self, *args, **kwargs):
      super(SomeForm, self).__init__(*args, **kwargs)
      self.fields['title'] = forms.CharField(label='Title', required = False)
      self.fields['description'] = forms.CharField(label='Description', required = False)
      self.fields['location'] = forms.ModelChoiceField(queryset= Location.objects.all(), required = False) # This line is for making location not required in form field for input

   class Meta:
        model = Review
        fields = '__all__'


   def save(self, commit=True):
       """
       It will save location from choice field or inputs of title and description
       """
       instance = super(SomeForm, self).save(commit=False)
       if instance.location_id:
           instance.save()
       else:
           new_location = Location.objects.create(title=self.cleaned_data['title'])
           instance.location = new_location
           instance.save()
      return instance

和views.py

class Create(CreateView):
  model = coremodels.Review
  template_name = 'location/test.html'
  form_class = SomeForm

1 个答案:

答案 0 :(得分:1)

不幸的是你不能使用这样的两个模型,你必须写一个表格然后在那里做些什么。例如:

class SomeForm(forms.ModelForm):

   def __init__(self, *args, **kwargs):
      super().__init__(*args, **kwargs)
      self.fields['title'] = forms.CharField(label='Title', required = False)
      self.fields['description'] = forms.CharField(label='Description', required = False)
      self.fields['location'] = forms.ModelChoiceField(queryset= Location.objects.all(), required = False) # This line is for making location not required in form field for input

   class Meta:
        model = Review
        fields = '__all__'


   def save(self, commit=True):
       """
       It will save location from choice field or inputs of title and description
       """
       instance = super().save(commit=False)
       if instance.location:
           instance.save()
       else:
           new_location = Location.objects.create(title=self.cleaned_data['title'], description = self.cleaned_data['description']])
           instance.location = new_location
           instance.save()
      return instance

在视图中使用它:

class Create(CreateView):
  model = coremodels.Review
  template_name = 'location/test.html'
  form = SomeForm

并且您确保位置可以为空或在表单中不需要(我已将其添加到示例中)