我正在使用django-extra-views,我希望用预先填充的数据填充表单及其关联的内联,然后保存(创建对象)。到目前为止,我有:
from extra_views import CreateWithInlinesView
class ItemCreateView(extra_views.CreateWithInlinesView):
def dispatch(self, request, item_pk, *args, **kwargs):
self.item = models.Item.objects.get(id=item_pk)
return super(ItemCreateView, self).dispatch(
request, *args, **kwargs
)
def get_initial(self):
# Thanks to that method a form is populated with initial data.
return forms.ItemCreateForm(instance=self.item).initial
def construct_inlines(self):
# Thanks to that method a form and its inlines are prepopulated successfully.
ItemFormSet = inlineformset_factory(models.Item, models.ItemEntry, extra=0)
formset = ItemFormSet(instance=self.item)
return [formset]
但是,尽管内联值是通过POST发送的,但是保存的对象没有内联。我究竟做错了什么?我应该初始化表单并以不同方式内联吗?