我JSONField
为其中一个模型添加了一个。我想在管理面板中创建该模型的实例,但JSONField
返回验证错误(输入有效的JSON。)
我该如何解决?
模型:
class Product(models.Model):
category = models.ManyToManyField(Category, related_name='products')
name = models.CharField(max_length=500)
slug = models.SlugField(max_length=500, allow_unicode=True, unique=True)
image_1 = models.ImageField(upload_to='products_pic/%Y/%m/%d/', null=True, blank=True)
description = models.TextField(null=True, blank=True)
attribute = models.JSONField(default={})
price = models.PositiveIntegerField()
discount = models.PositiveIntegerField(null=True, blank=True)
available = models.BooleanField(default=True)
popular = models.BooleanField(default=False)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
comments = GenericRelation(Comment)
class Meta:
ordering = ('-created',)
def __str__(self):
return self.name
我在管理面板中添加产品。我希望产品具有多个属性,例如颜色,尺寸和...。为此使用json字段是真的吗?
这是无效的JSON。JSON中的字符串用双引号引起来。实际上,您可以使用JSONLint.com例如对此进行验证。这是有效的Python字典,但无效的JSON。
因此,您应该输入:
{ "key": "value" }
有关更多信息,请参见JSON规范。
我在管理面板中添加产品。我希望产品具有多个属性,例如颜色,尺寸和...。为此使用json字段是真的吗?
如果字段是固定的(是all Product
的属性),则应使用单个字段,因为这样更方便筛选,制作表格等。如果数据更具动态性,则使用JSON通常更好领域。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句