[Solved]-Django: Foreign Key relation with User Table does not validate

33đź‘Ť

âś…

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

class Topic(models.Model):
    user = models.ForeignKey(User) 

'auth.User' would have worked, too. It’s not Python’s library syntax, it’s the Django ORM’s “app.model” syntax. But you should only pass the model as a string if you’re desperately trying to solve a circular dependency. And if you have a circular dependency, your code is eff’d.

👤Elf Sternberg

7đź‘Ť

Even I faced same issue,

The error message is clear: you haven’t installed the User model.

Add "django.contrib.auth" to INSTALLED_APPS in your settings.py.

That all..Hope it will solve this issue, worked fine for me.

👤Abdul Rafi

1đź‘Ť

I had the same error, but in different situation.
I splitted the models.py in two files:

myapp/
    models/
        __init__.py
        foo.py
        bar.py

In foo.py I had two models:

class Foo(models.Model):
    attr1 = ... etc

class FooBar(models.Model):
    other = ... etc
    foo = models.ForeignKey(Foo)

    class Meta():
        app_label = 'foo'

I solved adding Meta also in Foo model:

class Foo(models.Model):
    attr1 = ... etc

    class Meta():
        app_label = 'foo'
👤Griffosx

Leave a comment