简体   繁体   中英

How to store arbitrary number of fields in django model?

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM