简体   繁体   中英

IntegrityError at /***/ (1048, "Column '***' cannot be null") in python django

Hi I am getting this error because I have a field in my additional user info model ( user = models.OneToOneField(User) ) that I am not filling in at sign up (as I want to let the user do it later).

I wondered if there was any way to solve this problem other than allowing null field in the db?

Cheers

Try to

user = models.OneToOneField(User, null=True, blank=True)

And then recreate your db. You can find more on https://docs.djangoproject.com/en/1.4/ref/models/fields/#null

Else you can use Proxy models:

class UserExtraInfo(User):
#here your extra fields

In this case you won`t need to create UserExtraInfo instance in same time with User. Read more on https://docs.djangoproject.com/en/1.4/topics/db/models/#model-inheritance

Integrity error occurred basically when you define some database field as not null and pass

the null or blank value

I am assuming you are storing the value in your database by django form

so in that case you can do like

if request.method == POST: # whatever the method

   get_form_obj = form.save(commit = False)

Don't forget to make change in your model user field like (null = True,blank = True)

hope this will help

For me it was depth = 1 in serializers.py, just remove this part and the request goes through. Nested serializers were causing the problem because of this (in console it was showing NestedSerializer(read_only=True):)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM