gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 13:52:29 25 4
gpt4 key购买 nike

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

最佳答案

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

关于python-3.x - 在 Django 中未定义主键类型警告时使用的自动创建主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66971594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com