Python 可以在 Django 模型中存储数组吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44630642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:16:08  来源:igfitidea点击:

It's possible to store an array in Django model?

pythonarraysdjango

提问by Elros Romeo

I was wondering if it's possible to store an array in a Django model?

我想知道是否可以在 Django 模型中存储数组?

I'm asking this because I need to store an array of int(e.g [1,2,3]) in a field and then be able to search a specific array and get a match with it or by it's possible combinations.

我问这个是因为我需要在一个字段中存储一个 int 数组(例如 [1,2,3]),然后能够搜索一个特定的数组并与它匹配或通过它的可能组合。

I was thinking to store that arrays as strings in charfields and then, when I need to search something, concatenate the values(obtained by filtering other model) with '[', ']' and ',' and then use a object filter with that generated string. The problem is that I will have to generate each possible combination and then filter one by one until I get a match and I believe that this might be inefficient.

我想将该数组作为字符串存储在 charfields 中,然后,当我需要搜索某些内容时,将值(通过过滤其他模型获得)与 '[', ']' 和 ',' 连接起来,然后使用对象过滤器那个生成的字符串。问题是我必须生成每个可能的组合,然后一个一个过滤,直到我得到一个匹配,我相信这可能是低效的。

So, I hope you can give me another ideas that I could try.

所以,我希望你能给我另一个我可以尝试的想法。

I'm not asking code necessarily, any ideas on how to achieve this will be good.

我不一定要问代码,关于如何实现这一点的任何想法都会很好。

回答by Tim Specht

I'd have two advices for you:

我有两个建议给你:

1) Use ArrayFieldif you are using PostgreSQL as your database. You can read more about ArrayFieldhere.

1)ArrayField如果您使用 PostgreSQL 作为数据库,请使用。您可以ArrayField在此处阅读更多信息。

2) Encode your array as JSON and store it either as a plain string or using a JSONFieldas found here.

2) 将您的数组编码为 JSON 并将其存储为纯字符串或使用 a JSONFieldas found here

I'd personally prefer option number 1 since that is the cleaner and nicer way but depending on what you are actually using to store your data that might not be available to you.

我个人更喜欢选项 1,因为这是更干净、更好的方式,但取决于您实际使用什么来存储您可能无法使用的数据。

回答by Exprator

Yes, you can use it like this:

是的,您可以像这样使用它:

from django.contrib.postgres.fields import ArrayField
class Board(models.Model):
    pieces = ArrayField(ArrayField(models.IntegerField()))

However, it can only be available when using PostgreSQLfor the database.

但是,它只能在数据库使用PostgreSQL时可用。

回答by Chris Conlan

If you aren't using Postgres, I recommend Django's validate_comma_separated_integer_listvalidator.

如果你不使用 Postgres,我推荐 Django 的validate_comma_separated_integer_list验证器。

https://docs.djangoproject.com/en/dev/ref/validators/#django.core.validators.validate_comma_separated_integer_list

https://docs.djangoproject.com/en/dev/ref/validators/#django.core.validators.validate_comma_separated_integer_list

You use is as a validator on a CharField().

您使用 is 作为CharField().

回答by alastair

I don't know why nobody has suggested it, but you can always pickle things and put the result into a binary field.

我不知道为什么没有人建议它,但是您总是可以腌制事物并将结果放入二进制字段。

The advantages of this method are that it will work with just about any database, it's efficient, and it's applicable to more than just arrays. The downside is that you can't have the database run queries on the pickled data (not easily, anyway).

这种方法的优点是它几乎可以与任何数据库一起使用,它很高效,并且不仅仅适用于数组。缺点是您不能让数据库对腌制数据运行查询(无论如何都不容易)。