30๐
Itโs not strange. You already have a profile for that user, so adding another one breaks the unique constraint. You need to edit the existing one, not add a new one.
Also note that youโre not using the cleaned form data when you save, which you should be. Either use form.cleaned_data['bio']
etc, or even better just do form.save()
which is the whole point of using a model form.
Putting that together:
try:
profile = request.user.userprofile
except UserProfile.DoesNotExist:
profile = UserProfile(user=request.user)
if request.method == 'POST':
form = UserProfileForm(request.POST, instance=profile)
if form.is_valid():
form.save()
return redirect...
else:
form = UserProfileForm(instance=profile)
return render...
9๐
I was getting the same error multiple times and I was really disheartened but finally came over the solution.
Convert:
user = models.OneToOneField(User)
to
user = models.ForeignKey(User)
This should solve the issue.
- [Django]-Handling single page application url and django url
- [Django]-Django ImageField change file name on upload
- [Django]-Cannot resolve 'django.utils.log.NullHandler' in Django 1.9+
2๐
Consider the case: If the same user makes the post request again on URL โprofile/editโ then your code will try to create the new UserProfile using the same user instance, but since this is a one to one field and you have already created one profile using that user hence it will throw integrity error.
So you should check first if the profile associated with that user already exists or not, then if it didnโt exist then create it.
- [Django]-Best way to get query string from a URL in python?
- [Django]-How to run gunicorn from a folder that is not the django project folder
- [Django]-Django 1.9.2 AssertionError: database connection isn't set to UTC
- [Django]-Difference between Django Form 'initial' and 'bound data'?
- [Django]-Django forms: "This field is required" when file POSTed to file field
- [Django]-Django admin โ make all fields readonly
0๐
I accidentally got this error because I forgot to specify update_fields
in my call to save
. I had:
modified_instance.save(video_attr)
When I should have had:
modified_instance.save(update_fields=[video_attr])
The truthy string was being interpreted as the positional force_update
parameter, which obviously causes problems if the record already exists.
- [Django]-How to clear Recent Actions panel on Django Admin?
- [Django]-Simple Log to File example for django 1.3+
- [Django]-Django FileField (or ImageField) open() method returns None for valid file?
-2๐
Yes you are making a OneToOne relation between User and UserProfile models thatโs why you are facing integrity error,
for fixing this you can change it to ForeignKey
Like :
class UserProfile(models.Model):
user = models.ForeignKey(User,on_delete=models.CASCADE)
bio = models.TextField(blank=True)
...
...
- [Django]-Top level object in './docker-compose.yml' needs to be an object not '<class 'NoneType'>'
- [Django]-"CSRF token missing or incorrect" while post parameter via AJAX in Django
- [Django]-How do I include image files in Django templates?