简体   繁体   中英

django - model foreign key as logged user

I've got model code as follows:

from django.db import models
from django.core.validators import MinLengthValidator
from django.urls import reverse
from django.conf import settings

class Doc(models.Model):
...
added_by = models.CharField(blank=False,null=True,max_length=100,validators=[MinLengthValidator(10)])
added_by_2 = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
...

I can migrate this without any problems, but every when I run server and in admin site try to view all records I get:

Exception Type: OperationalError
Exception Value: no such column: added_by_2_id

What is the cause of this error? How to resolve it?

You have to add the null=True option on your added_by_2 field declaration, so Django can store empty values as NULL in the database.

Check the documentation for more info

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