views:

439

answers:

2

I'm new to python/django. I need to store an arbitrary number of fields in a django model. I'm wondering if django has something that takes care of this.

Typically, I would store some XML in a column to do this. Does django offer some classes that makes this easy to do whether it be XML or some other(better) method?

Thanks, Pete

+5  A: 

There are a lot of approaches to solve this problem, and depending on your situation any of them might work. You could certainly use a TextField to store XML or JSON or any other form of text. In combination with Python's pickle feature you can do some neater stuff.

You might look at the Django Pickle Field definition on DjangoSnippets: http://www.djangosnippets.org/snippets/513/

That allows you to dump Python dictionaries into fields and does some manipulation so that when you reference those fields you can get easy access to the dictionaries without any need to re-parse XML or anything.

I imagine you could also explore writing a custom field definition that would do a similar thing for other serialization formats, although I'm not sure how useful that would be.

Or you could simply refactor your model to take advantage of ManyToMany fields. You can create a model for a generic key,value pair, then on your primary model you would have a M2M reference to that generic key,value model. In that way you could leverage more of the Django ORM to reference data, etc.

shawnr
Very good answer presenting many options, thanks!
slypete
+1 for me typing a long reply, realizing that you said it better and clearer, and then deleting said reply.
sixthgear
This taught me something I didn't know that will be very useful!
Technical Bard
A: 

There is a XML field available: http://docs.djangoproject.com/en/dev/ref/models/fields/#xmlfield

But it would force you to do extra parsing on the resulting query. (Which I think you'll have to do to some degree...)

I've considered just dumping a list, unicode(mycolumnlist), into a single char field and having just a set number of indexed charfields after that like:

class DumbFlexModel(models.Model):
    available_fields = models.CharField()
    field1 = models.CharField()
    field2 = models.CharField()
    field3 = models.CharField()
    ...

That way you could at least perform a contains query on available_fields to filter results to only those with the field your trying to get vaules for, but then the position is arbitrary, so you'd still have to go through each result and process available_fields get get the position of the value.

Or maybe dumping a serialized (pickle.dumps()) list of dictionaries?

I'm interested in other suggestions.

monkut