【问题标题】:Auto-create primary key used when not defining a primary key type warning in Django未在 Django 中定义主键类型警告时使用的自动创建主键
【发布时间】:2021-07-02 09:52:17
【问题描述】:

我刚刚将我的 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'

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

模型.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]

【问题讨论】:

  • 您能否使用控制台日志作为文本而不是屏幕截图来更新您的答案?
  • @Nuts 如何从命令提示符复制文本?

标签: python-3.x django


【解决方案1】:

您的模型没有主键。但它们是由 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)
    ...

【讨论】:

  • 我在哪里将它添加到设置中。这可以添加到任何地方或开始或结束吗?
  • 任何地方,但只需在最后添加它
【解决方案2】:

您无意中将 Django 更新到 3.2,它会警告您,并且提示文本建议您必须将 DEFAULT_AUTO_FIELD 设置为 documented 这样您就可以避免在将来的版本中进行不必要的迁移,因为 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)

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

【讨论】:

  • 这是列出所有选项的完整说明,谢谢
【解决方案3】:

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 上创建的,因此您可以将此添加到您的设置中。

【讨论】:

    【解决方案4】:
    models.py:
           class Topic(models.Model):
               id = models.AutoField(primary_key=True)
    

    primary_key=Truehttps://docs.djangoproject.com/en/3.2/ref/models/fields/#primary-key

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

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

    【讨论】:

    • 您能否更新您的答案以包含解释?
    【解决方案5】:

    一个简单的解决方案:

    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()
    

    快乐编码?

    【讨论】:

      猜你喜欢
      • 2021-08-19
      • 2016-07-09
      • 2018-06-05
      • 1970-01-01
      • 2018-12-30
      • 1970-01-01
      • 1970-01-01
      • 2018-02-16
      • 1970-01-01
      相关资源
      最近更新 更多