0
0
Djangoframework~5 mins

Search and ordering in Django - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the search_fields attribute in Django's admin?
The search_fields attribute allows you to specify which model fields should be searchable in the Django admin interface. It enables a search box that filters the list view based on the entered query.
Click to reveal answer
beginner
How do you enable ordering of model records in Django admin?
You can enable ordering by setting the <code>ordering</code> attribute in the model's <code>Meta</code> class or by using the <code>ordering</code> attribute in the admin class. This controls the default order of records shown.
Click to reveal answer
intermediate
What does the ordering attribute accept in Django models or admin?
It accepts a list or tuple of field names as strings. Prefixing a field name with a minus sign (-) orders it in descending order; otherwise, ascending order is used.
Click to reveal answer
intermediate
How can you add search functionality to a Django REST Framework API view?
You add the <code>SearchFilter</code> class to the view's <code>filter_backends</code> and specify <code>search_fields</code>. This enables searching by query parameters on specified fields.
Click to reveal answer
intermediate
What is the difference between search_fields and ordering_fields in Django REST Framework?
search_fields defines which fields can be searched using a query string, while ordering_fields defines which fields can be used to order the results. Both are used with filter backends.
Click to reveal answer
In Django admin, which attribute enables a search box to filter records?
Aordering
Bsearch_fields
Clist_filter
Dfilter_backends
How do you specify descending order for a field in Django model ordering?
AUse the field name with a minus sign (-)
BUse uppercase field name
CUse the field name with a plus sign (+)
DUse the field name in quotes
Which Django REST Framework filter backend enables search functionality?
ASearchFilter
BOrderingFilter
CDjangoFilterBackend
DLimitOffsetPagination
What does the ordering_fields attribute do in Django REST Framework?
ADefines fields that can be searched
BDefines pagination size
CDefines fields that can be ordered
DDefines fields that are hidden
Where do you set the default ordering of records in a Django model?
AIn the admin class using <code>search_fields</code>
BIn the model's <code>__init__</code> method
CIn the view using <code>filter_backends</code>
DIn the model's <code>Meta</code> class using <code>ordering</code>
Explain how to add search and ordering features to a Django admin interface.
Think about attributes in admin classes and model Meta.
You got /4 concepts.
    Describe how to implement search and ordering in a Django REST Framework API view.
    Focus on filter backends and query parameters.
    You got /4 concepts.