Use django-filters on multiple modelsDoes Django scale?django - inlineformset_factory with more than one ForeignKeyShow information of subclass in list_display djangoCatch multiple exceptions in one line (except block)how to use select_related in templates Django?Products catalogue: filter by parametersfilter json data from Django modelHow to expose some specific fields of model_b based on a field of model_a?How to define Mode with generic ForeignKey in DjangoHow to check if Django Signal works?

What happens to matryoshka Mordenkainen's Magnificent Mansions?

What word means "to make something obsolete"?

Was there ever a Kickstart that took advantage of 68020+ instructions that would work on an A2000?

What happens to the Time Stone

Selecting a secure PIN for building access

What is the most remote airport from the center of the city it supposedly serves?

What is a "listed natural gas appliance"?

Do I really need diodes to receive MIDI?

Catholic vs Protestant Support for Nazism in Germany

Is Cola "probably the best-known" Latin word in the world? If not, which might it be?

Transpose of product of matrices

Enumerate Derangements

How can I close a gap between my fence and my neighbor's that's on his side of the property line?

Can Ghost kill White Walkers or Wights?

Was Unix ever a single-user OS?

What are the spoon bit of a spoon and fork bit of a fork called?

Would a 1/1 token with persist dying trigger on death effects a second time?

In a vacuum triode, what prevents the grid from acting as another anode?

Point of the the Dothraki's attack in GoT S8E3?

Why is `abs()` implemented differently?

Missed the connecting flight, separate tickets on same airline - who is responsible?

How did Arya get her dagger back from Sansa?

My ID is expired, can I fly to the Bahamas with my passport?

Alias to source .bashrc after it's been edited?



Use django-filters on multiple models


Does Django scale?django - inlineformset_factory with more than one ForeignKeyShow information of subclass in list_display djangoCatch multiple exceptions in one line (except block)how to use select_related in templates Django?Products catalogue: filter by parametersfilter json data from Django modelHow to expose some specific fields of model_b based on a field of model_a?How to define Mode with generic ForeignKey in DjangoHow to check if Django Signal works?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I'm trying to create a search capability in my Django project that will filter multiple models (currently 3). It works for a single model and an additional model tied by a foreign key. But, when I added a dropdown menu for the third model not containing a direct reference to the initial model, I got an error saying they keyword was unavailable (because it wasn't looking in the model):




Cannot resolve keyword 'rooms' into field. Choices are: DateBegin, FAN, IS, Location, PI, holdings, id




Models:



#models.py
class rooms(models.Model):
ContainerLocation = models.CharField(max_length=100, blank=True, null=True)
Database = models.CharField(max_length=100, blank=True, null=True)
Name = models.CharField(max_length=100, blank=True, null=True)
Datatype = models.CharField(max_length=100, blank=True, null=True)

class holdings(models.Model):
Contents = models.CharField(max_length=700, blank=True, null=True, default='No description')
FAN = models.ForeignKey('surveys', on_delete=models.SET_NULL, blank=True, null=True)
Database = models.ForeignKey('rooms', on_delete=models.SET_NULL, blank=True, null=True)
...(more fields)...

class surveys(models.Model):
FAN = models.SlugField(max_length=100, blank=True, null=True)
PI = models.CharField(max_length=100, blank=True, null=True)
IS = models.CharField(max_length=100, blank=True, null=True)
DateBegin = models.DateField(blank=True, null=True)
Location = models.CharField(max_length=200, blank=True, null=True)


Filter:



#filters.py
from django import forms
from datalibrary.models import surveys, rooms, holdings
import django_filters

class MultiFilter(django_filters.FilterSet):
FAN = django_filters.CharFilter(lookup_expr='icontains', distinct=True)
PI = django_filters.CharFilter(lookup_expr='icontains', distinct=True)
Location = django_filters.CharFilter(lookup_expr='icontains', distinct=True)
Contents = django_filters.CharFilter(field_name='holdings__Contents', lookup_expr='icontains', label='Contents', distinct=True)
Room = django_filters.ModelChoiceFilter(queryset=rooms.objects.all(), label='Room', distinct=True)

class Meta:
model = surveys
fields = ['FAN', 'PI', 'Location', 'Contents', 'Room']


Views:



#views.py
def search(request):
multifilter = MultiFilter(request.GET, queryset=surveys.objects.all())
return render(request, 'search_results.html', 'filter': multifilter)


