#25795: formfield_for_foreignkey() and limit_choices_to() dont work as expected
-------------------------------+--------------------
Reporter: olivierpons | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------
Here's the idea: I want to have labels translated in my database. This is
a model that I called "TexteCourt" (which means "short text"). I'll have
tons of "TexteCourt", and some of them will be used by my models. So I've
made a field "groupe" thus:
{{{
class TexteCourt(BaseModel):
BULLE = u'0'
VIGNETTE = u'1'
BANDE = u'2'
PLANCHE = u'3'
BANDEDESSINEE = u'4'
groupe = models.CharField(max_length=1,
choices=[(BULLE, _(u'Bulle')),
(VIGNETTE, _(u'Vignette')),
(BANDE, _(u'Bande')),
(PLANCHE, _(u'Planche')),
(BANDEDESSINEE, _(u'Bande
dessinée'))],
default=BULLE)
etiquette = models.CharField(max_length=200)
}}}
Now I have all other models, but I'll just give you BandeDessinee:
class BandeDessinee(BaseModel): it has a "title" that is a "TexteCourt". I
want to limit the choice of the title to "groupe". Easy, just use
"limit_choices_to" like that:
{{{
class BandeDessinee(BaseModel):
titre = models.ForeignKey(TexteCourt, blank=True,
limit_choices_to={
'groupe': TexteCourt.BANDEDESSINEE
})
}}}
The problem is in the admin interface. When I try to edit a BandeDessinee,
the field "titre" is filtered. Ok. But there's the "plus" sign that allows
me to add a new title = ModelForm TexteCourt.
- First problem: the form is not filtered so I can create a TexteCourt of
''another'' group which is not normal
- Second problem, and '''this is a bug''': if I create a TexteCourt that
is ''not'' of the groupe "BANDEDESSINEE", it's added to the choices! It
should not even appear!
I've tried another solution: I tried to make my own filter like on another
model that works the same way: Bande. In the admin.py file :
Here's my model:
{{{
@python_2_unicode_compatible
class Bande(BaseModel):
description = models.ForeignKey(TexteCourt, blank=True,
limit_choices_to={
'groupe': TexteCourt.BANDE
})
vignettes = models.ManyToManyField(Vignette, blank=True)
def __str__(self):
return self.description
class Meta(BaseModel.Meta):
verbose_name = _(u"Bande")
}}}
And here's what I did in admin.py:
{{{
class BandeAdmin(admin.ModelAdmin):
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == 'description':
kwargs["queryset"] = TexteCourt.objects.filter(
groupe__exact=TexteCourt.BANDE
)
return super(BandeAdmin, self).formfield_for_foreignkey(
db_field, request, **kwargs
)
admin.site.register(Bande, BandeAdmin)
}}}
Those problems are still there: in admin, from "Bande" form edition I can
create a TexteCourt that is '''not''' part of the filter, and if I refresh
the "Bande" form edition, it disappear.
--
Ticket URL: <https://code.djangoproject.com/ticket/25795>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/054.c19ee855a1b3cb92e213c9001c659d8e%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.