Users as foreign key in Django

Question:

I have the below in my models.py file:

class Film(models.Model):
    title = models.CharField(max_length=200)
    director = models.CharField(max_length=200)
    description = models.CharField(max_length=200)
    pub_date = models.DateField('date published')

class Comment(models.Model):
    film = models.ForeignKey(Film, on_delete=models.CASCADE)
    body = models.CharField(max_length=200)

When I logged into Django admin I added some films, and then added some comments, selecting which film object the comment related to. I then created a couple of users via the admin panel also.

I would like my relationships to be:

Film can have many comments / Comments belong to film

User can have many comments / Comments belong to user

I think, like with comments and films, I just need to define user as a foreign key to comment. I am struggling to do this. I am working through the Django tutorials but I can’t see the tutorials covering how I can link other tables to the user.

I thought I would be able to do something like this:

user = models.ForeignKey(User, on_delete=models.CASCADE)

While importing User like this:

from django.contrib.auth.models import User

The result at the moment is if I keep user = models.ForeignKey(User, on_delete=models.CASCADE) I get err_connection_refused

Asked By: Dan

||

Answers:

Maybe have you changed your default user model in the settings?

Instead of using User directly with the the Foreign key, you should use user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) in your Comment Model, as follow

class Comment(models.Model):
    film = models.ForeignKey(Film, on_delete=models.CASCADE)
    body = models.CharField(max_length=200)
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)

You need to apply migrations to be able to add user to Comment,
python manage.py makemigrations
python manage.py migrate

if at the moment that you are applying migrations, shell shows a message telling You are trying to add a non-nullable field ‘user’ to comment without a default

You have 2 Options

  1. Skip migrations and add a default value to the field in the models or set the attribute as nullable, whatever else that you need
    ie

user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True, blank=True)

and apply migrations again

  1. Or select a default value to the new field, should be an id of an existing user in databse

This is because django should populate existing records in database, if exist

Answered By: Angel F

Use "settings.AUTH_USER_MODEL".

So, import "settings" from "django.conf", then use "settings.AUTH_USER_MODEL" as shown below:

from django.db import models
from django.conf import settings # Here

class Comment(models.Model):
    film = models.ForeignKey(Film, on_delete=models.CASCADE)
    body = models.CharField(max_length=200)
                            # Here
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
Answered By: Kai – Kazuya Ito