I m still stuck with
Exception Type: IntegrityError
Exception Value: column user_id is not unique
Whenever I try to save, it throws the above error.
I have attached files for reference, can someone help ??
On Saturday, April 6, 2013 7:07:32 PM UTC+5:30, sachin wrote:
>
> Thanx Shawn,
> *
> *
> UserCreationForm really helped.
>
> Now I have a different problem, I'm trying to add custom feilds to *auth
> user *using
> storing-additional-information-about-users<https://docs.djangoproject.com/en/1.4/topics/auth/#storing-additional-information-about-users>
> .
> But now I m getting this error.
>
> Exception Type:IntegrityErrorException Value:
>
> column user_id is not unique
>
>
>
> This is my *models.py *file:
> *
> *
> from django.db import models
> from django.contrib.auth.models import User
> from django.db.models.signals import post_save
>
> class UserProfile(models.Model):
> user = models.OneToOneField(User)
> sr_no = models.CharField(max_length=10)
>
> def __unicode__(self):
> return self.sr_no
>
> def create_user_profile(sender, instance, created, **kwargs):
> if created:
> UserProfile.objects.create(user=instance)
>
> post_save.connect(create_user_profile, sender=User)
>
> I have added *unique *field, but it does not make any difference. So far
> I haven't got
> any convincing answer.
>
> Any Idea ??
> On Tuesday, April 2, 2013 12:49:52 AM UTC+5:30, Shawn Milochik wrote:
>>
>> Don't even worry about factories. They're for when you want a bunch of
>> forms for the same model on the page at once.
>>
>> Use the UserCreationForm in django.contrib.auth.forms. It only accepts
>> a username and password, so you can either subclass it to add the
>> fields or make your own form and add it to your view so that they both
>> appear in the same HTML form. You can validate both and do what you
>> need to do.
>>
>> You definitely shouldn't be writing validation logic for the password
>> and username and such -- that's what ModelForms are for.
>>
>> If you have more specific questions just ask.
>>
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
class UserProfile(models.Model):
user = models.OneToOneField(User)
serial_num = models.CharField(max_length=10)
def __unicode__(self):
return "%s" % self.user
def create_user_profile(sender, instance, created, **kwargs):
if created == True:
profile, created = UserProfile.objects.get_or_create(user=instance)
post_save.connect(create_user_profile, sender=User)
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django import forms
from models import UserProfile
class Register(UserCreationForm):
"""my form"""
serial_num = forms.CharField(max_length=10)
class Meta:
model = User
fields = ('username','first_name','last_name','email','serial_num','password1','password2',)
def save(self, commit=True):
if not commit:
raise NotImplementedError("Can't create User and UserProfile without database save")
user = super(UserCreationForm, self).save(commit=True)
user.username = self.cleaned_data["username"]
user.first_name = self.cleaned_data["first_name"]
user.last_name = self.cleaned_data["last_name"]
user.email = self.cleaned_data["email"]
user.set_password(self.cleaned_data["password1"])
user.is_active = True
# # if commit:
# user.save()
user_profile = UserProfile(user=user, serial_num=self.cleaned_data['serial_num'])
user_profile.save()
return user, user_profile
from django.shortcuts import render_to_response, HttpResponse
from django.template import RequestContext
from django.contrib.auth.models import User
from models import UserProfile
from forms import Register
def profile(request, pID):
users = User.objects.all()
user_profiles = users.get(id=pID)
user_profile = user_profiles.get_profile()
return render_to_response('profile.html',{'users':users,'user_profile':user_profile},context_instance=RequestContext(request))
# ====================
def register(request):
"""my register"""
if request.method == 'POST':
form = Register(request.POST)
if form.is_valid():
user = form.save()
success="<html>sign_up_success</html>"
return HttpResponse(success)
else:
form = Register()
return render_to_response('sign_up.html', {'form':form}, context_instance=RequestContext(request))
def index(request):
return HttpResponseRedirect('/register')
from django.contrib import admin
from register.models import UserProfile
admin.site.register(UserProfile)