Is it possible to build a filter with django-filters that searches multiple models? Can I create a merged queryset or something like that? I tried adding a list to the filters Meta (e.g. model = [surveys, rooms, holdings]) but that obviously doesn't work.



If django-filters can't do it, are there other options for searching multiple models in Django?










share|improve this question






























    1















    I'm trying to create a search capability in my Django project that will filter multiple models (currently 3). It works for a single model and an additional model tied by a foreign key. But, when I added a dropdown menu for the third model not containing a direct reference to the initial model, I got an error saying they keyword was unavailable (because it wasn't looking in the model):




    Cannot resolve keyword 'rooms' into field. Choices are: DateBegin, FAN, IS, Location, PI, holdings, id




    Models:



    #models.py
    class rooms(models.Model):
    ContainerLocation = models.CharField(max_length=100, blank=True, null=True)
    Database = models.CharField(max_length=100, blank=True, null=True)
    Name = models.CharField(max_length=100, blank=True, null=True)
    Datatype = models.CharField(max_length=100, blank=True, null=True)

    class holdings(models.Model):
    Contents = models.CharField(max_length=700, blank=True, null=True, default='No description')
    FAN = models.ForeignKey('surveys', on_delete=models.SET_NULL, blank=True, null=True)
    Database = models.ForeignKey('rooms', on_delete=models.SET_NULL, blank=True, null=True)
    ...(more fields)...

    class surveys(models.Model):
    FAN = models.SlugField(max_length=100, blank=True, null=True)
    PI = models.CharField(max_length=100, blank=True, null=True)
    IS = models.CharField(max_length=100, blank=True, null=True)
    DateBegin = models.DateField(blank=True, null=True)
    Location = models.CharField(max_length=200, blank=True, null=True)


    Filter:



    #filters.py
    from django import forms
    from datalibrary.models import surveys, rooms, holdings
    import django_filters

    class MultiFilter(django_filters.FilterSet):
    FAN = django_filters.CharFilter(lookup_expr='icontains', distinct=True)
    PI = django_filters.CharFilter(lookup_expr='icontains', distinct=True)
    Location = django_filters.CharFilter(lookup_expr='icontains', distinct=True)
    Contents = django_filters.CharFilter(field_name='holdings__Contents', lookup_expr='icontains', label='Contents', distinct=True)
    Room = django_filters.ModelChoiceFilter(queryset=rooms.objects.all(), label='Room', distinct=True)

    class Meta:
    model = surveys
    fields = ['FAN', 'PI', 'Location', 'Contents', 'Room']


    Views:



    #views.py
    def search(request):
    multifilter = MultiFilter(request.GET, queryset=surveys.objects.all())
    return render(request, 'search_results.html', 'filter': multifilter)


    Is it possible to build a filter with django-filters that searches multiple models? Can I create a merged queryset or something like that? I tried adding a list to the filters Meta (e.g. model = [surveys, rooms, holdings]) but that obviously doesn't work.



    If django-filters can't do it, are there other options for searching multiple models in Django?










    share|improve this question


























      1












      1








      1








      I'm trying to create a search capability in my Django project that will filter multiple models (currently 3). It works for a single model and an additional model tied by a foreign key. But, when I added a dropdown menu for the third model not containing a direct reference to the initial model, I got an error saying they keyword was unavailable (because it wasn't looking in the model):




      Cannot resolve keyword 'rooms' into field. Choices are: DateBegin, FAN, IS, Location, PI, holdings, id




      Models:



      #models.py
      class rooms(models.Model):
      ContainerLocation = models.CharField(max_length=100, blank=True, null=True)
      Database = models.CharField(max_length=100, blank=True, null=True)
      Name = models.CharField(max_length=100, blank=True, null=True)
      Datatype = models.CharField(max_length=100, blank=True, null=True)

      class holdings(models.Model):
      Contents = models.CharField(max_length=700, blank=True, null=True, default='No description')
      FAN = models.ForeignKey('surveys', on_delete=models.SET_NULL, blank=True, null=True)
      Database = models.ForeignKey('rooms', on_delete=models.SET_NULL, blank=True, null=True)
      ...(more fields)...

      class surveys(models.Model):
      FAN = models.SlugField(max_length=100, blank=True, null=True)
      PI = models.CharField(max_length=100, blank=True, null=True)
      IS = models.CharField(max_length=100, blank=True, null=True)
      DateBegin = models.DateField(blank=True, null=True)
      Location = models.CharField(max_length=200, blank=True, null=True)


      Filter:



      #filters.py
      from django import forms
      from datalibrary.models import surveys, rooms, holdings
      import django_filters

      class MultiFilter(django_filters.FilterSet):
      FAN = django_filters.CharFilter(lookup_expr='icontains', distinct=True)
      PI = django_filters.CharFilter(lookup_expr='icontains', distinct=True)
      Location = django_filters.CharFilter(lookup_expr='icontains', distinct=True)
      Contents = django_filters.CharFilter(field_name='holdings__Contents', lookup_expr='icontains', label='Contents', distinct=True)
      Room = django_filters.ModelChoiceFilter(queryset=rooms.objects.all(), label='Room', distinct=True)

      class Meta:
      model = surveys
      fields = ['FAN', 'PI', 'Location', 'Contents', 'Room']


      Views:



      #views.py
      def search(request):
      multifilter = MultiFilter(request.GET, queryset=surveys.objects.all())
      return render(request, 'search_results.html', 'filter': multifilter)


      Is it possible to build a filter with django-filters that searches multiple models? Can I create a merged queryset or something like that? I tried adding a list to the filters Meta (e.g. model = [surveys, rooms, holdings]) but that obviously doesn't work.



      If django-filters can't do it, are there other options for searching multiple models in Django?










      share|improve this question
















      I'm trying to create a search capability in my Django project that will filter multiple models (currently 3). It works for a single model and an additional model tied by a foreign key. But, when I added a dropdown menu for the third model not containing a direct reference to the initial model, I got an error saying they keyword was unavailable (because it wasn't looking in the model):




      Cannot resolve keyword 'rooms' into field. Choices are: DateBegin, FAN, IS, Location, PI, holdings, id




      Models:



      #models.py
      class rooms(models.Model):
      ContainerLocation = models.CharField(max_length=100, blank=True, null=True)
      Database = models.CharField(max_length=100, blank=True, null=True)
      Name = models.CharField(max_length=100, blank=True, null=True)
      Datatype = models.CharField(max_length=100, blank=True, null=True)

      class holdings(models.Model):
      Contents = models.CharField(max_length=700, blank=True, null=True, default='No description')
      FAN = models.ForeignKey('surveys', on_delete=models.SET_NULL, blank=True, null=True)
      Database = models.ForeignKey('rooms', on_delete=models.SET_NULL, blank=True, null=True)
      ...(more fields)...

      class surveys(models.Model):
      FAN = models.SlugField(max_length=100, blank=True, null=True)
      PI = models.CharField(max_length=100, blank=True, null=True)
      IS = models.CharField(max_length=100, blank=True, null=True)
      DateBegin = models.DateField(blank=True, null=True)
      Location = models.CharField(max_length=200, blank=True, null=True)


      Filter:



      #filters.py
      from django import forms
      from datalibrary.models import surveys, rooms, holdings
      import django_filters

      class MultiFilter(django_filters.FilterSet):
      FAN = django_filters.CharFilter(lookup_expr='icontains', distinct=True)
      PI = django_filters.CharFilter(lookup_expr='icontains', distinct=True)
      Location = django_filters.CharFilter(lookup_expr='icontains', distinct=True)
      Contents = django_filters.CharFilter(field_name='holdings__Contents', lookup_expr='icontains', label='Contents', distinct=True)
      Room = django_filters.ModelChoiceFilter(queryset=rooms.objects.all(), label='Room', distinct=True)

      class Meta:
      model = surveys
      fields = ['FAN', 'PI', 'Location', 'Contents', 'Room']


      Views:



      #views.py
      def search(request):
      multifilter = MultiFilter(request.GET, queryset=surveys.objects.all())
      return render(request, 'search_results.html', 'filter': multifilter)


      Is it possible to build a filter with django-filters that searches multiple models? Can I create a merged queryset or something like that? I tried adding a list to the filters Meta (e.g. model = [surveys, rooms, holdings]) but that obviously doesn't work.



      If django-filters can't do it, are there other options for searching multiple models in Django?







      python django python-3.x django-2.1 django-filters






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 10 at 20:14







      Bird

















      asked Mar 22 at 21:28









      BirdBird

      7223934




      7223934






















          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55308022%2fuse-django-filters-on-multiple-models%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55308022%2fuse-django-filters-on-multiple-models%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript