[英]Is it better to use JsonField or multiple model fields for a Django Model?
例如,
class TestModel(models.Model):
title = models.CharField(max_length = 200)
descriptions = models.JsonField()
或者
class TestModel(models.Model):
title = models.CharField(max_length = 200)
description_1 = models.TextField()
description_2 = models.TextField()
description_3 = models.TextField()
description_4 = models.TextField()
description_5 = models.TextField()
假设我的描述数量有限(最多 5 个)。 哪种方法更好并且会被视为良好做法?
我通常赞成使用多种模型而不是使用 JSON,尽管 JSON 字段仍有时间和地点。 您有许多描述字段,您可以执行以下操作:
class TestModel(models.Model):
title = models.CharField(max_length = 200)
class TestModelDescription(models.Model):
test_model = models.ForeignKey(TestModel, ...
description = models.CharField(...
然后,您可以拥有任意数量的描述并像这样访问它们:
test_model.testmodeldescription_set.all()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.