3
MultiModelForm
isn’t part of Django and you didn’t post a link to whatever project provides this class, but obviously here:
user = form['user']
user
is the UserForm
instance, NOT the User
model instance. What you want is something like:
# good naming is key to readable code...
user_form = form['user']
profile_form = form['profile']
if user_type == 'Parent' or user_type == 'GC':
c_id = generate_cid()
user = user_form.save()
profile = profile_form.save(commit=False)
profile.user = user
profile.customer_id = c_id
profile.save()
return HttpResponseRedirect(reverse_lazy('users:login'))
Also note that I removed your try/except clause which was worse than useless – in your dev environment you want to let Django catch those errors and render the much more useful debug page (which has the full traceback etc), and on production you want to let Django catch those errors and return a 500 response – both thing Django does by default if you don’t interfere. As a general rule, if you cannot effectively handle an exception, let it propagate (and no, returning a 200 response when the request actually failed – and possibly leaking some internal informations – doesn’t qualify as “effective handling”).
2
The below worked for me:
$ python manage.py shell
...
...
>>> from django.contrib.auth.models import User
>>> user = User.objects.create_user("test", password="test")
>>> user
<User: test>
>>> user.id
3
>>>
- [Django]-Proper implementation of Django model inheritance architecture?
- [Django]-How to secure files upload using django