1、数据库迁移踩坑

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

问题:

  1. WARNINGS:
  2. learning_logs.Entry: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
  3. 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'.
  4. learning_logs.Topic: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
  5. 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'.
  6. 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

  1. class Topic(models.Model):
  2. id = models.AutoField(primary_key=True)
  3. ...

1.2、Django (fields.W340) null has no effect on ManyToManyField.

model定义

  1. class Customer(models.Model):
  2. name = models.CharField(verbose_name='客户名', max_length=128)
  3. contact = models.CharField(verbose_name='电话', max_length=11)
  4. address = models.CharField(verbose_name='地址', max_length=128)
  5. status_choices = (
  6. (0, '意向客户'),
  7. (3, '已报价'),
  8. (1, '已拿样'),
  9. (2, '已成交')
  10. )
  11. status = models.IntegerField(choices=status_choices, verbose_name='状态')
  12. company = models.CharField(verbose_name='公司名', max_length=128, null=True, blank=True)
  13. create_time = models.DateField(verbose_name='创建时间', auto_now=True)
  14. product = models.ManyToManyField(Product, verbose_name='意向产品',null=True, blank=True)
  15. description = models.TextField(verbose_name='备注', null=True, blank=True)

问题:警告信息

  1. (fields.W340) null has no effect on ManyToManyField.

解决方法:

这只是一个小小的警告,可以不用处理,如果觉得碍眼,可以把product字段的null=True移除
移除后就是这样的:

  1. product = models.ManyToManyField(Product, verbose_name='意向产品', blank=True)

重启后,警告就消失了。

参考博客

问题1:https://www.5axxw.com/questions/content/22qijg
问题2:https://cloud.tencent.com/developer/article/1595866