Is there a way to store an arbitrary number of a particular Field
in a Django Model
such that a user can specify as many as he or she deems necessary? This would be useful for tags or image uploads.

- 16,341
- 7
- 39
- 70
-
I'm looking for something like `*args` but for models. – Malik Brahimi Jun 28 '15 at 13:48
-
If I were you I would break the table and make it foregin key for repetatible field... then apply inline formset... – nKandel Jun 28 '15 at 13:50
-
2Django documentation is very thorough and covers relationships pretty well. There are even [detailed examples](https://docs.djangoproject.com/en/1.8/topics/db/examples/many_to_one/) and, if that's not enough, duplicates of your question such as [this one](http://stackoverflow.com/questions/1110153/what-is-the-most-efficent-way-to-store-a-list-in-the-django-models) or [that one](http://stackoverflow.com/questions/5090047/django-create-a-model-that-lets-you-insert-multiple-values-for-the-same-field) can easily be found on stackoverflow. – spectras Jun 28 '15 at 14:07
4 Answers
If you want to add unknown quantitiy of attributes to a model, the obvious design choice would be to seperate them out.
Let's assume you have a Post
class and want to assign Tag
s to a Post. You can't know, how many Tags any Post will be tagged with.
You could model it this way:
class Post(models.Model):
author = models.CharField(max_length=255)
text = models.TextField()
class Tag(models.Model):
name = models.CharField(max_length=100)
attached_to = models.ForeignKey(Post)
Please note: This is written without testing, consider it dummy-code.

- 843
- 5
- 18
-
This is wrong. I was told to use a `ManytoMany` relationship by others. – Malik Brahimi Jul 02 '15 at 09:38
-
1It is **not** wrong. But you could use a m2m, of course. https://docs.djangoproject.com/en/1.8/topics/db/examples/many_to_many/ For your question, I would still use my FK solution... – Mischback Jul 02 '15 at 10:27
-
How do you add more to models to a foreign key in code, not in admin interface? – Malik Brahimi Jul 02 '15 at 11:29
Depending on the kind of data you want associated with a record, you either want to create a model which points to the original record using a ForeignKey, or you want to create a ManyToManyField on the original record.
Where you'd use a ForeignKey
In the example of multiple images per record, you want those images associated with that record and only that record. In those cases, you set up a foreign key which points the images back to the record. Like so:
class Record(models.Model):
name = models.CharField(max_length=100)
class Photo(models.Model):
record = models.ForeignKey(Record, related_name="photos")
image = models.ImageField()
Where you'd want to use ManyToMany field
In the case of tags, different records can all have the same tags. For example, you could have one record that was tagged "apples" and "farms" and another record that was tagged "apples" and "recipes". For both of these records, you want to point to a Tag record with the text "apple". You could create a tag record for each with the text "apple", but that's unnecessary duplication of data and can cause problems in the future. Using a ManyToMany record fixes this.
You make a change to the Record model:
class Record(models.Model):
name = models.CharField(max_length=100)
tags = models.ManyToManyField("Tag")
and create the Tag model:
class Tag(models.Model):
text = models.CharField(max_length=100)
This is more basic database design that Django programming, but whenever you're solving a problem where the goal is "store multiple values for x for a given record", the answer is nearly always "create a table to store the values for x, and link it back to the record table". In Django, that's done using ForeignKey
or ManyToManyField
.

- 20,467
- 11
- 95
- 161
I think you're better off with a new table/model to store your variable field count, and refer to them with a ForeignKey.

- 612
- 8
- 14
-
-
@MalikBrahimi : If you think about it as _key to another model_ or _key to another db table_ it becomes fitting! ;) – Mischback Jun 28 '15 at 15:39
If you use postgresql database, you could try one of those new Postgres specific fields: ArrayField, HStoreField, JsonField (JsonB to be done in 1.9). https://docs.djangoproject.com/en/dev/ref/contrib/postgres/fields/
If you don't need foreign key relation with those arbitrary data, it could be excellent choice.

- 1,687
- 18
- 16