Django 为啥我应用迁移时会创建一个字段?

Posted

技术标签:

【中文标题】Django 为啥我应用迁移时会创建一个字段?【英文标题】:Django Why is one field created when I apply migrations?Django 为什么我应用迁移时会创建一个字段? 【发布时间】:2020-09-25 17:51:08 【问题描述】:

有模型,为什么python manage.py makemigrations后migrations中只有1个字段创建,如何解决?我尝试做manage.py migrate --fake zero,然后再次进行迁移,但没有。该应用程序已在设置中注册。

from django.db import models
from django.urls import reverse

class Category(models.Model):

   image = models.ImageField(default='default.png', upload_to='category_image'),
   title = models.CharField(max_length=50, db_index = True),

   def __str__(self):
        return self.title

   def get_absolute_url(self):
        return reverse('category_detail_url', kwargs='title': self.title)


class Provider(models.Model):

    name = models.CharField(max_length=50, db_index = True),
    phone_number = models.CharField(max_length=12, db_index = True),
    address = models.CharField(max_length=50, db_index = True),

    def __str__(self):
            return self.name


class Product(models.Model):

    title = models.CharField(max_length=50, db_index = True),
    receipt_date = models.DateTimeField(auto_now_add=True, blank=True),
    quantity_stock = models.IntegerField(),
    quantity_store = models.IntegerField(),
    purchase_price = models.IntegerField(),
    image = models.ImageField(default='default.png', upload_to='product_image'),
    provider = models.ForeignKey(Provider, null = True ,related_name='to_provider',on_delete=models.CASCADE),
    category =  models.ForeignKey(Category, null = True ,related_name='to_category',on_delete=models.CASCADE),

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('product_detail_url', kwargs='title': self.title)


class Sale(models.Model):

    product  = models.ForeignKey(Product, related_name='to_product',on_delete=models.CASCADE),
    date_of_sale = models.DateTimeField(auto_now_add=True, blank=True),
    quantity_goods_sold = models.IntegerField(),
    retail_price = models.IntegerField(), 

    def __str__(self):
        return self.id

【问题讨论】:

因为您在字段中添加了尾随逗号,因此将字段包装在一个元组中。 去掉最后的逗号,所以IntegerField()而不是IntegerField(), 【参考方案1】:

您的字段不应以逗号 (,) 结尾。如果添加尾随逗号,它将将该字段包装在 单元组中,因此 Django 无法检测到该字段:

from django.db import models
from django.urls import reverse

class Category(models.Model):
   image = models.ImageField(default='default.png', upload_to='category_image')
   title = models.CharField(max_length=50, db_index = True)

   def __str__(self):
        return self.title

   def get_absolute_url(self):
        return reverse('category_detail_url', kwargs='title': self.title)


class Provider(models.Model):
    name = models.CharField(max_length=50, db_index=True)
    phone_number = models.CharField(max_length=12, db_index=True)
    address = models.CharField(max_length=50, db_index=True)

    def __str__(self):
        return self.name


class Product(models.Model):
    title = models.CharField(max_length=50, db_index = True)
    receipt_date = models.DateTimeField(auto_now_add=True, blank=True)
    quantity_stock = models.IntegerField()
    quantity_store = models.IntegerField()
    purchase_price = models.IntegerField()
    image = models.ImageField(default='default.png', upload_to='product_image')
    provider = models.ForeignKey(
        Provider,
        null=True,
        related_name='products',
        on_delete=models.CASCADE
    )
    category =  models.ForeignKey(
        Category,
        null=True,
        related_name='products',
        on_delete=models.CASCADE
    )

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('product_detail_url', kwargs='title': self.title)


class Sale(models.Model):

    product = models.ForeignKey(
        Product,
        related_name='sales',
        on_delete=models.CASCADE
    )
    date_of_sale = models.DateTimeField(auto_now_add=True, blank=True)
    quantity_goods_sold = models.IntegerField()
    retail_price = models.IntegerField()

    def __str__(self):
        return self.id

【讨论】:

以上是关于Django 为啥我应用迁移时会创建一个字段?的主要内容,如果未能解决你的问题,请参考以下文章

如何在gae中部署django应用程序时包含migrate命令

为啥 django 1.7 会为字段选择的变化创建迁移?

Django 1.7 迁移不会重新创建删除的表,为啥?

实体框架代码首次迁移文件源代码控制

在 vagrant 的 Django 应用程序中创建迁移文件夹的权限被拒绝

为啥升级我的版本 django-mptt 后出现此数据库迁移错误?