11👍
If you use an Array field,
-
The size of each row in your DB is going to be a bit large thus Postgres is going to be using more toast tables
-
Every time you get the row, unless you specifically use defer the field or otherwise exclude it from the query via only, or values or something, you paying the cost of loading all those values every time you iterate across that row. If that’s what you need then so be it.
-
Filtering based on values in that array, while possible isn’t going to be as nice and the Django ORM doesn’t make it as obvious as it does for M2M tables.
If you use M2M field,
-
You can filter more easily on those related values
Those fields are postponed by default, you can use prefetch_related if you need them and then get fancy if you want only a subset of those values loaded. -
Total storage in the DB is going to be slightly higher with M2M because of keys, and extra id fields.
-
The cost of the joins in this case is completely negligible because of keys.
With that being said, the above answer doesn’t belong to me. A while ago, I had stumbled upon this dilemma when I was learning Django. I had found the answer here in this question, Django Postgres ArrayField vs One-to-Many relationship.
Hope you get what you were looking for.
4👍
If you want the class tags to be monitored ( For eg : how many tags, how many of a particular tag etd ) , the go for the first option as you can add more fields to the model and will add richness to the app.
On the other hand, if you just want it to be a array list just for sake of displaying or minimal processing, go for that option.
But if you wish to save time and add richness to the app, you can use this
https://github.com/alex/django-taggit
It is as simple as this to initialise :
from django.db import models
from taggit.managers import TaggableManager
class Food(models.Model):
# ... fields here
tags = TaggableManager()
and can be used in the following way :
>>> apple = Food.objects.create(name="apple")
>>> apple.tags.add("red", "green", "delicious")
>>> apple.tags.all()
[<Tag: red>, <Tag: green>, <Tag: delicious>]
- Multiple database support in django
- Limit Maximum Choices of ManyToManyField
- Postgresql operator class "varchar_pattern_ops" does not accept data type integer
- Jekyll templates using django-like liquid blocks / inheritance