crucialfelix / django-ajax-selects

jQuery UI-powered auto-complete fields for ForeignKey, ManyToMany and text fields
Other
825 stars 248 forks source link

Update form with django ajax selects #143

Closed EdwinTSalcedo closed 8 years ago

EdwinTSalcedo commented 9 years ago

Hi everyone! I've run into with this problem, when I want to update an object i got : [u"'Edwin Salcedo Aliaga - Hospital SSU - La Paz' must be an integer"]. So I changed the next in my model:

class Expediente(models.Model): paciente = models.ForeignKey(Paciente,db_column='paciente') establecimiento = models.ForeignKey(Establecimiento, db_column='establecimiento')

class Meta:
    #managed = False
    db_table = 'expediente'
    verbose_name = "Expediente"
    verbose_name_plural = "Expedientes"
    unique_together = ('paciente', 'establecimiento',) 

def __unicode__(self):
    return "%s - %s"%(self.paciente, self.establecimiento)

return by return int(self.pk), but then i got this: TypeError: coercing to Unicode: need string or buffer, int found.

It looked like a paradox xD. So I changed the next, return by return str(self.id) and ids = [pk_type(pk.pk) for pk in ids] in the file /root/virtenvs/sirssu/local/lib/python2.7/site-packages/ajax_select/lookup_channel.py in my virtualenvs. Please tell me if there is another way to do it in order not to modify the library! Thanks for django ajax selects!

crucialfelix commented 9 years ago

What database are you using ?

The Django ORM should be converting to the correct type itself. Previously the ajax-selects widget was converting the type by just guessing what was correct, but we decided that this was not the responsibility of the widget.

where exactly are you trying to do this:

return int(self.pk)

and the other thing you changed. can you link to it with a URL on github ?

somewhere in here:

https://github.com/crucialfelix/django-ajax-selects/blob/develop/ajax_select%2Flookup_channel.py#L98

you can and should just subclass LookupChannel and then implement that method if your database does not use int pks and the django orm is not correctly converting it to unicode.

alexcasalboni commented 8 years ago

Hi @crucialfelix,

I think everything works fine as far as rendering, but I am having a related problem on save. I believe the reason is that AutoCompleteSelectField only takes care of checking that the db objects still exists on clean, but then it simply returns the fetched object.

Therefore, unless your model has a convenient str or unicode representation, you will save an arbitrary string into your database. Is there a way to avoid this and always save your model pk in the db?

EDIT: my bad, I was simply using the wrong ORM mapping (a CharField instead of a ForeignKey).

crucialfelix commented 8 years ago

yes it is supposed to return the object for the ForeignKey fields.

crucialfelix commented 8 years ago

stale, closing.