Default django foreign key value for users

I read http://www.b-list.org/weblog/2006/jun/06/django-tips-extending-user-model/ but I'm still having problems migrating my new db schema using ForeignKey for user to product list

simplified version of my code:

from django.contrib.auth.models import User # other imports class Product(models.Model): author = models.ForeignKey(User) # other product fields.. 

but when I try to move to the South, and this can be a problem, since my user database is not processed by the South ("sorry my newbie"), I get errors that need to be set by default: / p>

 _mysql_exceptions.OperationalError: (1067, "Invalid default value for 'author_id'") 

Can someone explain what I'm doing wrong? Ideally, I would not want the default value for the user, or at least null, false, or 0 (but this does not seem to work).

thank you very much,

Adam

+4
source share
2 answers

If you want to have an empty value for author , you need to define a ForeignKey with null=True . You will probably need blank=True , as this controls the validation in Django forms.

 author = models.ForeignKey(User, null=True, blank=True) 
0
source

A simple route ended and completely dropped the product table and migrated from scratch. Not sure if I learned anything (except how to reset the application)!

0
source

Source: https://habr.com/ru/post/1336791/


All Articles