问答中心分类: PYTHON在Django中未定义主键类型警告时使用的自动创建主键
0
匿名用户 提问 31分钟 前

我刚刚将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]
4 Answers
0
iklinac 回答 31分钟 前

您无意中将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)

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

Dos 回复 31分钟 前

这是所有选项的完整解释,谢谢

0
Ali Aref 回答 31分钟 前

新建项目settings.py文件输入django 3.2包括:

..

# 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的早期版本上创建的,因此您可以附加此到您的设置。

0
Himaloy Mondal 回答 31分钟 前
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-钥匙

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

DEFAULT_AUTO_FIELD https://docs.djangoproject.com/en/3.2/ref/settings/#default-自动字段

Jeremy Caney 回复 31分钟 前

你能更新你的答案以包括解释吗?

0
Mayur Gupta 回答 31分钟 前

一个简单的解决方案:
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()

快乐编码🤗