我应该对标签使用ArrayField还是ManyToManyField [英] Should I use ArrayField or ManyToManyField for tags

查看:92
本文介绍了我应该对标签使用ArrayField还是ManyToManyField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Django中为postgres db的模型添加标签,我发现了两种解决方案:

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

使用外键:

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

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

使用数组字段:

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 ?

推荐答案

如果您使用Array字段,

If you use an Array field,

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