djangopostgresqldjango-modelsdjango-postgresql

Should I use ArrayField or ManyToManyField for tags


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 ?


Solution

  • If you use an Array field,

    If you use M2M field,

    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.