On Wed, Sep 24, 2008 at 3:25 PM, J Cook <[EMAIL PROTECTED]> wrote:
> 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:)
>
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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---