在 Django 中未定义主键类型警告时使用的自动创建主键

时间:2021-04-06 15:23:41

标签: python-3.x django

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

5 个答案:

答案 0 :(得分:95)

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

答案 1 :(得分:19)

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

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

答案 2 :(得分:6)

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

答案 3 :(得分:1)

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

答案 4 :(得分:0)

一个简单的解决方案:

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

快乐编码?

相关问题