D
D
dim_nikita2018-03-23 19:19:27
Django
dim_nikita, 2018-03-23 19:19:27

Why Django error?

It is necessary to extend the standard User model. Here is the model:

class Post(AbstractUser):
    post = models.CharField(max_length=500)
    user = models.ForeignKey(User)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    user_pk  = models.IntegerField(default=0)
    user_send = models.IntegerField(default=0)

    def __str__(self):
        return str(self.created)

In settings.py I wrote AUTH_USER_MODEL = 'home.Post' I logged
into the admin panel admin.site.register(Post, UserAdmin)
Why is the error ERRORS:
accounts.UserProfile.user: (fields.E301) Field defines a relation with the model 'auth .User', which has been swapped out.
HINT: Update the relation to point at 'settings.AUTH_USER_MODEL'.
home.Friend.current_user: (fields.E301) Field defines a relation with the model 'auth.User', which has been swapped out.
What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
TechGirl, 2021-02-26
@TechGirl

Hello) Oh, the question is two years old, but just in case I will answer.
I faced the same problem today, there are two options:
1. Delete the database and migration and recreate it (with the command python manage.py makemigrations, python manage.py migrate), but unfortunately in my case it did not work ...
2. Replace the word User with settings.AUTH_USER_MODEL, and don't forget to import settings(from django.conf import settings)
models.py

from django.conf import settings

class Post(AbstractUser):
    post = models.CharField(max_length=500)
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    user_pk  = models.IntegerField(default=0)
    user_send = models.IntegerField(default=0)

    def __str__(self):
        return str(self.created)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question