Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error with ChoiceFiter and ManyToOneRel #30

Closed
tback opened this issue Jan 25, 2010 · 3 comments
Closed

Error with ChoiceFiter and ManyToOneRel #30

tback opened this issue Jan 25, 2010 · 3 comments

Comments

@tback
Copy link

tback commented Jan 25, 2010

Hi,

while trying to use django-filter in a project of mine I got stuck when I wanted to customize a FilterSet (i.e. provide a lookup type). Once I define a custom filter it loses the choices.

I tried to figure it out by myself, but unfortunately the metaprogramming prevented me from doing so.

This works:

    class FooFilter(django_filters.FilterSet):
        class Meta:
            model=Foo
            fields=('foofield','bar__language')

This doesn't (The language choices aren't displayed):

    class FooFilter(django_filters.FilterSet):
        bar__language=django_filters.ChoiceFilter() # <- look ma, no choices 
        class Meta:
            model=Foo
            fields=('foofield','bar__language')

Using ModelChoiceFilter doesn't (seem to) work either - it complains about
missing keyword somewhere.

Relevant parts of the test application:

    #models.py
    from django.db import models

    class Foo(models.Model):
        foofield=models.CharField(blank=True, max_length=100)

        def __unicode__(self):
            return self.foofield

    class Bar(models.Model):
        LANGUAGES=(('en','english'),
                   ('de','german'))
        foo=models.ForeignKey(Foo)
        language=models.CharField(max_length=2,choices=LANGUAGES)
        barfield = models.CharField(blank=True, max_length=100)

        def __unicode__(self):
            return self.barfield

        #class Meta:
        #    unique_together=(('foo','language'))

    #filters.py
    from models import *
    import django_filters

    class FooFilter(django_filters.FilterSet):
        bar__language=django_filters.ChoiceFilter()
        class Meta:
            model=Foo
            fields=('foofield','bar__language')

    class BarFilter(django_filters.FilterSet):
        class Meta:
            model=Bar
            fields=('foo__foofield',)

    #views.py
    from django.shortcuts import render_to_response
    from models import *
    from filters import *

    def index(request):
        f = FooFilter(request.GET, queryset=Foo.objects.all())
        return render_to_response('index.html',{'f':f})
@tshddx
Copy link

tshddx commented Sep 14, 2010

I've got the same problem. The relevant part of my trivial test model:

class Person(models.Model):
    # ...
    gender = models.CharField(max_length=1, choices=(('m', "Male"), ('f', "Female"), ('o', "Other")), blank=False)
    # ...

My Filter:

    class PersonFilter(django_filters.FilterSet):
        # The following one line breaks the gender widget:
        gender = django_filters.ChoiceFilter()
        class Meta:
            model = Person
            fields = ['gender',]

Without the gender = ... line above, the Filter renders and works perfectly, using a select element to show the options for gender. When I add in the line, which I assumed would just be making explicit what is being done behind the scenes, I still get the select element, but it's empty.

@tshddx
Copy link

tshddx commented Sep 14, 2010

This other issue looks relevant, but I can't get to a dev box to try it out right now:

http://github.com/alex/django-filter/issues#issue/25

nkryptic added a commit to nkryptic/django-filter that referenced this issue Jan 21, 2013
@nkryptic
Copy link
Contributor

I have a functional branch which attempts to address this:
https://github.com/nkryptic/django-filter/tree/declared-filter-with-field-choices

When a FilterSet is instantiated, it normally adds the defined model to each filter. I've added a loop over the declared filters which are a ChoiceFilter, MultipleChoiceFilter, ModelChoiceFilter or ModelMultipleChoiceFilter and added choices (extracted the same way they are for the undeclared filters) if they don't already have choices.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants