Use an Array of Fields with SQLite ?

Hi everyone !

I’m just wondering how can we write a model which would be an Array of CharFields ?

Let’s take this example :

image

would be the result of this model :

    class SkillModel(models.Model):
        skill_name = models.CharField(max_length=40, default="")
        skill_level = models.CharField(max_length=40, default="")
        skill_key_points = models.??

For this skill the data would be :

skill_name = "HTML5"
skill_level = "Advanced"
skill_key_points = #Array Of CharFields represented by "Item" in the image above

How can I express this “Array” dimension in my model ?

Thank you in advance

You have at least three options - if you’re using PostgreSQL as your database, you have the option of using the PostgreSQL array field.

Or, if you’re looking for the more “pure” relational representation, you would use a related model as a many-to-one relationship.

Or, now that JSONField is available for all database backends, you could store these elements as an array in a JSON object.

Ken

2 Likes

(Oh, and thinking about my deep, dark history, there’s a fourth option I’ve used in the past under vastly different circumstances - you could store this list as a TextField with some delimiter between elements. I do not recommend this under any normal circumstances any more, but it is an option. A bad one perhaps, but an option none the less.)