开发者

Django's Inline Forms

I am trying to create records using an inline formSet while at the same time creating a record with a normal form whose primary key is the foreign key for the inline formSet all on the same HTML page.

Make sense? Here's what I mean: Suppose I have the following two models (not real co开发者_运维技巧de, obviously, but you get the idea):

Class mainModel
    Primary Key (custom pk I create)
    field1
    field2

Class inlineFormModel
    autoPK
    field1 = ForeignKey(mainModel)
    field2

Now, I want to create a single HTML page for the user so they can create a mainModel instance at the same time as creating a number of inlineFormModel instances. The mainModel would be a normal form while the inlineFormModel would be using inlineFormsets. The problem is that when I save all the forms, there is no foreignKey to link to the inline formSet records since the model it refers to is still be created (everything gets saved in the same view). Does that make sense?

How would I go about creating a new mainModel instance with several secondModel instances and save the whole batch all with the same view function?

Thanks!


This is a common scenario, I do not know why is not addressed in the docs:

    initial_form = mainModelForm(request.POST)
    if initial_form.is_valid():
        form= initial_form.save(commit=False)
        my_formset = inline_formset(request.POST,instance=form)
        if my_formset.is_valid():
           form.save()
           my_formset.save()
.......  .........
# return codes here
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