Categories
Python Answers

How to perform query filtering in Python Django templates?

Sometimes, we want to perform query filtering in Python Django templates.

In this article, we’ll look at how to perform query filtering in Python Django templates.

How to perform query filtering in Python Django templates?

To perform query filtering in Python Django templates, we can create a template tag that calls filter.

For instance, we write

@register.filter
def in_category(things, category):
    return things.filter(category=category)

to create the in_category tag function that calls things.filter to filter by category.

Then we write

{% for category in categories %}
  {% for thing in things|in_category:category %}
    {{ thing }}
  {% endfor %}
{% endfor %}

in our template to use the in_category tag with the category as the value of the category parameter.

Conclusion

To perform query filtering in Python Django templates, we can create a template tag that calls filter.

Categories
Python Answers

How to pass extra arguments to serializer Class in Python Django Rest Framework?

Sometimes, we want to pass extra arguments to serializer Class in Python Django Rest Framework.

In this article, we’ll look at how to pass extra arguments to serializer Class in Python Django Rest Framework.

How to pass extra arguments to serializer Class in Python Django Rest Framework?

To pass extra arguments to serializer Class in Python Django Rest Framework, we set the context argument to a dict with the values we want to pass to the serializer.

For instance, we write

my_objects = MyModelSerializer(
    input_collection, 
    many=True, 
    context={'user_id': request.user.id}
).data

to create a MyModelSerializer by calling MyModelSerializer with the context argument set to a dict.

Then in our serializer class, we write

class MyModelSerializer(serializers.ModelSerializer):
    #...

    is_my_object = serializers.SerializerMethodField('_is_my_find')
    #...

    def _is_my_find(self, obj):
        user_id = self.context.get("user_id")
        if user_id:
            return user_id in obj.my_objects.values_list("user_id", flat=True)
        return False

to get the context values with self.context.get.

Conclusion

To pass extra arguments to serializer Class in Python Django Rest Framework, we set the context argument to a dict with the values we want to pass to the serializer.

Categories
Python Answers

How to calculate the sum of the column values through query with Python Django?

Sometimes, we want to calculate the sum of the column values through query with Python Django.

In this article, we’ll look at how to calculate the sum of the column values through query with Python Django.

How to calculate the sum of the column values through query with Python Django?

To calculate the sum of the column values through query with Python Django, we can use the aggregate method.

For instance, we write

from django.db.models import Sum

ItemPrice.objects.aggregate(Sum('price'))

to call aggregate with the Sum('price') to create an aggregation result by summing up the price column value of each row.

Conclusion

To calculate the sum of the column values through query with Python Django, we can use the aggregate method.

Categories
Python Answers

How to redirect logged in users from login page with Python Django?

Sometimes, we want to redirect logged in users from login page with Python Django.

In this article, we’ll look at how to redirect logged in users from login page with Python Django.

How to redirect logged in users from login page with Python Django?

To redirect logged in users from login page with Python Django, we can set the redirect_authenticated_user argument when we call as_view.

For instance, we write

from django.contrib.auth import views as auth_views
from django.urls import path

urlpatterns = [
    path('login/', auth_views.LoginView.as_view(redirect_authenticated_user=True), name='login'),
]

to call auth_views.LoginView.as_view with the redirect_authenticated_user to True to redirect users from the login page to the destination once the user is logged in.

Conclusion

To redirect logged in users from login page with Python Django, we can set the redirect_authenticated_user argument when we call as_view.

Categories
Python Answers

How to split models.py into several files with Python Django?

Sometimes, we want to split models.py into several files with Python Django.

In this article, we’ll look at how to split models.py into several files with Python Django.

How to split models.py into several files with Python Django?

To split models.py into several files with Python Django, we can put the model files in the models folder in our app folder.

And we add the __init__.py file into the root of the models folder.

For instance, we can have this structure in our app

app1/
    views.py
    __init__.py
    models/
        __init__.py
        model1.py
        model2.py

We add __init__.py into app1/models and we add the model class files inside app1/models.

Conclusion

To split models.py into several files with Python Django, we can put the model files in the models folder in our app folder.

And we add the __init__.py file into the root of the models folder.