Thursday, December 22, 2011

Using filter for serialized model.Field's

Hi,
I have i weird problem when using model fields JSONField and
PickledObjectField together with the filter function.

from jsonfield.fields import JSONField
from picklefield.fields import PickledObjectField

class Item(models.Model):
picklefield = PickledObjectField(null=True, blank=True)
jsonfield = JSONField(null=True, blank=True)

if I store a dict in this model

mydict = {'description': 'Hello', }
item1 = Item.objects.create(picklefield=mydict)
item1.save()
item2 = Item.objects.filter(picklefield=mydict)
item2.exists() # returns True, as expected

if however my dict looks like this

mydict = {'description': None, 'name': u'Color', 'id': 1L,
'option': 'red'}
...same code as above...
item2.exists() # returns False

Then I tested the same with JSONField. There, I also expect that item2
shall exists, but this function also returns False.

Then I tested with mydict as

mydict = [ 2, 3, 4 ]
item1 = Item.objects.create(jsonfield=mydict)
item1.save()
item2 = Item.objects.filter(jsonfield=mydict)
item2.exists() # returns True, as expected

BTW, this example also works with PickledObjectField.

I don't think its a bug in both implementations of JSONField and
PickledObjectField, because they always serialize to the same string.

Is this undefined behavior intentional and I missed to read some
documentation? How can I solve this, without having to serialize the
objects manually?

Any help is greatly appreciated.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate