(fields.E301)字段定义与模型'auth.User'的关系,该模型已被换出

时间:2017-10-04 10:17:33

标签: python django django-models

错误: blog.Post.author:(fields.E301)字段定义与模型'auth.User'的关系,该模型已被换出。 提示:更新关系以指向'settings.AUTH_USER_MODEL'。

我知道我需要为auth.User交换settings.AUTH_USER_MODEL,但出于某种原因这样做会导致我的博客无法显示。当我尝试从导航栏查看博客但没有内容时,路由是正确的。我也无法在Django admin上登录或创建新用户。在帐户设置中设置AUTH_USER_MODEL时,就会出现这些问题。

博客/ models.py

from django.db import models
from django.utils import timezone
from fitness_project import settings


class Post(models.Model):
    author = models.ForeignKey('auth.User', related_name='User')
    #author = models.ForeignKey(settings.AUTH_USER_MODEL)
    title = models.CharField(max_length=200)
    text = models.TextField()
    created_date = models.DateTimeField(
            default=timezone.now)
    published_date = models.DateTimeField(
            blank=True, null=True)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def __str__(self):
        return self.title

settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'hello',
    'blog',
    'membership',
    'accounts',
    'django_forms_bootstrap',
]

AUTH_USER_MODEL = 'accounts.User'

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'accounts.backends.EmailAuth',
)

博客/ admin.py

from django.contrib import admin
from .models import Post

admin.site.register(Post)

2 个答案:

答案 0 :(得分:5)

由于您已设置"content",因此您的模型应为:

{
  "presentationId": "000AAAAAAAAA_AAA0AA00AA00A0A0AAAA00AAAA7AAA",
  "pageSize": {
    "width": {
      "magnitude": 9144000,
      "unit": "EMU"
    },
    "height": {
      "magnitude": 5143500,
      "unit": "EMU"
    }
  },
  "slides": [
    {
      "objectId": "p",
      "pageElements": [
        {
          "objectId": "i0",
          "size": {
            "width": {
              "magnitude": 3000000,
              "unit": "EMU"
            },
            "height": {
              "magnitude": 3000000,
              "unit": "EMU"
            }
          },
          "transform": {
            "scaleX": 2.8402,
            "scaleY": 0.6842,
            "translateX": 311708.35000000003,
            "translateY": 744575,
            "unit": "EMU"
          },
          "shape": {
            "shapeType": "TEXT_BOX",
            "text": {
              "textElements": [
                {
                  "endIndex": 6,
                  "paragraphMarker": {
                    "style": {
                      "direction": "LEFT_TO_RIGHT"
                    }
                  }
                },
                {
                  "endIndex": 6,
                  "textRun": {
                    "content": "My ID\n",
                    "style": {}
                  }
                }
              ]
            },

然而,switch to a custom user model for an existing project非常困难。如果您尝试这样做,您可能会发现删除数据库最简单,删除迁移,然后重新运行AUTH_USER_MODEL = 'accounts.User'class Post(models.Model): author = models.ForeignKey(settings.AUTH_USER_MODEL)

答案 1 :(得分:0)

Alasdair 的回答是正确的,但这是我对它的更多修改。

由于您已经导入 - from fitness_project import settings ,您还可以分配一个名为 User 的变量,以便下次在模型的字段中使用它

User = settings.AUTH_USER_MODEL

然后,像往常一样,您可以像这样使用 User -

author = models.ForeignKey(User, related_name='User')