django-filter Defining filter on Detail View The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceHow to combine 2 or more querysets in a Django view?How do I do a not equal in Django queryset filtering?Does Django scale?What is wrong with my models.py?Radio buttons in django admindjango models request get id error Room matching query does not existDjango-Rest-Framework - How to serialize queryset from an unrelated model as nested serializerHow to expose some specific fields of model_b based on a field of model_a?Django Model Issue In Multilevel Inheritance.How to check if Django Signal works?

How did passengers keep warm on sail ships?

What was the last x86 CPU that did not have the x87 floating-point unit built in?

Is this wall load bearing? Blueprints and photos attached

Working through the single responsibility principle (SRP) in Python when calls are expensive

Is there a writing software that you can sort scenes like slides in PowerPoint?

How did the audience guess the pentatonic scale in Bobby McFerrin's presentation?

What force causes entropy to increase?

How to determine omitted units in a publication

Can the DM override racial traits?

Can I visit the Trinity College (Cambridge) library and see some of their rare books

Drawing arrows from one table cell reference to another

University's motivation for having tenure-track positions

why is the limit of this expression equal to 1?

How to read αἱμύλιος or when to aspirate

Do working physicists consider Newtonian mechanics to be "falsified"?

Button changing its text & action. Good or terrible?

What can I do if neighbor is blocking my solar panels intentionally?

Am I ethically obligated to go into work on an off day if the reason is sudden?

Is 'stolen' appropriate word?

Simulating Exploding Dice

Could an empire control the whole planet with today's comunication methods?

How to politely respond to generic emails requesting a PhD/job in my lab? Without wasting too much time

Does Parliament hold absolute power in the UK?

Why can't devices on different VLANs, but on the same subnet, communicate?



django-filter Defining filter on Detail View



The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceHow to combine 2 or more querysets in a Django view?How do I do a not equal in Django queryset filtering?Does Django scale?What is wrong with my models.py?Radio buttons in django admindjango models request get id error Room matching query does not existDjango-Rest-Framework - How to serialize queryset from an unrelated model as nested serializerHow to expose some specific fields of model_b based on a field of model_a?Django Model Issue In Multilevel Inheritance.How 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;








0















On my existing view I am trying to add filter view (by field "Sprint" in Issue - working in Issue model as foreign key). Below is my code:



#models.py


class Sprint(models.Model):
sprint_type = models.TextField(default='Sprint', editable=False)
name = models.TextField()
goal = models.TextField()
start_date = models.DateField()
end_date = models.DateField()
project = models.ForeignKey(Project, on_delete=models.CASCADE)

class Issue(models.Model):
ISSUE_PRIORITY = (
('Critical', 'Critical'),
('High', 'High'),
('Medium', 'Medium'),
('Low', 'Low'),
)
issue_type = models.TextField(default='Issue', editable=False)
issue_id = models.AutoField(primary_key=True)
# slug = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
date_created = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
project = models.ForeignKey(Project, on_delete=models.CASCADE)
title = models.CharField(max_length=128)
description = models.TextField(null=True, blank=True)
initiative = models.ForeignKey(Initiative, on_delete=models.CASCADE, null=True, blank=True)
epic = models.ForeignKey(Epic, null=True, blank=True, on_delete=models.CASCADE)
sprint = models.ForeignKey(Sprint, on_delete=models.CASCADE, null=True, blank=True)
priority = models.CharField(max_length=8, choices=ISSUE_PRIORITY, default='M')


I am using django-filter library.



#filters.py
import django_filters
from .models import Issue

class BacklogFilter(django_filters.FilterSet):
sprint = django_filters.CharFilter(lookup_expr='icontains')

class Meta:
model = Issue
fields = ['sprint']


Using class-based view, I defined:



class ProjectBacklogView(DetailView):
model = Project
template_name = 'project-backlog.html'
context_object_name = 'backlog'


@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
return ListView.dispatch(self, request, *args, **kwargs)

def get_queryset(self):
try:
self.filter = BacklogFilter(self.request.GET, queryset=Issue.objects.all())
return self.filter
except:
return Issue.objects.all()

