47

I read the docs and this post... Django - Foreign Key to User model

I followed what it said and I still cannot get it to work. When I try to run the migrations I get this error in the traceback...

django.db.utils.ProgrammingError: column "author_id" cannot be cast automatically to type integer
HINT:  You might need to specify "USING author_id::integer".

I just don't know how to go about fixing that error.

from django.db import models
from django.contrib.auth.models import User

# Create your models here.
class BlogCategory(models.Model):
    '''model for categories'''

    title = models.CharField(max_length=30)
    description = models.CharField(max_length=100)


class BlogPost(models.Model):
    '''a model for a blog post'''

    author = models.ForeignKey(User)
    date = models.DateField()
    title = models.CharField(max_length=100)
    post = models.TextField()
Joff
  • 11,247
  • 16
  • 60
  • 103

4 Answers4

168

Don't use the User model directly.

From the documentation

Instead of referring to User directly, you should reference the user model using django.contrib.auth.get_user_model()

When you define a foreign key or many-to-many relations to the user model, you should specify the custom model using the AUTH_USER_MODEL setting.

Example:

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

class Article(models.Model):
    author = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
    )
Flimm
  • 136,138
  • 45
  • 251
  • 267
Andrew E
  • 7,697
  • 3
  • 42
  • 38
  • 4
    One thing that confused me when using this answer was whether or not I needed to define the `AUTH_USER_MODEL` setting in `settings.py`. Documentation says that is not necessary - `This method will return the currently active user model – the custom user model if one is specified, or User otherwise.` – Jordan Carroll Sep 09 '17 at 14:21
  • 8
    The [documentation](https://docs.djangoproject.com/en/1.10/topics/auth/customizing/#auth-custom-user) states: "Django allows you to override the default user model by providing a value for the AUTH_USER_MODEL setting". Basically: *if* you are providing a custom `User` model you *must* provide the `AUTH_USER_MODEL` setting. – Andrew E Sep 10 '17 at 04:09
3

If you created a custom User model, you would use setting.AUTH_USER_MODEL, if not you can go ahead an use User model

Referencing Django User model

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
Jermaine
  • 770
  • 7
  • 9
  • what if presently not using custom model and in future there are chances to use which option will be better then? – Krishnadas PC Apr 25 '23 at 07:18
  • 1
    Always go with using a custom user model from the initial setup, because it will be difficult making the changes later on. – Jermaine Apr 25 '23 at 20:34
0

the column "author_id" doesn't exist, looks like is the same problem from here : Django suffix ForeignKey field with _id , so to avoid this traceback you may use :

author = models.ForeignKey(User, db_column="user")
Community
  • 1
  • 1
Sérgio
  • 6,966
  • 1
  • 48
  • 53
-15

I do not know the "settings.AUTH_USER_MODEL" approach but a well-known approach and commonly used is the "Auth.User" model. Something like this on your end.

from django.contrib.auth.models import User

class BlogPost(models.Model):
    '''a model for a blog post'''

    author = models.ForeignKey(User)
    date = models.DateField()
    title = models.CharField(max_length=100)
    post = models.TextField()
Dean Christian Armada
  • 6,724
  • 9
  • 67
  • 116
  • I tried that before looking it up and doing the other way. I get the same error – Joff Dec 16 '15 at 07:29
  • Providing you haven't overridden the [`AUTH_USER_MODEL` setting](https://docs.djangoproject.com/en/1.9/ref/settings/#auth-user-model), the model you're referring to *is* the default – Sayse Dec 16 '15 at 07:37
  • 1
    https://docs.djangoproject.com/en/1.9/topics/auth/customizing/#substituting-a-custom-user-model, as what the link says the "AUTH_USER_MODEL" should be the user model of a certain app, have you done that on your end? Or you might want to provide other extra details like the value of your AUTH_USER_MODEL – Dean Christian Armada Dec 16 '15 at 07:41
  • I have not defined a user model for that class. Is there not some way to make it a foreign key to all users in my database? – Joff Dec 16 '15 at 07:52
  • from django.contrib.auth.models import User author = models.ForeignKey(User) is the best answer I got for you in your case as it is what I have always used – Dean Christian Armada Dec 16 '15 at 08:09
  • @DeanChristianArmada I get the same error when I use that way – Joff Dec 16 '15 at 08:30
  • I just tried it and emptied out the database of the previous post entries – Joff Dec 16 '15 at 08:31
  • I don't understand what happened here. The code in this answer looks like a snippet from the one in the question. – 101is5 Feb 01 '22 at 18:25