Re: Using filter for serialized model.Field's

2011-12-23 Thread jrief
Hi,
as a workaround, I added an additional column (aka CharField) to store the 
hash of that JSON string. Then only the hashes have to be compared. Sure, 
this is not an elegant solution, as it adds redundant data to your database.

If I would write SQL by hand, I could compare the JSON-string using the 
build-in md5() function. But this is not portable, since it would use two 
different hashing implementations, one in Python and one in MySQL.

I did not test to restrict the query using '__contains', but I tested with 
'__exact' which in my opinion is more appropriate - but this did not help. 
So, the hashing field is probably the best workable solution.

Thanks for your answer.

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



Re: Using filter for serialized model.Field's

2011-12-22 Thread Matt Schinckel
I'm guessing it's got to do with testing for equality. It will be 
restricted to how the field prepares data for querying the database, and 
(speaking as the maintainer of JSONField) you need to be very careful with 
querying on json fields, as the data is stored as a string, so changes 
about how it serialises can affect query results.

You can restrict queries to `__contains` queries, and query for keys, but 
there is not really any way to have structured queries that exactly match a 
defined dict, for instance.

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



Using filter for serialized model.Field's

2011-12-22 Thread jrief
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.