72

I need to store some data in a Django model. These data are not equal to all instances of the model.

At first I thought about subclassing the model, but I’m trying to keep the application flexible. If I use subclasses, I’ll need to create a whole class each time I need a new kind of object, and that’s no good. I’ll also end up with a lot of subclasses only to store a pair of extra fields.

I really feel that a dictionary would be the best approach, but there’s nothing in the Django documentation about storing a dictionary in a Django model (or I can’t find it).

Any clues?

Kushan Gunasekera
  • 7,268
  • 6
  • 44
  • 58
AticusFinch
  • 2,351
  • 3
  • 25
  • 32
  • 8
    "some data" -- poorly defined. Without some data model or other hint, there is no answer. – S.Lott Dec 31 '08 at 03:29

14 Answers14

46

If it's really dictionary like arbitrary data you're looking for you can probably use a two-level setup with one model that's a container and another model that's key-value pairs. You'd create an instance of the container, create each of the key-value instances, and associate the set of key-value instances with the container instance. Something like:

class Dicty(models.Model):
    name      = models.CharField(max_length=50)

class KeyVal(models.Model):
    container = models.ForeignKey(Dicty, db_index=True)
    key       = models.CharField(max_length=240, db_index=True)
    value     = models.CharField(max_length=240, db_index=True)

It's not pretty, but it'll let you access/search the innards of the dictionary using the DB whereas a pickle/serialize solution will not.

Parand
  • 102,950
  • 48
  • 151
  • 186
  • 5
    The only downside is that an additional DB query will ensue – Filip Dupanović May 07 '10 at 21:28
  • 5
    Another downside is that you have exactly one "level" of data, you can't create multi-level complex JSON-style data. ( but good idea nonetheless ) – Nick Perkins Jul 26 '11 at 20:43
  • 9
    Found a nice extension of this solution: https://djangosnippets.org/snippets/2451/ this guy extended the dictionary to all pythonic dictionary functions – michel.iamit Mar 07 '14 at 15:26
  • 1
    So how does this get called? – Micah Walter Jun 26 '14 at 20:56
  • 2
    @NickPerkins You could make it recursive by having `Dicty` contain a field `parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True)`. Any dict without a parent is a top level object. Tadaaa! Yes it would be nice if `value` pointed to another dict rather than using a special field but ¯\_(ツ)_/¯... Alternatively on `KeyVal` add `child = models.ForeignKey('Dicty', models.CASCADE, null=True, blank=True)` – Anthony Manning-Franklin Mar 13 '17 at 07:31
19

Another clean and fast solution can be found here: https://github.com/bradjasper/django-jsonfield

For convenience I copied the simple instructions.

Install

pip install jsonfield

Usage

from django.db import models
from jsonfield import JSONField

class MyModel(models.Model):
    json = JSONField()
odedfos
  • 4,491
  • 3
  • 30
  • 42
  • It is a nice way to use but it does not support dictionary validation by default. So if I send an int or a string it directly stores that data. – Devendra Bhat Feb 14 '19 at 07:10
  • 1
    `from django.contrib.postgres import fields` for PostgreSQL – Dimitrios Mistriotis Jan 22 '20 at 11:03
  • 3
    `JSONField` is available directly from `django.db.models`: `from django.db.models import JSONField` Usage details in docs: https://docs.djangoproject.com/en/4.0/ref/models/fields/#django.db.models.JSONField – geogeo Jun 07 '22 at 21:30
18

If you don't need to query by any of this extra data, then you can store it as a serialized dictionary. Use repr to turn the dictionary into a string, and eval to turn the string back into a dictionary. Take care with eval that there's no user data in the dictionary, or use a safe_eval implementation.

For example, in the create and update methods of your views, you can add:

if isinstance(request.data, dict) == False:
    req_data = request.data.dict().copy()
else:
    req_data = request.data.copy()

dict_key = 'request_parameter_that_has_a_dict_inside'
if dict_key in req_data.keys() and isinstance(req_data[dict_key], dict):
    req_data[dict_key] = repr(req_data[dict_key])
Philippe Fanaro
  • 6,148
  • 6
  • 38
  • 76
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
15

I came to this post by google's 4rth result to "django store object"

A little bit late, but django-picklefield looks like good solution to me.

Example from doc:

To use, just define a field in your model:

>>> from picklefield.fields import PickledObjectField
>>> class SomeObject(models.Model):
>>>     args = PickledObjectField()

