Django: saving multiple model models at the same time (hard case)

Well, that might be a simple case, but it's hard for me to figure it out.

I have two user registration sequences (corporate and all others). When a corporate user creates a user instance through my registration form, I would also like them to enter secondary forms that create linked instances (based on the Website and Organization_Contact models).

I know how to solve this problem by completing additional synchronous or asynchronous requests, but I would like the user to fill out three forms and submit with one click.

My problem is that the other two forms rely on the user's foreign key as a field. I created this field "null = True, blank = True" so that I can check and save forms without a foreign key, but in the end I want to add this key to both instances of the model.

I thought that I could check three forms, save UserForm, and then use a model query to return a new User.id (all in one view). Then I would add this user_id value to two other form dictionaries (WebsiteForm and Organization_ContactForm).

This will work like this:

def register_company(request):
    if request.method=='POST'
       uform=UserCreationForm(request.POST, prefix='user')
       sform=SiteForm(request.POST, prefix='site')
       oform=OrgForm(request.POST, prefix='site')
       if uform.is_valid() and sform.is_valid() and oform.is_valid():
            uform.save()
            user=User.objects.get(email__iexact=uform.cleaned_data['email'])
            uid=unicode(user.id)
       #now I'd add uid back into the SiteForm and Orgform dictionaries and then save both instances              

Problems: 1) Not sure if I can save the model form and then return the model instance as a set of queries in one view

2) I say that I am not sure, because I could not pass the problem of trying to pass the variable to the set of queries.

get, , . , , .

, ( ), ( ), , ,

, , , .

. .

+1
2

ModelForm s?

if request.method=='POST'
   uform=UserCreationForm(request.POST, prefix='user')
   sform=SiteForm(request.POST, prefix='site')
   oform=OrgForm(request.POST, prefix='site')
   if uform.is_valid() and sform.is_valid() and oform.is_valid():
        # save returns the object 
        user = uform.save()

        # commit = false doesn't save to DB.
        site = sform.save(commit=False)
        site.user = user
        site.save()

        org = oform.save(commit=False)
        org.user = user
        org.save()

: , (, ), ?

, . .

+2

:

def register_company(request):
    uform=UserCreationForm(request.POST or None, prefix='user')
    sform=SiteForm(request.POST or None, prefix='site')
    oform=OrgForm(request.POST or None, prefix='site')

    if request.POST and all((uform.is_valid(), sform.is_valid(), oform.is_valid()):
        user = uform.save()
        sform.save(user)
        oform.save(user)

    ruturn ...

class UserCreateionForm(ModelForm):
    Meta:
        model = User

class SiteForm(ModelForm):
    Meta:
        model=Site
        exclude=['user', ]

    def save(self, user, commit=True):
        self.instance.user = user
        return super(SiteForm, self).save(commit=commit)
0

All Articles