0
在一個簡單的形式,我有以下類別:如何從Django中的模型創建表單字段?
class Upload(models.Model):
name = models.CharField()
groupy = models.ForeignKey('Group',null=True)
class Group(models.Model):
slogan = models.CharField()
我現在想創建它同時創建這兩個類的對象的形式。我的方法到目前爲止是:
class GroupCreate(CreateView):
def get_form(self, form_class):
form = super(GroupCreate, self).get_form(form_class)
form.fields['name'] = CharField()
return form
def form_valid(self, form):
response = super(GroupCreate, self).form_valid(form)
groupy = form.save()
upload = Upload()
upload.groupy = groupy
upload.name = form.instance.name
upload.save()
return response
...
這工作正常。然而,我的問題是,我有很多領域,我需要分別設置每個領域的選項。因此:
我怎麼可以轉換上傳模型的領域爲表領域,只是他們管進入表?