类型字符 Django Factory 的值太长

value too long for type character Django Factory

我有下面的 OrderDataFactory class。

class OrderDataFactory(factory.django.DjangoModelFactory):

    class Meta:
        model = models.OrderData

    order = factory.SubFactory(OrderFactory)
    category = 'single',
    quantity = 75.6
    price_per_kg = 10.5
    sku = factory.SelfAttribute('crop_data.id')

    class Params:
        crop_data = factory.SubFactory(CropFactory)

models.py

class OrderData(models.Model):
    CATEGORY_CHOICES = (
        (SKUCategory.SINGLE, 'Single Sku'),
        (SKUCategory.COMBO, 'Combo Sku'),
    )
    sku = models.PositiveIntegerField(null=False, validators=[MinValueValidator(1)])
    order = models.ForeignKey(Order, on_delete=models.CASCADE)
    quantity = models.FloatField()
    price_per_kg = models.FloatField()
    category = models.CharField(choices=CATEGORY_CHOICES, max_length=8)
    class Meta:
        unique_together = (('sku', 'order', 'category'),)

Category.py

class SKUCategory:
    SINGLE = 'single'
    COMBO = 'combo'

我收到以下错误。

django.db.utils.DataError:对于类型字符 varying(8)

,值太长

即使类别的最大长度为 6。

你在这一行的末尾有一个逗号 category = 'single',。这将 category 定义为元组 ('single',),它可能被转换为 11 个字符长的字符串。删除逗号以使用字符串

category = 'single'