簡體   English   中英

錯誤:blog.Comment: (models.W042) 未定義主鍵類型時使用自動創建的主鍵,默認為 'django.db.models.AutoField'

[英]Error: blog.Comment: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'

mysite-virtualenv) 10:43 ~/django-blog (master)$ ./manage.py migrate
System check identified some issues:
WARNINGS:
blog.Comment: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
        HINT: Configure the DEFAULT_AUTO_FIELD setting or the BlogConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'dja
ngo.db.models.BigAutoField'.
blog.Post: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
        HINT: Configure the DEFAULT_AUTO_FIELD setting or the BlogConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'dja
ngo.db.models.BigAutoField'.
users.Profile: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
        HINT: Configure the DEFAULT_AUTO_FIELD setting or the UsersConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'dj
ango.db.models.BigAutoField'.

要執行的操作:應用所有遷移:管理員、身份驗證、博客、內容類型、會話、用戶 運行遷移:沒有要應用的遷移。

自動創建的主鍵 [Django Doc]

為避免將來發生不必要的遷移,請將 DEFAULT_AUTO_FIELD 顯式設置為 AutoField

在你的settings.py中添加DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'

如果要設置每個應用程序的字段類型,那么您可以指定每個應用程序的基礎

from django.apps import AppConfig

class MyAppConfig(AppConfig):
    default_auto_field = 'django.db.models.AutoField'
    name = 'my_app'

甚至您可以將每個 model 指定為

from django.db import models

class MyModel(models.Model):
    id = models.AutoField(primary_key=True)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM