4

I have a model that has a Charfield (let's name it advantages) with a choices attribute. After a while, I've decided that this field should be "upgraded" to some kind of ListField, since more than one choice can be selected.

From what I have searched, I have two options:

1 - Create a new model, and use a ManyToManyField in the first model referencing this new model. This way, the "multiple select" default field used in admin will be rendered. Life is good.

2- Create a custom field that saves my field as a string with some kind of separator.

These two approaches are summarized in What is the most efficent way to store a list in the Django models? and the 2nd approach in more examples: How to create list field in django, http://cramer.io/2008/08/08/custom-fields-in-django/, https://djangosnippets.org/snippets/1200/, https://djangosnippets.org/snippets/1491/

Fact is: I don't want to create another model just to have the ManyToManyField. This is a controlled list of choices that I have (and don't want people adding new items) and think that creating a table for this is overkill (although I can create a fixture to this table and not register the model in admin.py, so people wouldn't be adding new items. But I don't know how would migrations work when changing these values in fixtures, when in the past I would just chance the choices tuple in my model definition).

...and creating a new custom field, I don't know. This seems like problems in the long run since I don't know the implications, problems when upgrading Django, etc.

Why there isn't a built in ListField? Which problems do you see in the long run for the two approaches I'm thinking of? I'm planning to do the first but I'm a little lost about migrations.

Community
  • 1
  • 1
  • Do you mean a `ListField` (Array datatype) ? The issue is with the backend database - Postgres does support a list field, but mysql, sqlite, etc do not.. The future versions of django have a possiblity of including features to more closely support postgres database, so this might indeed be a reality. – karthikr Jun 30 '14 at 03:53
  • @karthikr I now this is because of backend agnostic features. But a not so 'fast' `ListField ` using the 2nd approach would be nice to have if it was builtin. It doesn't necessarily needs to be an array, but it would *appear* to be, even with some performance issues. – Somebody still uses you MS-DOS Jun 30 '14 at 04:29
  • 1
    Because it would violate normalization and never be large enough. – Ignacio Vazquez-Abrams Jun 30 '14 at 04:32

2 Answers2

2

django.contrib.postgres has an ArrayField.

1

You seem to not be willing to create a new table with only the List inside (because it would be "overkill"), but what you are suggesting is to copy/paste the same exact values in all the entries of your table, which isn't a good model solution in my opinion.

I'd advise to go for the ManyToMany (or any other implementation doing the trick with another table).

Raphael Laurent
  • 1,941
  • 1
  • 17
  • 27