Should I use ArrayField or ManyToManyField for tags

青春壹個敷衍的年華 提交于 2020-01-17 06:42:13

问题


I am trying to add tags to a model for a postgres db in django and I found two solutions:

using foreign keys:

class Post(models.Model):
    tags = models.ManyToManyField('tags')
    ...

class Tag(models.Model):
    name = models.CharField(max_length=140)

using array field:

from django.contrib.postgres.fields import ArrayField

class Post(models.Model):
    tags = ArrayField(models.CharField(max_length=140))
    ...

assuming that I don't care about supporting other database-backends in my code, what is a recommended solution ?


回答1:


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 a lot more toast tables (http://www.postgresql.org/docs/9.5/static/storage-toast.html)

  • Every time you get the row, unless you specifically use defer (https://docs.djangoproject.com/en/1.9/ref/models/querysets/#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.




回答2:


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>]


来源:https://stackoverflow.com/questions/44382653/should-i-use-arrayfield-or-manytomanyfield-for-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!