and assign whatever you like (as long as it's picklable) to the field:

>>> obj = SomeObject()
>>> obj.args = ['fancy', {'objects': 'inside'}]
>>> obj.save()
Stéphane
  • 2,068
  • 22
  • 28
8

As Ned answered, you won't be able to query "some data" if you use the dictionary approach.

If you still need to store dictionaries then the best approach, by far, is the PickleField class documented in Marty Alchin's new book Pro Django. This method uses Python class properties to pickle/unpickle a python object, only on demand, that is stored in a model field.

The basics of this approach is to use django's contibute_to_class method to dynamically add a new field to your model and uses getattr/setattr to do the serializing on demand.

One of the few online examples I could find that is similar is this definition of a JSONField.

Van Gale
  • 43,536
  • 9
  • 71
  • 81
6

I'm not sure exactly sure of the nature of the problem you're trying to solve, but it sounds curiously similar to Google App Engine's BigTable Expando.

Expandos allow you to specify and store additional fields on an database-backed object instance at runtime. To quote from the docs:

import datetime
from google.appengine.ext import db

class Song(db.Expando):
  title = db.StringProperty()

crazy = Song(title='Crazy like a diamond',
             author='Lucy Sky',
             publish_date='yesterday',
             rating=5.0)

crazy.last_minute_note=db.Text('Get a train to the station.')

Google App Engine currently supports both Python and the Django framework. Might be worth looking into if this is the best way to express your models.

Traditional relational database models don't have this kind of column-addition flexibility. If your datatypes are simple enough you could break from traditional RDBMS philosophy and hack values into a single column via serialization as @Ned Batchelder proposes; however, if you have to use an RDBMS, Django model inheritance is probably the way to go. Notably, it will create a one-to-one foreign key relation for each level of derivation.

Community
  • 1
  • 1
cdleary
  • 69,512
  • 53
  • 163
  • 191
6

This question is old, but I was having the same problem, ended here and the chosen answer couldn't solve my problem anymore.

If you want to store dictionaries in Django or REST Api, either to be used as objects in your front end, or because your data won't necessarily have the same structure, the solution I used can help you.

When saving the data in your API, use json.dump() method to be able to store it in a proper json format, as described in this question.

If you use this structure, your data will already be in the appropriate json format to be called in the front end with JSON.parse() in your ajax (or whatever) call.

Lucas Colzani
  • 135
  • 2
  • 11
5

I use a textfield and json.loads()/json.dumps()

models.py

import json
from django.db import models

class Item(models.Model):
    data = models.TextField(blank=True, null=True, default='{}')

    def save(self, *args, **kwargs):
        ## load the current string and
        ## convert string to python dictionary
        data_dict = json.loads(self.data)

        ## do something with the dictionary
        for something in somethings:
            data_dict[something] = some_function(something)

        ## if it is empty, save it back to a '{}' string,
        ## if it is not empty, convert the dictionary back to a json string
        if not data_dict:
            self.data = '{}'
        else:
            self.data = json.dumps(data_dict)


        super(Item, self).save(*args, **kwargs)

Grady Woodruff
  • 143
  • 2
  • 11
  • To explain in short, first you change the dict to JSON, then you change it to a string using json.loads and json.dumps respectively, and save that in the db. Finally when you retrieve the data you read it as a dict, right? – Odasaku Sep 28 '21 at 11:55
3

Being "not equal to all instances of the model" sounds to me like a good match for a "Schema-free database". CouchDB is the poster child for that approach and you might consider that.

In a project I moved several tables which never played very nice with the Django ORM over to CouchDB and I'm quite happy with that. I use couchdb-python without any of the Django-specific CouchDB modules. A description of the data model can be found here. The movement from five "models" in Django to 3 "models" in Django and one CouchDB "database" actually slightly reduced the total lines of code in my application.

Parag Tyagi
  • 8,780
  • 3
  • 42
  • 47
max
  • 29,122
  • 12
  • 52
  • 79
3

Django-Geo includes a "DictionaryField" you might find helpful:

http://code.google.com/p/django-geo/source/browse/trunk/fields.py?r=13#49

In general, if you don't need to query across the data use a denormalized approach to avoid extra queries. User settings are a pretty good example!

jb.
  • 9,987
  • 12
  • 39
  • 38
3

I agree that you need to refrain stuffing otherwise structured data into a single column. But if you must do that, Django has an XMLField build-in.

There's also JSONField at Django snipplets.

muhuk
  • 15,777
  • 9
  • 59
  • 98
2

I know this is an old question, but today (2021) the cleanest alternative is to use the native JSONfield (since django 3.1)

docs: https://docs.djangoproject.com/en/3.2/ref/models/fields/#django.db.models.JSONField

you just create a field in the model called jsonfield inside the class model and voilá

José
  • 1,774
  • 1
  • 17
  • 21
1

If you are using Postgres, you can use an hstore field: https://docs.djangoproject.com/en/1.10/ref/contrib/postgres/fields/#hstorefield.

Scott Stafford
  • 43,764
  • 28
  • 129
  • 177
1

Think it over, and find the commonalities of each data set... then define your model. It may require the use of subclasses or not. Foreign keys representing commonalities aren't to be avoided, but encouraged when they make sense.

Stuffing random data into a SQL table is not smart, unless it's truly non-relational data. If that's the case, define your problem and we may be able to help.

Daniel Naab
  • 22,690
  • 8
  • 54
  • 55