Reputation: 5184
This seems like such an obvious question that I'm absolutely certain it's been asked before, but unfortunately 'set' isn't a very good keyword for a search.
I'm trying to define a model that uses a 'set' datatype for one of it's member's -- think hours of the day and days of the week. There's a very limited set of data that can be put in there, but you can select none or all of the possible values. Something might be allowed on any day of the week, just weekends, or anything in between. (Edit: Technically I could manage this through a many-to-many relationship and then adding the days of the week and hours of the day classes, but that seems rather absurd given that they're so heavily fixed. Making the client add hours of the day / days of the week to the system is silly, and they actually need to be fixed for consumers of relevant APIs to properly parse them)
But I can't figure out how to reproduce MySQLs 'set' datatype in Django.
Upvotes: 1
Views: 925
Reputation: 33379
This module implements ArrayField, which is pretty close to what you are after.
https://github.com/niwibe/djorm-ext-pgarray
From the documentation:
class Register(models.Model):
name = models.CharField(max_length=200)
points = ArrayField(dbtype="int")
objects = ExpressionManager()
>>> Register.objects.create(points = [1,2,3,4])
<Register: Register object>
Here's how the ArrayField gets implemented:
https://github.com/niwibe/djorm-ext-pgarray/blob/master/djorm_pgarray/fields.py
Upvotes: 2
Reputation: 5184
I approached the problem from the wrong direction, search wise. Instead of searching for a 'set' datatype (which is simply possible because 'set' is also used in the sense of 'set the variable to foo'), I needed to look up multiple select options.
From there, there are plenty of references that point me to this code snippet, which is apparently the only option:
http://djangosnippets.org/snippets/1200/
Upvotes: 2