Monday, February 28, 2011

multi model forms

hi guys
I am new to django.

I have got two model User(Django built in ) and a model customer, user
is foreign key in customer

class Customer(models.Model):
user = models.ForeignKey(User, related_name='customers')
street = models.CharField(max_length=200)
city = models.CharField(max_length=100)
postal_code = models.IntegerField()
country = models.CharField(max_length=70)

i have written a view as below

def userForm(request):

if request.method == 'POST':
userform = UserForm(request.POST)
customerform = CustomerForm(request.POST)
if userform.is_valid() and customerform.is_valid():

u = userform.save()
customer = Customer()
customer.user = u
customerform.save()
return HttpResponseRedirect('/webshop/')

else:
userform = UserForm()
customerform = CustomerForm()
context = RequestContext(request, {'userform':userform,
'customerform':customerform,})
return render_to_response('userRegister.html', context)

but i get an erro saying xception Type: IntegrityError
Exception Value:

mainsiteapp_customer.lastname may not be NULL

please if anyonw can help
thanks

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment