Is it possible to store an array in Django model?

后端 未结 5 1700
日久生厌
日久生厌 2020-12-09 02:11

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

I\'m asking this because I need to store an array of int (e.g [1,2,3]) in a field

5条回答
  •  旧巷少年郎
    2020-12-09 02:44

    I'd have two advices for you:

    1) Use ArrayField if you are using PostgreSQL as your database. You can read more about ArrayField here.

    2) Encode your array as JSON and store it either as a plain string or using a JSONField as 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.

提交回复
热议问题