Giter Site home page Giter Site logo

Comments (6)

rpkilby avatar rpkilby commented on May 20, 2024

Hi @NotSqrt

cf https://docs.djangoproject.com/en/1.9/topics/db/models/#abstract-base-classes

Is there an equivalent for FilterSets ?

No, django-filter is a very similar abstraction to standard django forms and is in fact built on top of them. There are no abstract Forms, no abstract FilterSets.

Could you provide a more complete stack trace? That's not enough information to go off of.

from django-rest-framework-filters.

NotSqrt avatar NotSqrt commented on May 20, 2024

Here is a longer trace:

Traceback (most recent call last):
  File "mycode.py", line 34, in <module>
    class MessageMixin(FilterSet):
  File "env/rest_framework_filters/filterset.py", line 57, in __new__
    field = filterset.get_model_field(model, filter_.name)
  File "env/django_filters/utils.py", line 43, in get_model_field
    opts = model._meta
AttributeError: 'NoneType' object has no attribute '_meta'

when my code looks like:

class MessageMixin(FilterSet):
    common_field = AllLookupsFilter('field_name')

    # no model in Meta class, this is a mixin

class ActualFilter1(MessageMixin, FilterSet):
    # other filters
    class Meta:
        model = MyModel1

class ActualFilter2(MessageMixin, FilterSet):
    # other filters
    class Meta:
        model = MyModel2

The exception is raised at import time, so runserver or anythong else fails immediately.

If I simply do:

class MessageMixin(object):
    common_field = AllLookupsFilter('field_name')

class ActualFilter1(MessageMixin, FilterSet):
    # other filters
    class Meta:
        model = MyModel1

class ActualFilter2(MessageMixin, FilterSet):
    # other filters
    class Meta:
        model = MyModel2

the common_field filter is not usable.

Whereas for modelforms, it works perfectly to do:

class FormMixin(ModelForm):
    common_field = AllLookupsFilter('field_name')

    # no model in Meta

class ActualForm1(FormMixin):
    # other filters
    class Meta:
        model = MyModel1

class ActualForm2(FormMixin):
    # other filters
    class Meta:
        model = MyModel2

from django-rest-framework-filters.

NotSqrt avatar NotSqrt commented on May 20, 2024

In django's ModelFormMetaclass.__new__, there's a if opts.model: # If a model is defined, extract form fields from it. code branch.

from django-rest-framework-filters.

rpkilby avatar rpkilby commented on May 20, 2024

Ah, that makes more sense. Checking for the presence of the model is the right thing to do here.

from django-rest-framework-filters.

rpkilby avatar rpkilby commented on May 20, 2024

Alright, this should be fixed on master - it was a straightforward change.

from django-rest-framework-filters.

NotSqrt avatar NotSqrt commented on May 20, 2024

Great ! Thanks !

from django-rest-framework-filters.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.