Re: Integrity Error: column username is not unique
Hi,
username field in django.contrib.auth.User model is unique.
Probably, the username you are trying to save already exists. In your view, you might want to check,
try:
username = User.objects.get(username=form.cleaned_data['username'])
except ObjectDoesNotExist:
#create user new object here
-Dhivya
On Sunday, June 24, 2012 9:36:48 AM UTC-4, Nikhil Verma wrote:
Hi
I got this error when i was making a post after filling the details from the user.
Can somebody throw a light in this where i am going wrong.
models.py
class UserProfile(models.Model):
user = models.ForeignKey(User, blank=True, null=True, unique=True)
first_name = models.CharField(max_length=30, blank=True)
last_name = models.CharField(max_length=30, blank=True)
gender = models.CharField(max_length=1, choices=GENDER_CHOICES, blank=True)
birth_date = models.DateField(auto_now_add=True)
street = models.CharField(max_length=75, blank=True)
state = models.CharField(max_length=30, blank=True)
zip_code = models.IntegerField(max_length=7, blank=True, null=True)
country = models.CharField(max_length=30, blank=True)
mobile = models.CharField(max_length=15, blank=True)
home_phone = models.CharField(max_length=15, blank=True)
primary_email = models.EmailField(max_length=60, blank=True)
institution_name = models.CharField(max_length=100,blank=True,null=True)
institution_website = models.CharField(max_length=100,blank=True,null=True)
forms.py
class UserProfileForm(forms.Form):
username = forms.CharField(label='Username', max_length=20)
first_name = forms.CharField(label="First Name", max_length=40, required=True)
last_name = forms.CharField(label="Last Name", max_length=40, required=True)
gender = forms.CharField(max_length=1, widget=Select(choices=GENDER_CHOICES), required=True)
date_of_birth = CustomDateField(label='Date of Birth', required=False)
telephone = forms.CharField(label='Mobile Phone', max_length=40, required=False)
institution_name = forms.CharField(label='Institute Name', max_length=80, required=False)
street = forms.CharField(label='Street', max_length=100, required=False)
zip_code = forms.CharField(label='Zip Code', max_length=40, required=False)
state = forms.CharField(label='State', max_length=80, required=False)
country = forms.CharField(label='Country', max_length=80, required=False)
institue_name = forms.CharField(label='Institute Name', max_length=80, required=False)
institue_website = forms.URLField(label='Institute Website', max_length=80, required=False)
views.py
def createprofile(request):
"""
Creating Profile
"""
if request.method == "POST":
form = UserProfileForm(request.POST)
if form.is_valid():
user = User(
username = form.cleaned_data['username'],
first_name = form.cleaned_data['first_name'],
last_name = form.cleaned_data['last_name'],
)
user.save()
userprofile_obj = UserProfile(
user = user.username,
first_name = user.first_name,
last_name = user.last_name,
gender = form.cleaned_data['gender'],
birth_date = form.cleaned_data['date_of_birth'],
mobile = form.cleaned_data['telephone'],
institution_name = form.cleaned_data['institution_name'],
street = form.cleaned_data['street'],
zip_code = form.cleaned_data['zip_code'],
state = form.cleaned_data['state'],
country = form.cleaned_data['country'],
)
userprofile_obj.save()
Any help how can i solve this problem ?
--
Regards
Nikhil Verma
+91-958-273-3156
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/QlDJxHaSvdsJ.
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.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home