def get_context_data(self, **kwargs):
context = super(ProjectBacklogView, self).get_context_data(**kwargs)
context['project'] = Project.objects.all()
context['issue'] = Issue.objects.all()
# context['epic'] = Epic.objects.all()
# context['initiative'] = Initiative.objects.all()
context['sprint'] = Sprint.objects.all()
context['filter_form'] = self.filter.form.as_table

return context


In urls.py:



path('<str:pk>/backlog/', ProjectBacklogView.as_view(), name='project-backlog'),
path('<str:pk>/backlog/(w+)/', ProjectBacklogView.as_view(), name='project-backlog-filter'),


And last not least, the template:



% extends 'base-project.html' %

% block content %

<div class="container-fluid">

<div id="simpleList" class="list-group">
% for issue in backlog.issue_set.all %
<div class="list-group-item">
<div style="float:left;"><strong><a href="% url 'issue-edit' issue.pk %"> issue.title </a></strong></div>
<div style="float: right;"><a href="#" class="btn btn-success btn-circle btn-sm"><i class="fas"> issue.remaining_estimate </i></a></div>
<br /><br /><hr style="border-top: dashed 1px;"></hr>
<div style="float: left;"><small>Assignee: <strong> issue.assignee </strong></small></div>
<div style="float: right;"><small>Initiative: <strong> issue.initiative </strong></small></div><br />
<div style="float: left;"><small>Priority: <strong> issue.priority </strong></small></div>
<div style="float: right;"><small>Epic: <strong> issue.epic </strong></small></div>

</div>
% endfor %
</div>

<!-- Filters form-->
<div>
<form action="" method="get" class="filter-form">
<table>
filter_form
</table>
<input type="submit" value="Szukaj" />
<a class="reset-form" href="% url 'project-backlog' project.pk %">Clean filters</a>
</form>
</div>
</div>


My issue is, that I receive error about "no attribute 'filter' in BacklogFilter. Also I am concerned about urls routing - my case is that I would like on currently existing view display user filtering form and after apply selected filter, limit the elements displayed on view accordingly to selected/applied filter. How can I achieve that? I read tons of manuals but always I find out case, that user has filtering form and after sending the form, user is redirected to completely different page :(



Many thanks in advance for your help, I know I have still a lot to learn..










share|improve this question




























    0















    On my existing view I am trying to add filter view (by field "Sprint" in Issue - working in Issue model as foreign key). Below is my code:



    #models.py


    class Sprint(models.Model):
    sprint_type = models.TextField(default='Sprint', editable=False)
    name = models.TextField()
    goal = models.TextField()
    start_date = models.DateField()
    end_date = models.DateField()
    project = models.ForeignKey(Project, on_delete=models.CASCADE)

    class Issue(models.Model):
    ISSUE_PRIORITY = (
    ('Critical', 'Critical'),
    ('High', 'High'),
    ('Medium', 'Medium'),
    ('Low', 'Low'),
    )
    issue_type = models.TextField(default='Issue', editable=False)
    issue_id = models.AutoField(primary_key=True)
    # slug = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    date_created = models.DateTimeField(auto_now_add=True)
    date_updated = models.DateTimeField(auto_now=True)
    project = models.ForeignKey(Project, on_delete=models.CASCADE)
    title = models.CharField(max_length=128)
    description = models.TextField(null=True, blank=True)
    initiative = models.ForeignKey(Initiative, on_delete=models.CASCADE, null=True, blank=True)
    epic = models.ForeignKey(Epic, null=True, blank=True, on_delete=models.CASCADE)
    sprint = models.ForeignKey(Sprint, on_delete=models.CASCADE, null=True, blank=True)
    priority = models.CharField(max_length=8, choices=ISSUE_PRIORITY, default='M')


    I am using django-filter library.



    #filters.py
    import django_filters
    from .models import Issue

    class BacklogFilter(django_filters.FilterSet):
    sprint = django_filters.CharFilter(lookup_expr='icontains')

    class Meta:
    model = Issue
    fields = ['sprint']


    Using class-based view, I defined:



    class ProjectBacklogView(DetailView):
    model = Project
    template_name = 'project-backlog.html'
    context_object_name = 'backlog'


    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
    return ListView.dispatch(self, request, *args, **kwargs)

    def get_queryset(self):
    try:
    self.filter = BacklogFilter(self.request.GET, queryset=Issue.objects.all())
    return self.filter
    except:
    return Issue.objects.all()

    def get_context_data(self, **kwargs):
    context = super(ProjectBacklogView, self).get_context_data(**kwargs)
    context['project'] = Project.objects.all()
    context['issue'] = Issue.objects.all()
    # context['epic'] = Epic.objects.all()
    # context['initiative'] = Initiative.objects.all()
    context['sprint'] = Sprint.objects.all()
    context['filter_form'] = self.filter.form.as_table

    return context


    In urls.py:



    path('<str:pk>/backlog/', ProjectBacklogView.as_view(), name='project-backlog'),
    path('<str:pk>/backlog/(w+)/', ProjectBacklogView.as_view(), name='project-backlog-filter'),


    And last not least, the template:



    % extends 'base-project.html' %

    % block content %

    <div class="container-fluid">

    <div id="simpleList" class="list-group">
    % for issue in backlog.issue_set.all %
    <div class="list-group-item">
    <div style="float:left;"><strong><a href="% url 'issue-edit' issue.pk %"> issue.title </a></strong></div>
    <div style="float: right;"><a href="#" class="btn btn-success btn-circle btn-sm"><i class="fas"> issue.remaining_estimate </i></a></div>
    <br /><br /><hr style="border-top: dashed 1px;"></hr>
    <div style="float: left;"><small>Assignee: <strong> issue.assignee </strong></small></div>
    <div style="float: right;"><small>Initiative: <strong> issue.initiative </strong></small></div><br />
    <div style="float: left;"><small>Priority: <strong> issue.priority </strong></small></div>
    <div style="float: right;"><small>Epic: <strong> issue.epic </strong></small></div>

    </div>
    % endfor %
    </div>

    <!-- Filters form-->
    <div>
    <form action="" method="get" class="filter-form">
    <table>
    filter_form
    </table>
    <input type="submit" value="Szukaj" />
    <a class="reset-form" href="% url 'project-backlog' project.pk %">Clean filters</a>
    </form>
    </div>
    </div>


    My issue is, that I receive error about "no attribute 'filter' in BacklogFilter. Also I am concerned about urls routing - my case is that I would like on currently existing view display user filtering form and after apply selected filter, limit the elements displayed on view accordingly to selected/applied filter. How can I achieve that? I read tons of manuals but always I find out case, that user has filtering form and after sending the form, user is redirected to completely different page :(



    Many thanks in advance for your help, I know I have still a lot to learn..










    share|improve this question
























      0












      0








      0








      On my existing view I am trying to add filter view (by field "Sprint" in Issue - working in Issue model as foreign key). Below is my code:



      #models.py


      class Sprint(models.Model):
      sprint_type = models.TextField(default='Sprint', editable=False)
      name = models.TextField()
      goal = models.TextField()
      start_date = models.DateField()
      end_date = models.DateField()
      project = models.ForeignKey(Project, on_delete=models.CASCADE)

      class Issue(models.Model):
      ISSUE_PRIORITY = (
      ('Critical', 'Critical'),
      ('High', 'High'),
      ('Medium', 'Medium'),
      ('Low', 'Low'),
      )
      issue_type = models.TextField(default='Issue', editable=False)
      issue_id = models.AutoField(primary_key=True)
      # slug = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
      date_created = models.DateTimeField(auto_now_add=True)
      date_updated = models.DateTimeField(auto_now=True)
      project = models.ForeignKey(Project, on_delete=models.CASCADE)
      title = models.CharField(max_length=128)
      description = models.TextField(null=True, blank=True)
      initiative = models.ForeignKey(Initiative, on_delete=models.CASCADE, null=True, blank=True)
      epic = models.ForeignKey(Epic, null=True, blank=True, on_delete=models.CASCADE)
      sprint = models.ForeignKey(Sprint, on_delete=models.CASCADE, null=True, blank=True)
      priority = models.CharField(max_length=8, choices=ISSUE_PRIORITY, default='M')


      I am using django-filter library.



      #filters.py
      import django_filters
      from .models import Issue

      class BacklogFilter(django_filters.FilterSet):
      sprint = django_filters.CharFilter(lookup_expr='icontains')

      class Meta:
      model = Issue
      fields = ['sprint']


      Using class-based view, I defined:



      class ProjectBacklogView(DetailView):
      model = Project
      template_name = 'project-backlog.html'
      context_object_name = 'backlog'


      @method_decorator(login_required)
      def dispatch(self, request, *args, **kwargs):
      return ListView.dispatch(self, request, *args, **kwargs)

      def get_queryset(self):
      try:
      self.filter = BacklogFilter(self.request.GET, queryset=Issue.objects.all())
      return self.filter
      except:
      return Issue.objects.all()

      def get_context_data(self, **kwargs):
      context = super(ProjectBacklogView, self).get_context_data(**kwargs)
      context['project'] = Project.objects.all()
      context['issue'] = Issue.objects.all()
      # context['epic'] = Epic.objects.all()
      # context['initiative'] = Initiative.objects.all()
      context['sprint'] = Sprint.objects.all()
      context['filter_form'] = self.filter.form.as_table

      return context


      In urls.py:



      path('<str:pk>/backlog/', ProjectBacklogView.as_view(), name='project-backlog'),
      path('<str:pk>/backlog/(w+)/', ProjectBacklogView.as_view(), name='project-backlog-filter'),


      And last not least, the template:



      % extends 'base-project.html' %

      % block content %

      <div class="container-fluid">

      <div id="simpleList" class="list-group">
      % for issue in backlog.issue_set.all %
      <div class="list-group-item">
      <div style="float:left;"><strong><a href="% url 'issue-edit' issue.pk %"> issue.title </a></strong></div>
      <div style="float: right;"><a href="#" class="btn btn-success btn-circle btn-sm"><i class="fas"> issue.remaining_estimate </i></a></div>
      <br /><br /><hr style="border-top: dashed 1px;"></hr>
      <div style="float: left;"><small>Assignee: <strong> issue.assignee </strong></small></div>
      <div style="float: right;"><small>Initiative: <strong> issue.initiative </strong></small></div><br />
      <div style="float: left;"><small>Priority: <strong> issue.priority </strong></small></div>
      <div style="float: right;"><small>Epic: <strong> issue.epic </strong></small></div>

      </div>
      % endfor %
      </div>

      <!-- Filters form-->
      <div>
      <form action="" method="get" class="filter-form">
      <table>
      filter_form
      </table>
      <input type="submit" value="Szukaj" />
      <a class="reset-form" href="% url 'project-backlog' project.pk %">Clean filters</a>
      </form>
      </div>
      </div>


      My issue is, that I receive error about "no attribute 'filter' in BacklogFilter. Also I am concerned about urls routing - my case is that I would like on currently existing view display user filtering form and after apply selected filter, limit the elements displayed on view accordingly to selected/applied filter. How can I achieve that? I read tons of manuals but always I find out case, that user has filtering form and after sending the form, user is redirected to completely different page :(



      Many thanks in advance for your help, I know I have still a lot to learn..










      share|improve this question














      On my existing view I am trying to add filter view (by field "Sprint" in Issue - working in Issue model as foreign key). Below is my code:



      #models.py


      class Sprint(models.Model):
      sprint_type = models.TextField(default='Sprint', editable=False)
      name = models.TextField()
      goal = models.TextField()
      start_date = models.DateField()
      end_date = models.DateField()
      project = models.ForeignKey(Project, on_delete=models.CASCADE)

      class Issue(models.Model):
      ISSUE_PRIORITY = (
      ('Critical', 'Critical'),
      ('High', 'High'),
      ('Medium', 'Medium'),
      ('Low', 'Low'),
      )
      issue_type = models.TextField(default='Issue', editable=False)
      issue_id = models.AutoField(primary_key=True)
      # slug = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
      date_created = models.DateTimeField(auto_now_add=True)
      date_updated = models.DateTimeField(auto_now=True)
      project = models.ForeignKey(Project, on_delete=models.CASCADE)
      title = models.CharField(max_length=128)
      description = models.TextField(null=True, blank=True)
      initiative = models.ForeignKey(Initiative, on_delete=models.CASCADE, null=True, blank=True)
      epic = models.ForeignKey(Epic, null=True, blank=True, on_delete=models.CASCADE)
      sprint = models.ForeignKey(Sprint, on_delete=models.CASCADE, null=True, blank=True)
      priority = models.CharField(max_length=8, choices=ISSUE_PRIORITY, default='M')


      I am using django-filter library.



      #filters.py
      import django_filters
      from .models import Issue

      class BacklogFilter(django_filters.FilterSet):
      sprint = django_filters.CharFilter(lookup_expr='icontains')

      class Meta:
      model = Issue
      fields = ['sprint']


      Using class-based view, I defined:



      class ProjectBacklogView(DetailView):
      model = Project
      template_name = 'project-backlog.html'
      context_object_name = 'backlog'


      @method_decorator(login_required)
      def dispatch(self, request, *args, **kwargs):
      return ListView.dispatch(self, request, *args, **kwargs)

      def get_queryset(self):
      try:
      self.filter = BacklogFilter(self.request.GET, queryset=Issue.objects.all())
      return self.filter
      except:
      return Issue.objects.all()

      def get_context_data(self, **kwargs):
      context = super(ProjectBacklogView, self).get_context_data(**kwargs)
      context['project'] = Project.objects.all()
      context['issue'] = Issue.objects.all()
      # context['epic'] = Epic.objects.all()
      # context['initiative'] = Initiative.objects.all()
      context['sprint'] = Sprint.objects.all()
      context['filter_form'] = self.filter.form.as_table

      return context


      In urls.py:



      path('<str:pk>/backlog/', ProjectBacklogView.as_view(), name='project-backlog'),
      path('<str:pk>/backlog/(w+)/', ProjectBacklogView.as_view(), name='project-backlog-filter'),


      And last not least, the template:



      % extends 'base-project.html' %

      % block content %

      <div class="container-fluid">

      <div id="simpleList" class="list-group">
      % for issue in backlog.issue_set.all %
      <div class="list-group-item">
      <div style="float:left;"><strong><a href="% url 'issue-edit' issue.pk %"> issue.title </a></strong></div>
      <div style="float: right;"><a href="#" class="btn btn-success btn-circle btn-sm"><i class="fas"> issue.remaining_estimate </i></a></div>
      <br /><br /><hr style="border-top: dashed 1px;"></hr>
      <div style="float: left;"><small>Assignee: <strong> issue.assignee </strong></small></div>
      <div style="float: right;"><small>Initiative: <strong> issue.initiative </strong></small></div><br />
      <div style="float: left;"><small>Priority: <strong> issue.priority </strong></small></div>
      <div style="float: right;"><small>Epic: <strong> issue.epic </strong></small></div>

      </div>
      % endfor %
      </div>

      <!-- Filters form-->
      <div>
      <form action="" method="get" class="filter-form">
      <table>
      filter_form
      </table>
      <input type="submit" value="Szukaj" />
      <a class="reset-form" href="% url 'project-backlog' project.pk %">Clean filters</a>
      </form>
      </div>
      </div>


      My issue is, that I receive error about "no attribute 'filter' in BacklogFilter. Also I am concerned about urls routing - my case is that I would like on currently existing view display user filtering form and after apply selected filter, limit the elements displayed on view accordingly to selected/applied filter. How can I achieve that? I read tons of manuals but always I find out case, that user has filtering form and after sending the form, user is redirected to completely different page :(



      Many thanks in advance for your help, I know I have still a lot to learn..







      django django-views django-filter






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 8 at 12:32









      yorgwarezyorgwarez

      126




      126






















          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%2f55063336%2fdjango-filter-defining-filter-on-detail-view%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%2f55063336%2fdjango-filter-defining-filter-on-detail-view%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

          1928 у кіно

          Захаров Федір Захарович

          Ель Греко