Answer the question
In order to leave comments, you need to log in
Django: queryset filters based on current url (currently selected site category)?
The essence of the problem
There is a data model, like `Organization` <- (fk) `Project` <- (fk) `Resource`. When a user selects an organization in the header, they are taken to the detail view, for example "/organization/1/details/". Now it is necessary that all links to projects and resources (for example, in the sidebar) display only information related to the selected organization, i.e. a filter would be added to the queryset, such as qs &= Q(project__organization=organization_id)
UPD1
That is, the choice of a project or organization is actually a change in the current display context. In the context of the selected organization, all links lead only to projects for that organization, and so on.
url(r'^(?:organization/(?P<organization_id>\d+)/)?projects/$', 'app.views.projects', name='projects'),
url(r'^(?:organization/(?P<organization_id>\d+)/)?(?:project/(?P<project_id>\d+)/)?resources/$', 'app.views.resources', name='resources'),
`{% url 'projects' org.id %}`
`{% url 'resources' org.id prj.id %}`
class FetchFiltersMiddleware(object):
def process_view(self, request, view_func, view_args, view_kwargs):
request.project_id = view_kwargs.get('project_id', 0) # или .pop(), тогда не надо переписывать аргументы вьюх
request.organization_id = view_kwargs.get('organization_id', 0)
Answer the question
In order to leave comments, you need to log in
Honestly, I did not understand the question, but I will insert five cents.
First you need to read the documentation .
Now in the project/organization we add the method:
@models.permalink
def get_absolute_url(self):
return ('projects', (), {'org_id': self.pk}) # именованные аргументы у вас позиционные
@models.permalink
def get_absolute_url(self):
return ('resources', (), {'org_id': self.org.pk, 'prj_id': self.pk})
{% for p in projects %}
{{ p.get_absolute_url }} # выведет /organization/1/details/2/
{% endfor %}
The session is the best option in my opinion. And at the expense of opening in another tab, it will spoil the navigation in the current one, so this is only after reloading the current tab.
Try like this:
organization = Organization.objects.get(id=organization_id)
projects = organization.organization_set.filter(...)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question