如何修复 Django 警告“(models.W042)未定义主键类型时使用自动创建的主键”?

问题描述 投票:0回答:5

我刚刚将 python 从 3.9.1 更新到 3.9.4。当我尝试运行服务器时。控制台为此向我发出警告:

WARNINGS:
learning_logs.Entry: (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 LearningLogsConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
learning_logs.Topic: (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 LearningLogsConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
No changes detected in app 'learning_logs'

请问我该如何解决这个问题。 我阅读了有关此内容的文档,但我不明白这部分此页面与此有何关系。

模型.py

from django.db import models
from django.contrib.auth.models import User
# Create your models here.

class Topic(models.Model):
    text = models.CharField(max_length = 200)
    date_added = models.DateTimeField(auto_now_add = True)
    image = models.ImageField(upload_to = 'backgroud_images', null = True, blank = True)
    owner = models.ForeignKey(User,on_delete = models.CASCADE)
    def __str__(self):
        return self.text



class Entry(models.Model):
    topic = models.ForeignKey(Topic,on_delete = models.CASCADE)
    text = models.TextField()
    date_added = models.DateTimeField(auto_now_add = True)

    class Meta:
        verbose_name_plural = "Entries"

    def __str__(self):
        return self.text[:50]
django django-models
5个回答
263
投票

您的模型没有主键。但它们是由 django 自动创建的。

您需要选择自动创建的主键类型 https://docs.djangoproject.com/en/3.2/releases/3.2/#customizing-type-of-auto-created-primary-keys(Django 3.2 中的新功能)

将其添加到settings.py中

DEFAULT_AUTO_FIELD='django.db.models.AutoField' 

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

67
投票

您无意中将 Django 更新到了 3.2,这会向您发出警告,并且提示文本建议您必须将

DEFAULT_AUTO_FIELD
设置为 记录 这样您就可以避免在未来版本中进行不必要的迁移,因为
DEFAULT_AUTO_FIELD
的值将更改为
BigAutoField

您应该将

DEFAULT_AUTO_FIELD
显式设置为当前
DEFAULT_AUTO_FIELD

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'

甚至每个模型(不鼓励)

from django.db import models

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

您还应该注意版本锁定您的需求,因为您可能会在生产中引入向后不兼容的更改


24
投票

django 3.2
中新建项目 settings.py 文件 包括这个:

..

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

由于您的项目是在早期版本的 django 上创建的,因此您可以将其附加到您的设置中。


6
投票
models.py: class Topic(models.Model): id = models.AutoField(primary_key=True)

primary_key=True

https://docs.djangoproject.com/en/3.2/ref/models/fields/#primary-key

settings.py: DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'

DEFAULT_AUTO_FIELD

https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
    


1
投票
一个简单的解决方案:

Django 希望您添加主键调用的序列号,如果您可以忽略此警告,那么 Django 会自动添加主键,否则,如果您想删除此警告,请在您定义的模型中添加此代码:

id = models.AutoField(primary_key=True)
例如:

class Contact(models.Model): sno = models.AutoField(primary_key=True) name = models.CharField(max_length=25, blank=True) email = models.EmailField(max_length=40, blank=True) phone = models.IntegerField()
快乐编码🤗

© www.soinside.com 2019 - 2024. All rights reserved.