Home > database >  django - model foreign key as logged user
django - model foreign key as logged user

Time:03-03

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?

CodePudding user response:

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

  • Related