T
T
trytrytry2014-09-24 21:27:23
Django
trytrytry, 2014-09-24 21:27:23

How to sort fields?

I need to sort notes by creation date, by category, and so on. tell me how to do it right?
a method that simply displays all the notes looks like this

def index(request):    
    note = Note.objects.filter(user=request.user.id)        
    return render(request, 'note/index.html',
                    {                    
                    'note' : note,                       
                    }					
                    )

what should I do now? which is easier to implement? can create buttons for each sorting, but how then to process everything in views?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
alternativshik, 2014-09-25
@alternativshik

Well, probably it would be necessary to add a date, a category to the model, and then read the Dzhanga doc and see how to do sorting in it.

M
Mikhail Podgursky, 2014-09-25
@kmmbvnr

Try Django-filter
Generic, reusable application to alleviate writing some of the more mundane bits of view code. Specifically, it allows users to filter down a queryset based on a model's fields, displaying the form to let them do this.
https://django-filter.readthedocs.org/en/latest/us...

I
Ilya, 2014-10-02
@FireGM

It is possible to implement requests through GET, and in view through if / elif filtering or sorting branching:

def index(request):
    if request.GET['pub_date']:
        items = Note.objects.order_by('-pub_date')
    else:
        items = Note.objects.all()
return render(...)

You can do whatever checks you like.
Or, instead of pub_date, use a generalized variable, from which it will take a condition for filtering / sorting.

B
blackbb, 2014-12-02
@blackbb

I do it through filters, for example Publisher.objects.filter(name__contains="press")
Will return all objects named press. Instead of press, you insert the name of the variable that you pass from the template. Here, read djbook.ru/ch05s11.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question