Closed elmbeech closed 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.
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
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.
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.
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
thank you very much @raphaelmerx. I would not have had any idea where to start digging into this. you are wizard!
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
Django 1.10.7 is out, which includes the bug fix. Can you please test that this solves your bug @elmbeech?
@ 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
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:
appbrick/models.py
class Brick( models.Model ): sample = models.ForeignKey( Sample, default="not_yet_specified")
appbrick/admin.py
class BrickForm(forms.ModelForm): class Meta: model = Brick fields = ["sample"] widgets = {"sample": AutoComboboxSelectWidget(SampleLookup),}
class BrickAdmin( admin.ModelAdmin ): form = BrickForm
admin.site.register(Brick, BrickAdmin)
appsample/lookups.py class SampleLookup(ModelLookup): model = Sample search_fields = ('annot_id__icontains',) registry.register(SampleLookup)