S
S
S0ulReaver2012-05-10 07:27:03
Django
S0ulReaver, 2012-05-10 07:27:03

What are the options for solving questions about filters in Django?

According to the TK, there are 2 tasks that need to be solved. How to solve it seems very vague.
1) The ability to select several elements in the Django admin panel, and somewhere show the result of processing the data of the selected elements (well, if it’s easier, show the sum of the fields of all the selected elements).
It seems that here somehow through Actions it is necessary to exit, but it is not clear where the summation result could be displayed.
2) You need to display data from the table in the Django admin panel for a certain period (i.e. from one date to another). Naturally, the dates must be entered by the user. Sorting by months in Django is already there, but it’s not clear how and where to add a panel for entering dates, and subsequent filtering.
Can anyone suggest options? I am using Django 1.4 + admin_tools

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
marazmiki, 2012-05-10
@marazmiki

The ability to select several elements in the Django admin panel, and somewhere show the result of processing the data of the selected elements (well, if it’s easier, show the sum of the fields of all the selected elements).
It seems that here it is necessary to exit somehow through Actions, but it is not clear where the result of the summation could be displayed.

Just in case, I’ll clarify that actions are usually understood as working with selected objects on the server side . This means that when you select a list of models, the desired action and press the button, a request is made to the server and a function of the following type is processed:
def action_name(request, queryset):
    """
    В queryset попадут выбранные модели.
    """

If this function returns None, it redirects to the same page with the list of models. But it can also return an HttpResponse object, which means it will work like a regular view. In particular, in this function, you can calculate the required amount and display it on a separate page (more about admin actions ).
Purely subjectively, the method is not very suitable: it’s inconvenient - first select the desired models, then select the action, then press the button, the page will reload, the selected checkboxes will fly off ... I think it’s better to do calculations in JS. By analogy with a script that shows how many records are selected.
You need to display data from the table in the Django admin panel for a certain period (i.e. from one date to another). Naturally, the dates must be entered by the user. Sorting by months in Django is already there, but it’s not clear how and where to add a panel for entering dates, and subsequent filtering.

Writing your own filters in 1.4 is pretty well implemented ( example ). You can create your own filter class with your own template, in which the "from" and "to" fields are output by date pickers. This panel will be located in the standard place for filters (on the right, in the gray column). I think it's quite a solution.

M
marazmiki, 2012-05-10
@marazmiki

Templates can not be touched, everything has already been invented before us:

class MyAppAdmin(admin.ModelAdmin):
    ...
    class Meta:
        js = ['/static/my_app/js/sum_fields.js']

C
Cyxapeff, 2012-05-10
@Cyxapeff

The second question is great to google djangosnippets.org/snippets/2675/ if you don't want to write your own.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question