Hello People,
I'm just getting started with Django and am currently trying to get my
head around the User/Authenticating-Backend. I want to save extra data
to the User Model so i followed the inheritance way described here:
http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/
Things are pretty well working, I'm generating a sign-up-form but i'm
not able to save the inherited model to the database. I'm using Django
1.0 and mysql.
This is the set-up:
I'm creating a model that is inheriting from the User Model
from django.db import models
from django.contrib.auth.models import User, UserManager
class CustomUserData(User):
# New Extra Data being saved with every User
city = models.CharField(max_length = 255)
[...]
# Use UserManager to get the create_user method, etc.
objects = UserManager()
Now I'm doing the registration process as described in the "Practical
Django Projects"-Book by James Bennett
from django.http import HttpResponseRedirect
from django import forms
from django.forms import ModelForm
from django.shortcuts import render_to_response
from benutzerextradata.models import CustomUserData
class RegistrationForm(ModelForm):
class Meta:
model = CustomUserData
exclude = ['username', 'date_joined','last_login',
'is_staff','is_active', 'is_superuser', 'groups', 'user_permissions']
def clean(self):
# do some stuff with the data
return self.cleaned_data
def save(self):
new_user = CustomUserData.objects.create_user(
username=self.cleaned_data['username'],
email=self.cleaned_data['email'],
password=self.cleaned_data['password'])
# I'm lazy so i want all the extra data being added to
the object
automatically
# except the ones that are already saved at creating
del self.cleaned_data['username']
del self.cleaned_data['email']
del self.cleaned_data['password']
for key in self.cleaned_data:
setattr(new_user, key,
self.cleaned_data[key])
new_user.save()
def register(request):
if request.method == 'POST':
form = RegistrationForm(data=request.POST)
if form.is_valid():
new_user = form.save()
return HttpResponseRedirect('sign_up_success')
else:
form = RegistrationForm()
return render_to_response('register_form.html', { 'form': form,
'add': True })
So I'm having a form, providing all the neccessary data, pass it to
clean which returns a perfect cleaned_data dictionary when valid. but
when it comes to save the stuff, i get a mysql error saying "(1048,
"Column 'city' cannot be null")" which makes sense with from debug sql-
query "'INSERT INTO `customuserdata_customuserdata` (`user_ptr_id`,
`city`) VALUES (%s)'"
So I see, that I have to get my extra user data somehow into this sql-
query via the create_user method. I thought of overriding the
create_user method in the CustomUserData or saving a normal user first
and inserting my CustomUserData into the Database by hand, but both
ways aren't the nicest to use i think.
So whats the best way to do this? Help is appreciated a lot. I'm
looking forward to your answers.
Greetings from Berlin/Germany
Johan UhIe
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---