For example,
JavaScript
x
4
1
class TestModel(models.Model):
2
title = models.CharField(max_length = 200)
3
descriptions = models.JsonField()
4
Or
JavaScript
1
8
1
class TestModel(models.Model):
2
title = models.CharField(max_length = 200)
3
description_1 = models.TextField()
4
description_2 = models.TextField()
5
description_3 = models.TextField()
6
description_4 = models.TextField()
7
description_5 = models.TextField()
8
Assume that I have a limited (max 5) number of descriptions. Which approach is better and would be considered as good practice?
Advertisement
Answer
I am generally in favour of multiple models rather than using JSON, though there is still a time and a place for the JSON field. You have a number of description fields, you can do something like this:
JavaScript
1
7
1
class TestModel(models.Model):
2
title = models.CharField(max_length = 200)
3
4
class TestModelDescription(models.Model):
5
test_model = models.ForeignKey(TestModel,
6
description = models.CharField(
7
You can then have any number of descriptions and access them like this:
JavaScript
1
2
1
test_model.testmodeldescription_set.all()
2