So I'm rolling out my own registration form that should update the following models:
- User (auth module) - UserProfile Everything seems to be coming together but I'm having a hard time with the save() method for the form. My UserProfile model has a couple of fields that refer to other models and I'm having difficulty setting them up properly. I keep getting an error telling me: Exception Type: IntegrityError at /registration/ Exception Value: auth_user.username may not be NULL I know where the problems are: new_user = User.objects.create_user(username=self.cleaned_data['username'], email=self.cleaned_data['email'], password=self.cleaned_data['password1']) and I'm getting this similar error for defining the user profile on the line below. new_profile.user = User.objects.get(username__exact=self.cleaned_data['username']) I'm just not sure how else to write this stuff. I'm including my form, model and the exact error message that I'm getting below. Any insight that you have is greatly appreciated. Thank you. # registration.forms.py import re from django import forms from django.db import models from django.contrib.localflavor.us.forms import USPhoneNumberField from django.contrib.auth.models import User from ylbbq.areas.models import Area from ylbbq.profiles.models import UserProfile STATES = ( ('WA', 'Washington'), ('AK', 'Alaska'), ('AL', 'Alabama'), ('AR', 'Arkansas'), ('AZ', 'Arizona'), ('CA', 'California'), ('CO', 'Colorado'), ('CT', 'Connecticut'), ('DE', 'Delaware'), ('FL', 'Florida'), ('GA', 'Georgia'), ('HI', 'Hawaii'), ('IA', 'Iowa'), ('ID', 'Idaho'), ('IL', 'Illinois'), ('IN', 'Indiana'), ('KS', 'Kansas'), ('KY', 'Kentucky'), ('LA', 'Louisiana'), ('MA', 'Massachusetts'), ('MD', 'Maryland'), ('ME', 'Maine'), ('MI', 'Michigan'), ('MN', 'Minnesota'), ('MO', 'Missouri'), ('MS', 'Mississippi'), ('MT', 'Montana'), ('NC', 'North Carolina'), ('ND', 'North Dakota'), ('NE', 'Nebraska'), ('NH', 'New Hampshire'), ('NJ', 'New Jersey'), ('NM', 'New Mexico'), ('NV', 'Nevada'), ('NY', 'New York'), ('OH', 'Ohio'), ('OK', 'Oklahoma'), ('OR', 'Oregon'), ('PA', 'Pennsylvania'), ('RI', 'Rhode Island'), ('SC', 'South Carolina'), ('SD', 'South Dakota'), ('TN', 'Tennessee'), ('TX', 'Texas'), ('UT', 'Utah'), ('VT', 'Vermont'), ('VA', 'Virginia'), ('WI', 'Wisconsin'), ('WV', 'West Virginia'), ('WY', 'Wyoming'), ('AB', 'Alberta'), ('BC', 'British Columbia'), ('MB', 'Manitoba'), ('NB', 'New Brunswick'), ('NL', 'Newfoundland and Labrador'), ('NS', 'Nova Scotia'), ('NT', 'Northwest Territories'), ('NU', 'Nunavut'), ('ON', 'Ontario'), ('PE', 'Prince Edward Island'), ('QC', 'Quebec'), ('SK', 'Saskatchewan'), ('YT', 'Yukon'), ) COUNTRIES = ( ('USA', 'United States'), ('Canada', 'Canada') ) POSTAL_CODE_PATTERN = re.compile(r'^\d{5}-\d{4}|\d{5}|[A-Z]\d[A-Z] \d[A-Z]\d$') class RegisterForm(forms.Form): username = forms.CharField(max_length=30, help_text='Create a user name for this Web site.') first_name = forms.CharField(max_length=30) last_name = forms.CharField(max_length=30) password1 = forms.CharField(max_length=60, label='Password', widget=forms.PasswordInput) password2 = forms.CharField(max_length=60, label='Password Confirmation', widget=forms.PasswordInput) email = forms.EmailField(help_text='Enter a valid e-mail address.') phone = USPhoneNumberField(max_length=12, help_text='Enter your phone number in the following format: 253-123-5678.') address = forms.CharField(max_length=70, help_text='Enter mailing address.') city = forms.CharField(max_length=50) state_province = forms.ChoiceField(choices=STATES, label='State or Province') country = forms.ChoiceField(choices=COUNTRIES) zip_code = forms.CharField(max_length=10, label='Mailing Code', help_text='Enter your zip code (US or Canadian).') birth_date = forms.DateField(help_text='Enter your birthdate in the following format: 1979-09-29 (YYYY-MM-DD).') areas = forms.ModelMultipleChoiceField(queryset=Area.objects.all(), label='Preferred Areas', help_text='Select the areas that you\'d like to serve.') def clean_username(self): data = self.cleaned_data['username'] try: User.objects.get(username=data) except User.DoesNotExist: return raise forms.ValidationError('The username "%s" is already taken.' % data) def clean_zip_code(self): data = self.cleaned_data['zip_code'] match_code = POSTAL_CODE_PATTERN.match(data) if match_code is None: raise forms.ValidationError('Enter a valid US or Canadian zip code.') return def clean(self): data = self.cleaned_data # Compare the two passwords for a match pw1 = data.get('password1') pw2 = data.get('password2') if pw1 != pw2: msg = "Passwords do not match" self._errors['password2'] = self.error_class([msg]) return data def save(self): # User new_user = User.objects.create_user(username=self.cleaned_data['username'], email=self.cleaned_data['email'], password=self.cleaned_data['password1']) new_user.first_name = self.cleaned_data['first_name'] new_user.last_name = self.cleaned_data['last_name'] new_user.is_active = True new_user.save() # Profile new_profile = UserProfile.objects.create() new_profile.user = User.objects.get(username__exact=self.cleaned_data['username']) new_profile.phone = self.cleaned_data['phone'] new_profile.address = self.cleaned_data['address'] new_profile.city = self.cleaned_data['city'] new_profile.state_province = self.cleaned_data['state_province'] new_profile.country = self.cleaned_data['country'] new_profile.zip_code = self.cleaned_data['zip_code'] new_profile.birth_date = self.cleaned_data['birth_date'] new_profile.areas = self.cleaned_data['areas'] new_profile.save() return # profiles.models.py from django.db import models from django.contrib.auth.models import User from ylbbq.areas.models import Area STATES = ( ('AK', 'Alaska'), ('AL', 'Alabama'), ('AR', 'Arkansas'), ('AZ', 'Arizona'), ('CA', 'California'), ('CO', 'Colorado'), ('CT', 'Connecticut'), ('DE', 'Delaware'), ('FL', 'Florida'), ('GA', 'Georgia'), ('HI', 'Hawaii'), ('IA', 'Iowa'), ('ID', 'Idaho'), ('IL', 'Illinois'), ('IN', 'Indiana'), ('KS', 'Kansas'), ('KY', 'Kentucky'), ('LA', 'Louisiana'), ('MA', 'Massachusetts'), ('MD', 'Maryland'), ('ME', 'Maine'), ('MI', 'Michigan'), ('MN', 'Minnesota'), ('MO', 'Missouri'), ('MS', 'Mississippi'), ('MT', 'Montana'), ('NC', 'North Carolina'), ('ND', 'North Dakota'), ('NE', 'Nebraska'), ('NH', 'New Hampshire'), ('NJ', 'New Jersey'), ('NM', 'New Mexico'), ('NV', 'Nevada'), ('NY', 'New York'), ('OH', 'Ohio'), ('OK', 'Oklahoma'), ('OR', 'Oregon'), ('PA', 'Pennsylvania'), ('RI', 'Rhode Island'), ('SC', 'South Carolina'), ('SD', 'South Dakota'), ('TN', 'Tennessee'), ('TX', 'Texas'), ('UT', 'Utah'), ('VT', 'Vermont'), ('VA', 'Virginia'), ('WA', 'Washington'), ('WI', 'Wisconsin'), ('WV', 'West Virginia'), ('WY', 'Wyoming'), ('AB', 'Alberta'), ('BC', 'British Columbia'), ('MB', 'Manitoba'), ('NB', 'New Brunswick'), ('NL', 'Newfoundland and Labrador'), ('NS', 'Nova Scotia'), ('NT', 'Northwest Territories'), ('NU', 'Nunavut'), ('ON', 'Ontario'), ('PE', 'Prince Edward Island'), ('QC', 'Quebec'), ('SK', 'Saskatchewan'), ('YT', 'Yukon'), ) COUNTRIES = ( ('USA', 'United States'), ('Canada', 'Canada') ) # Create your models here. class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) phone = models.CharField(max_length=12) address = models.CharField(max_length=70) city = models.CharField(max_length=50) state_province = models.CharField(max_length=2, choices=STATES, verbose_name='State or Province') country = models.CharField(max_length=20, choices=COUNTRIES) zip_code = models.CharField(max_length=7) birth_date = models.DateField() areas = models.ManyToManyField(Area) def __unicode__(self): return self.user.username ERROR MESSAGE: Environment: Request Method: POST Request URL: http://localhost:8000/registration/ Django Version: 1.2.1 Python Version: 2.6.1 Installed Applications: ['django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.admin', 'django.contrib.admindocs', 'ylbbq.areas', 'ylbbq.profiles', 'ylbbq.shifts'] Installed Middleware: ('django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware') Traceback: File "/Library/Python/2.6/site-packages/django/core/handlers/base.py" in get_response 100. response = callback(request, *callback_args, **callback_kwargs) File "/Users/bealtr/Personal/YLBBQ/Django Projects/ylbbq/../ylbbq/ register/views.py" in register 10. user = form.save() File "/Users/bealtr/Personal/YLBBQ/Django Projects/ylbbq/../ylbbq/ register/forms.py" in save 129. new_user = User.objects.create_user(username=self.cleaned_data['username'], email=self.cleaned_data['email'], password=self.cleaned_data['password1']) File "/Library/Python/2.6/site-packages/django/contrib/auth/models.py" in create_user 129. user.save(using=self._db) File "/Library/Python/2.6/site-packages/django/db/models/base.py" in save 435. self.save_base(using=using, force_insert=force_insert, force_update=force_update) File "/Library/Python/2.6/site-packages/django/db/models/base.py" in save_base 528. result = manager._insert(values, return_id=update_pk, using=using) File "/Library/Python/2.6/site-packages/django/db/models/manager.py" in _insert 195. return insert_query(self.model, values, **kwargs) File "/Library/Python/2.6/site-packages/django/db/models/query.py" in insert_query 1479. return query.get_compiler(using=using).execute_sql(return_id) File "/Library/Python/2.6/site-packages/django/db/models/sql/ compiler.py" in execute_sql 783. cursor = super(SQLInsertCompiler, self).execute_sql(None) File "/Library/Python/2.6/site-packages/django/db/models/sql/ compiler.py" in execute_sql 727. cursor.execute(sql, params) File "/Library/Python/2.6/site-packages/django/db/backends/util.py" in execute 15. return self.cursor.execute(sql, params) File "/Library/Python/2.6/site-packages/django/db/backends/sqlite3/ base.py" in execute 200. return Database.Cursor.execute(self, query, params) Exception Type: IntegrityError at /registration/ Exception Value: auth_user.username may not be NULL -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.