MySQL ENUM and SET fields - can Django handle them?

1,038 views
Skip to first unread message

J Cook

unread,
Sep 24, 2008, 12:52:50 PM9/24/08
to Django users
I have a legacy database with a table that has many ENUM and SET fields
- it seems that Django's models cannot handle these field types - or can
they? I would rather not modify the table if I can get it to work as is.
Is this what CommaSeparatedIntegerField is for?

Google has not been my friend since I cannot really find anything with
Django MySQL ENUM and SET fields that explains one way or the other.
Maybe this should be addressed on
http://docs.djangoproject.com/en/dev/ref/databases/ so it could help
others with this problem.

TIA,

Justin

Karen Tracey

unread,
Sep 24, 2008, 1:28:49 PM9/24/08
to django...@googlegroups.com

I too started with a legacy MySQL database that uses ENUM and SET fields. 

ENUM is easy, in the Django model just use a CharField with choices (http://docs.djangoproject.com/en/dev/topics/db/models/#field-options) set to match your possible values.  (Though there is no help here provided by the introspection of inspectdb, so you have to set this up manually.)

SET as I recall required a little more work.  It's also a CharField, but required some translation from the comma-delimited string value passed to/from the database layer to the Python set type I wanted to use for manipulating the data in my Python code.  If I were writing the code today I'd probably look into creating a custom field (http://docs.djangoproject.com/en/dev/howto/custom-model-fields/) for a set, but back when I had to do this it was not so easy to write custom fields, so I did not go that route (and I have not had to revisit the code, so it stays as originally written despite there now maybe being a better way to do it...if it's not broken I'm not inclined to 'fix' it).

You definitely should not have to modify the table (if Django had required that I do that, I would not have used Django). 

Karen

J Cook

unread,
Sep 24, 2008, 3:25:48 PM9/24/08
to django...@googlegroups.com

Karen Tracey wrote:

>
> SET as I recall required a little more work. It's also a CharField, but
> required some translation from the comma-delimited string value passed
> to/from the database layer to the Python set type I wanted to use for
> manipulating the data in my Python code. If I were writing the code
> today I'd probably look into creating a custom field
> (http://docs.djangoproject.com/en/dev/howto/custom-model-fields/) for a
> set, but back when I had to do this it was not so easy to write custom
> fields, so I did not go that route (and I have not had to revisit the
> code, so it stays as originally written despite there now maybe being a
> better way to do it...if it's not broken I'm not inclined to 'fix' it).

Is there any way you could post an example of what works for you with a
SET field? Pretty please:)

Justin

Karen Tracey

unread,
Sep 24, 2008, 4:59:07 PM9/24/08
to django...@googlegroups.com

I have a model representing a crossword puzzle, one field tracks letters not used in the puzzle.  On the MySQL side the field is defined as a "set('A','B', ... ,'Z')".  On the Django side it's a CharField.  To my Django app code it looks like this:

>>> from crossword.models import Puzzles
>>> puz = Puzzles.objects.get(pk=222)
>>> puz.UnusedLetters
u'J,K,Q,X,Z'

To transform that value to a set I do:

>>> set_of_unused_letters = set(x for x in puz.UnusedLetters.split(','))
>>> set_of_unused_letters
set([u'Q', u'X', u'K', u'J', u'Z'])

To transform the set to a string value acceptable for saving in the database, I do:

>>> val_to_save = ','.join(x for x in set_of_unused_letters)
>>> val_to_save
u'Q,X,K,J,Z'
>>> puz.UnusedLetters = val_to_save
>>> puz.save()

Karen
Reply all
Reply to author
Forward
0 new messages