Opened 9 years ago

Closed 9 years ago

#25795 closed Bug (duplicate)

formfield_for_foreignkey() and limit_choices_to() dont work as expected

Reported by: Olivier Pons Owned by: nobody
Component: Uncategorized Version: 1.8
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Change History (1)

comment:1 by Tim Graham, 9 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #23595

Note: See TracTickets for help on using tickets.
Back to Top