django-ajax-selects doesn't appear to be able to cope with models where the primary key is not in integer.
The problem appears to be here:
File "/usr/lib/pymodules/python2.6/ajax_select/fields.py", line 65, in value_from_datadict
return long(got)
ValueError: invalid literal for long() with base 10: 'VR0001'
I also note similar code in AutoCompleteSelectMultipleWidget.
For the case of AutoCompleteSelectWidget - is the cast to long(...) required?
Fixing AutoCompleteSelectMultipleWidget might be harder, as any | characters in the string need to be quoted somehow.
Brian May
Comment #1
Posted on Sep 13, 2010 by Helpful Birdi looked into this a little bit. its no problem to change (and it should be changed).
value_from_datadict would return a string
then field.clean_value(value) should pass it to the Lookup who would then have the responsibility to convert from string to float for get_objects( ids ) if need be. hmmm. I think the query will automatically convert it.
but the reason I think the long() is in there is that django was considering the field to be changed every single time you save the form because it compares long to string and declares them different. yep. so there's an admin log every time. django bug ?
maybe its possible to query the field and determine what to cast it to.
value_from_datadict should return the correct value with the correct type.
Comment #2
Posted on Sep 13, 2010 by Helpful BirdFixing AutoCompleteSelectMultipleWidget might be harder, as any | characters in the string need to be quoted somehow.
this will change in the next version anyway as it will return a proper json response
Comment #3
Posted on Sep 13, 2010 by Swift CatMy first reaction was to wrap the cast in a try statement and if the exception occurs, just return the value without the cast.
However, this seems wrong. It could be a string value of "42" for example, in which case you really don't want to convert it to an integer.
How does ModelChoiceField deal with this issue? It appears ModelChoiceField is derived from ChoiceField, which uses the Select widget; however the Select widget doesn't override the value_from_datadict function from Widget:
def value_from_datadict(self, data, files, name):
"""
Given a dictionary of data and this widget's name, returns the value
of this widget. Returns None if it's not provided.
"""
return data.get(name, None)
So I am wondering if maybe the problem you have encountered in comment #1 no longer exists.
It would seem like the best solution would be to do whatever ModelChoiceField does, unless it does something dodgy - I can't see any evidence of that though.
Comment #4
Posted on Nov 23, 2010 by Happy HorseI patched the code slightly, but I am sure this is not the best solution:
def value_from_datadict(self, data, files, name):
got = data.get(name, None)
if got:
try:
long(got)
return long(got)
except:
return got
else:
return None
Works for now... but I am looking for a better solution!
Comment #5
Posted on Nov 28, 2010 by Swift CatAre you sure that this is required? See the comment in #3, Django itself just returns the raw data for ModelChoiceField, so I think it should be OK if you do the same thing.
Comment #6
Posted on Feb 27, 2012 by Swift CatHello,
Any progress on this bug?
Also where do you want people to send bug reports to now? I notice you have moved the project to github, however you haven't enabled issue tracking there.
Thanks
Brian May
Comment #7
Posted on Feb 27, 2012 by Swift CatOops, sorry, just noticed your note that issue tracking here is still active.
Comment #8
Posted on Feb 28, 2012 by Helpful BirdYeah I forked it from somebody else who had first put it on github. this means he has the issue tracker because he is the "original".
ah wait ! it IS just a setting. I've been moaning about this for months. if you fork then by default its off but you can still turn it on.
anyway I will look at this tomorrow, promise
Status: New
Labels:
Type-Defect
Priority-Medium