mlavin / django-selectable

Tools and widgets for using/creating auto-complete selection widgets using Django and jQuery UI.
http://django-selectable.readthedocs.io/en/latest/
BSD 2-Clause "Simplified" License
129 stars 64 forks source link

djnago 1.10 AutoComboboxSelectWidget selects always models default, not what is really selected in the dropdown #179

Closed elmbeech closed 7 years ago

elmbeech commented 7 years ago

I use AutoComboboxSelectWidget to select e.g. the "Sample", linked by ForeignKey from an other model. with django 1.9 this works perfect. with django 1.10, it does not matter what I select in the drop down, when i press save always the default (here "not_yet_specified") will be truly selected.

Extracts form the source code:

raphaelmerx commented 7 years ago

@elmbeech I cannot reproduce your issue with Django==1.10.5. Please check out this branch that attempts to reproduce in the example folder that lives in the repo:

# core/models.py
class Farm(models.Model):
    name = models.CharField(max_length=200)
    owner = models.ForeignKey('auth.User', related_name='farms')
    fruit = models.ManyToManyField(Fruit)
# core/lookups.py
class OwnerLookup(ModelLookup):
    model = User
    search_fields = ('username__icontains', )
registry.register(OwnerLookup)
# core/admin.py
class FarmForm(forms.ModelForm):
    class Meta:
        model = Farm
        fields = ['owner']
        widgets = {'owner': selectable.AutoComboboxSelectWidget(OwnerLookup)}

class FarmAdmin(admin.ModelAdmin):
    form = FarmForm

Go to http://localhost:8000/admin/core/farm/2/change/, you can change the user without any issue.

elmbeech commented 7 years ago

hi raphaelmerx, thank you for coming back to this issue. i have at the moment an other prj burning. but I will come back to this issue in the end of this week!
stay tuned, bue

elmbeech commented 7 years ago

ok @raphaelmerx, please change your model like this:

# core/models.py
class Farm(models.Model):
    name = models.CharField(max_length=200)
    owner = models.ForeignKey('auth.User', default=1, related_name='farms')
    fruit = models.ManyToManyField(Fruit)

the default=1 causes the trouble.

raphaelmerx commented 7 years ago

Thanks @elmbeech, I can reproduce the bug by adding a default foreign key value. It is indeed a Django 1.10 specific bug. I'll look into fixing it and come back to you.

raphaelmerx commented 7 years ago

This is a Django bug, related to the wrapping of widgets in the admin. I submitted a pull request to Django to fix this issue. https://code.djangoproject.com/ticket/27905

elmbeech commented 7 years ago

thank you very much @raphaelmerx. I would not have had any idea where to start digging into this. you are wizard!

raphaelmerx commented 7 years ago

No prob @elmbeech! It was actually fun to trace this back.

My pull request has been merged into Django (master, 1.10.x and 1.11.x), the fix will be part of the Django 1.10.7 release: https://github.com/django/django/blob/master/docs/releases/1.10.7.txt

raphaelmerx commented 7 years ago

Django 1.10.7 is out, which includes the bug fix. Can you please test that this solves your bug @elmbeech?

elmbeech commented 7 years ago

@ raphaelmerx, I checked with django 1.10.7. works perfectly now! problem solved. I will close the issue. thank a bunch! was nice to see this bug getting tacked down. best, Elmar