1、数据库迁移踩坑
1.1、在Django中未定义主键类型警告时使用的自动创建主键
问题:
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'
解决方法:
您的模型没有主键。但它们是由django自动创建的。
您需要选择auto-created主键的类型https://docs.djangoproject.com/en/3.2/releases/3.2/#定制--auto-created-主键的类型(django3.2中的新功能)
或者将此添加到settings.pyDEFAULT_AUTO_FIELD=’django.db.models.AutoField’
or
class Topic(models.Model):
id = models.AutoField(primary_key=True)
...
1.2、Django (fields.W340) null has no effect on ManyToManyField.
model定义
class Customer(models.Model):
name = models.CharField(verbose_name='客户名', max_length=128)
contact = models.CharField(verbose_name='电话', max_length=11)
address = models.CharField(verbose_name='地址', max_length=128)
status_choices = (
(0, '意向客户'),
(3, '已报价'),
(1, '已拿样'),
(2, '已成交')
)
status = models.IntegerField(choices=status_choices, verbose_name='状态')
company = models.CharField(verbose_name='公司名', max_length=128, null=True, blank=True)
create_time = models.DateField(verbose_name='创建时间', auto_now=True)
product = models.ManyToManyField(Product, verbose_name='意向产品',null=True, blank=True)
description = models.TextField(verbose_name='备注', null=True, blank=True)
问题:警告信息
(fields.W340) null has no effect on ManyToManyField.
解决方法:
这只是一个小小的警告,可以不用处理,如果觉得碍眼,可以把product字段的null=True移除
移除后就是这样的:
product = models.ManyToManyField(Product, verbose_name='意向产品', blank=True)
重启后,警告就消失了。
参考博客
问题1:https://www.5axxw.com/questions/content/22qijg
问题2:https://cloud.tencent.com/developer/article/1595866