Form with 2 models

6 views
Skip to first unread message

Paddy Joy

unread,
Oct 29, 2008, 10:06:25 AM10/29/08
to Django users
I have an Account model:

class Account(models.Model):

accountname = models.CharField(max_length=50, unique=True)
owner = models.ForeignKey(User)
admins = models.ManyToManyField(User, related_name='admins',
blank=True)
accounts = models.ManyToManyField(User,
related_name='accounts', blank=True)
def __str__(self):
return self.name

I want to create a single form that creates an account and an
owner(contrib.auth.User) at the same time.

Is there an easy way of doing this or do I need to create a custom
form that contains the fields of both models and then separate the
data in my view?

Paddy

Rajesh Dhawan

unread,
Oct 29, 2008, 3:19:35 PM10/29/08
to Django users
Hi Paddy,
Django forms support normal OOP extension. So you can simply subclass
the built-in user creation form (see
django.contrib.auth.forms.UserCreationForm) and add your relevant
account model fields to it. You could then extend the save() method of
the UserCreationForm to first call the base save method followed by
your own save for the Account model.

-RD

Masklinn

unread,
Oct 29, 2008, 3:54:42 PM10/29/08
to django...@googlegroups.com
On 29 Oct 2008, at 15:06 , Paddy Joy wrote:
> Is there an easy way of doing this or do I need to create a custom
> form that contains the fields of both models and then separate the
> data in my view?
>
> Paddy

What's wrong with creating two different Forms (one for the account,
one for the user) and displaying them together in the template? Django
has no problem with that (it's in fact one of Django's strengths,
being able to mix and match several Forms in a single view&template).

Steve Holden

unread,
Oct 29, 2008, 11:07:31 PM10/29/08
to django...@googlegroups.com
Are you sure that both forms' save() methods will be called on a submit?

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Paddy Joy

unread,
Oct 30, 2008, 1:21:17 AM10/30/08
to Django users
Thanks I didn't know I could do that. For some reason I was fixated on
the concept of one form/one view/one template.

I tried as you suggested and it turned out to be pretty straight
forward.

view.py:
@login_required
def CreateAccount(request):

if request.method == "POST":
AccountForm = NewAccountForm(request.POST)
UserForm = CreateUserForm(request.POST)

if UserForm.is_valid() & AccountForm.is_valid():
newuser = UserForm.save()

newaccount = AccountForm.save(commit=False)
newaccount.owner = newuser
newaccount.save()

return HttpResponseRedirect("/account/")

else:
AccountForm = NewAccountForm()
UserForm = CreateUserForm()


return render_to_response('test.html', {"form1": AccountForm,
"form2": UserForm, }, context_instance=RequestContext(request))

test.html:
{% extends "base.html" %}

{% block content %}

<form method="POST" action="">
<table>
{{ form1.as_table }}
{{ form2.as_table }}
</table>
<input type="submit" value="Submit" />
</form>

{% endblock %}


Paddy
Reply all
Reply to author
Forward
0 new messages