Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
How to show date from db in input filed in html - django
My small project is in django. When I add new record to the db everything is OK. One of the input fields is for the date and have type 'date' <input type="date" id="id_date_one" name="date_one" class=" form-control" required> It is working... sending date to the db. Problem is when I try to edit records again. In forms I see everything without this date. <input type="date" id="id_date_one" name="date_one" class=" form-control" required value="{{obj.date_one}}"> input field is empty when type of this field is 'date', but if I change type for 'text' the date is presented in input field normally. In db this column have type 'date' also. My question is: Why when input field have type 'date' is empty during edition the records and how can I change it? Thanks for any sugestions -
Django unable to authenticate a user with custom SSO
I'm using an API response to login to my Django application. The api redirects to google login. Based on the response The user is validated. I've added an authentication backend to validate the user, But my app doesn't recognise the login even though the api response is fine. Pretty new to Django, so can anyone suggest what am I missing here -
Pagination Output
Can someone please help me detect my error? For some reason my for loop is not generating the correct HTML output. This Is my view class CustomerListView(ListView): model = Customer template_name = 'customer/list.html' context_object_name = 'customers' paginate_by = 1 ordering = ['lastName'] This is my html {% if is_paginated %} {% if page_obj.has_previous %} <a class="btn btn-outline-info mb-4" href="?page=1">First</a> <a class="btn btn-outline-info mb-4" href="?page={{ page_obj.previous_page_number }}">Previous</a> {% endif %} {% for num in page_obj.paginator.page_range %} {% if page_obj.number == num %} <a class="btn btn-info mb-4" href="?page={{ num }}">{{ num }}</a> {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %} <a class="btn btn-outline-info mb-4" href="?page={{ num }}">{{ num }}</a> {% endif %} {% endfor %} {% if page_obj.has_next %} <a class="btn btn-outline-info mb-4" href="?page={{ page_obj.next_page_number }}">Next</a> <a class="btn btn-outline-info mb-4" href="?page={{ page_obj.paginator.num_pages }}">Last</a> {% endif %} {% endif %} Output -
Hide Objects List in Django Admin Delete Form
When deleting an item from the Django Admin page, the delete form shows: Summary Objects The Objects list shows all the objects and their related objects. The list is pretty huge and difficult to navigate when deleting multiple records. Is there a way to hide the "Objects" list from the delete form in Django? I have looked around, but the only option I have found is to override the template. Is it possible to achieve this through ModelAdmin? -
Add Load Spinner Across All Django Ajax Request
I am able to add a loading spinner for my custom pages, however, how do I add a loading spinner across the entire Django project whenever waiting for the response to a request, e.g. while waiting for the delete to complete, or adding an object. -
Profile matching query does not exist
I am making my first django website. I want to make user profile eddition option.I use a custom user model adm a profile model. forms.py: class UserEditForm(forms.ModelForm): class Meta: model = User fields = ( 'first_name', 'last_name', 'email', 'phone_number', ) class ProfileEditForm(forms.ModelForm): class Meta: model = Profile fields = ( 'bio', 'skills', 'birth', 'image', ) models.py class User(AbstractUser): first_name = models.CharField(_('first name'), max_length=150) last_name = models.CharField(_('last name'), max_length=150) email = models.EmailField(_('email address')) phone_number = models.CharField( _('Phone number'), max_length=11, blank=True, null=True) age = models.CharField(_('age'), max_length=3, blank=True, null=True) date_joined = models.DateTimeField(auto_now_add=True) class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.SET_NULL, null=True,blank=True) bio = models.TextField(max_length=1000, help_text='User bio') skills = models.ManyToManyField(Category, help_text='Choose Your Skills',blank=True) birth = models.DateField('Birth', null=True, blank=True) image = models.ImageField(upload_to='users/%Y/%m/%d/',blank=True) last_update = models.DateTimeField(auto_now=True) def __str__(self): return self.user.username admin.py class ProfileAdmin(admin.ModelAdmin): list_display=('user','birth','image') admin.site.register(Profile, ProfileAdmin) admin.site.register(User) first I used this view function but it returns error RelatedObjectDoesNotExist : User has no profile views.py def registration_view(request): if request.method == 'POST': user_form = RegisterForm(request.POST) if user_form.is_valid(): new_user = user_form.save(commit=False) new_user.set_password( user_form.cleaned_data['password']) new_user.save() profile = Profile.objects.create(user=new_user) return render(request,'proFile/register_done.html', {'new_user':new_user}) else: user_form = RegisterForm() return render(request,'proFile/register.html', {'user_form':user_form}) @login_required def edit_view(request): if request.method == 'POST': user_form = UserEditForm(instance=request.user, data=request.POST) profile_form = ProfileEditForm(instance=request.user.profile, data=request.POST, files=request.FILES) if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() else: user_form … -
Python, Django: how to get checked table rows into lists?
Good evening, I'm hoping someone knows an answer to my problem of getting all checked rows inside a table into a multidimensional list (after submit)? I know I can do something like this to get - for example - all the checked ids into a list: template: <form action="" method="post"> {% csrf_token %} <div class="table-wrapper"> <table class="table table-hover"> <thead> <tr> <th scope="col"></th> <th scope="col">id</th> <th scope="col">country</th> <th scope="col">continent</th> <th scope="col">...</th> </tr> </thead> <tbody> {% for item in data %} <tr> <td> <input type="checkbox" name="list_gap" value="{{ item.id }}"> </td> <td>{{ item.id}}</td> <td>{{ item.country }}</td> <td>{{ item.continent}}</td> <td>...</td> views.py if request.method == 'POST': checked_data = request.POST.getlist('list_gap') But as mentioned, this will only get me a list of the checked ids! Is there a way to get all the data of the row into a multidimensional list, for example like..? list_checked = [ [1, 'country', 'continent', '...'], [2, 'country', 'continent', '...'], [3, 'country', 'continent', '...'], ... ] Thanks for all your help and a good night! -
Django and Nginx Docker Containers with AWS ECS Error
I am currently building a project using Terraform and AWS ECS with two containers: Django App and Nginx (to host the static files). Currently it works great; however, I am receiving an error in the logs of Nginx (using CloudWatch Logs) saying, CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False. I know this has to do with Django's ALLOWED_HOSTS since my DEBUG is set to False in the settings.py file, but I feel everything should be working as it should. Here is what my settings.py has for ALLOWED_HOSTS: ALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS', '').split() From here, I have my task definition file named container-def.json to do the job in AWS ECS: [ { "name": "django-app", "image": "${django_docker_image}", "cpu": 10, "memory": 256, "memoryReservation": 128, "links": [], "essential": true, "portMappings": [ { "hostPort": 0, "containerPort": 8000, "protocol": "tcp" } ], "command": ["gunicorn", "-w", "3", "-b", ":8000", "project.wsgi:application"], "environment": [ { "name": "RDS_DB_NAME", "value": "${rds_db_name}" }, { "name": "RDS_USERNAME", "value": "${rds_username}" }, { "name": "RDS_PASSWORD", "value": "${rds_password}" }, { "name": "RDS_PORT", "value": "5432" }, { "name": "ALLOWED_HOSTS", "value": "${allowed_hosts}" } ], "mountPoints": [ { "containerPath": "/usr/src/app/staticfiles", "sourceVolume": "static_volume" } ], "logConfiguration": { "logDriver": "awslogs", "options": { "awslogs-group" : "/ecs/frontend-container", "awslogs-region": "us-east-1" } } }, … -
Django: background task infinite loop to listen for data
Having big Django system, I need to constantly send GET requests to check if any new information is there. Pseudocode I need to be run is: while 1: has_response = True while has_response: r = request.get("[url]") has_response = len(r.content) > 0 if not has_response: break process_response(r.content) time.sleep(10000) How can I implement this on Django, considering that process_response function requires Django ORM? AWS Lambda seems to be too complicated if you use raw SQL as database structure is pretty complicated django-background-task didn't work very well (or maybe I did it the wrong way) Any good and effective ideas? -
Canceling the default AdminSite, gives problems
This is my custom AdminSite (file courses/admin.py): class IBMEAdminSite(AdminSite): site_header = f'Administración de { settings.WEBSITE_NAME }' site_title = f'Administración de { settings.WEBSITE_NAME }' admin_site = IBMEAdminSite() # registered models... ... Override the default admin site: File courses/apps.py: from django.contrib.admin.apps import AdminConfig class IBMEAdminConfig(AdminConfig): default_site = 'ibme.courses.admin.IBMEAdminSite' File settings.py: DJANGO_APPS = [ #'django.contrib.admin', 'ibme.courses.apps.IBMEAdminConfig', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ] Once this is done, the default admin site is assumed to be IBMEAdminSite, and it is, as all models of all apps are successfully registered to admin IBMEAdminSite. The problem is that exactly the models of the courses app are not registered in the new administration site. I register them correctly so I don't know why it is not shown in the new administration site: @admin.register(Class) class ClassAdmin(admin.ModelAdmin): list_display = ['__str__', 'section', 'created'] search_fields = ['title', 'section__title'] date_hierarchy = 'created' def get_changeform_initial_data(self, request): return {'section': QueryDict(request.GET.get('_changelist_filters')).get('section__id')} I have tried the following ways to register the models, but none works: @admin.register(Model, site = admin_site) admin.register(Model, ModelAdmin) admin_site.register(Model, ModelAdmin) Why does this happen? all the models of the other apps are registered without problem, but exactly these not. By the way, it should be noted that the IBMEAdminSite manager is in the same … -
Sending messages to different groups Django Channels 3
I would like to send message to different group but delay and other problems appearing. I am using Redis for the channel layer. Here user make connection and adds the first group, here is backend: Routes: from . import consumers websocket_urlpatterns = [ re_path(r'ws/chat/messages/test', consumers.MessagesConsumer.as_asgi()), re_path(r'ws/chat/(?P<room_name>[0-9a-f-]+)/$', consumers.ChatRoomConsumer.as_asgi()) ] MessagesConsumer: class MessagesConsumer(AsyncWebsocketConsumer): async def connect(self): self.group_name = 'chat_messages' # Join room group await self.channel_layer.group_add( self.group_name, self.channel_name ) headers = {} await self.accept(subprotocol=(None, headers)) async def send_message(self, message): await self.send(text_data=json.dumps(message)) class ChatRoomConsumer(AsyncWebsocketConsumer): async def connect(self): self.room_group_name = 'chat_%s' % self.room_name # Join room group await self.channel_layer.group_add( self.room_group_name, self.channel_name ) headers = {} await self.accept(subprotocol=(None, headers)) async def send_chat_message(self, message): # Send message to room group await self.channel_layer.group_send( self.room_group_name, { 'type': 'chat_message', 'message': message } ) async def chat_message(self, event): message = event['message'] # Send message to WebSocket await self.send(text_data=json.dumps(message)) The problem is here, when i send the messega to the both groups(in ChatRoomConsumer class): async def new_message(self, message_data): created_msg = await self.create_message(self.chat_room, author_name, is_from_guest, content, seen_by) serialized_message = MessageSerializer( created_msg, context=self.get_user_or_guest_context()) # FIRST await self.send_chat_message({'new_message': serialized_message.data}) # SECOND await self.channel_layer.group_send( 'chat_messages', { 'type': 'send_message', 'message': {'new_message': serialized_message.data} } ) I added comments where I call the two function ( '#FIRST' and '#SECOND'). … -
Node.js or django for backend?
I am about to start learning javascript for node.js or python for django. Which language one do you think that has the biggest potential for back-end development? -
Django send data to WebSocket from
I have a project where I want to have content of the page update as the changes happen in the database. I have set up channels and communications works good (users from certain groups can send messages and receive them inside their groups - everything is good). Now I have background tasks running and firing at certain times where the values in database change. What I'm trying to do (if its possible) is to have some data sent to the certain socket (sent after the function where the data in database changes inside the background worker function) and the users related to it would get the message and certain action would be performed. So I'm trying to find a way to send that data but can't figure out how. @background(schedule=30) def room_start(room_url): room= Room.objects.get(room_url=room_url) room.state = "R" room.save() # This is where I would send some data to users and notify them of changes and perform # actions on the frontend according to it. # I'm not sure what the solution would be, but I was thinking of maybe sending test message to # the socket and checking if there is an update but that doesn't seem to be the … -
Dependent forms in Django using AJAX
In my project I am trying to add a dependent forms solution from this answer. My template seems to accept all data correctly, but it is not displayed in the city field. Models class Country(models.Model): name = models.CharField(max_length=50) def __unicode__(self): return u'%s' % (self.name) class City(models.Model): name = models.CharField(max_length=50) country = models.ForeignKey(Country, on_delete=models.CASCADE) def __unicode__(self): return u'%s' % (self.name) urls path('getdetails/', views.getdetails, name='getdetails'), path('new-post/', views.new_post, name='new_post'), views from django.shortcuts import render from django.http import JsonResponse from django.http import HttpResponse def new_post(request): countries = Country.objects.all() [...] def getdetails(request): #country_name = request.POST['country_name'] country_name = request.GET['cnt'] result_set = [] all_cities = [] answer = str(country_name[1:-1]) selected_country = Country.objects.get(name=answer) print("selected country name ", selected_country) all_cities = selected_country.city_set.all() print(all_cities) for city in all_cities: print("city name", city.name) result_set.append({'name': city.name}) return HttpResponse(JsonResponse({'result_set': result_set})) templates <select name="selectcountries" id="selectcountries"> {% for item in countries %} <option val="{{ item.name }}"> {{ item.name }} </option> {% endfor %} </select> <select name="selectcities" id="selectcities"> </select> <!-- and jquery --> <script type="text/javascript" src="http://yourjavascript.com/7174319415/script.js"></script> <script> $(document).ready(function() { $('select#selectcountries').change(function() { var optionSelected = $(this).find("option:selected"); var valueSelected = optionSelected.val(); var country_name = optionSelected.text(); data = { 'cnt': country_name }; ajax('/getdetails', data, function(result) { console.log(result); $("#selectcities option").remove(); for (var i = result.length - 1; i >= 0; i--) … -
Is it possible to make work together Django Rest Framework, + Django Channels + Reactjs? if so..do I need Socket.io too on the client side?
I guess that the Title is quite explicit, but I will try to further explain my requirements so maybe anybody can help. As explained Im building a site that uses DRF as backend and React in the client side, and i would like to have some real time functionalities, so I´ve been researching on the issue which took me to Channels as the way to manage asyncronous actions and websockets. The question is that the more I read the more I get confused... by the Channels documentation one might say that it has capabilities to work whether sincronous as asyncronous server..but then i do not want to miss my DRF classes that simplify my life so much... and the there is this other question coming to my mind regarding if then, i must also use socket.io in the front to connect with channels on the back.... so as you see... im quite confused...anybody could help? -
django use admin widget without being logged in
I have a ModelForm with a ModelMultipleChoiceField widget from django import forms from django.contrib import admin class PortFolioForm(forms.ModelForm): class Meta: model = PortFolio fields = ['title', 'description', 'keywords'] class Media: css = {'all':['admin/css/widgets.css']} js = ['/admin/jsi18n/'] keywords = forms.ModelMultipleChoiceField(label='Mots clés', widget=admin.widgets.FilteredSelectMultiple('Mots clés', False), queryset=Keyword.objects.all()) I'm using the form outside the admin, inside a view c = {'form': PortFolioForm() } return render(request, 'portfolio.html', c) ...... form = PortFolioForm(request.POST) if form.is_valid(): form.save() ...... {{ form | crispy }} When I'm already logged in as admin, the widget is dispayed as normal If not it does not appear I get the following js errors : Refused to execute script from 'http://localhost/admin/login/?next=/admin/jsi18n/' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled. Uncaught ReferenceError: interpolate is not defined at Object.init (SelectFilter2.js:38) at SelectFilter2.js:233 at NodeList.forEach () at SelectFilter2.js:231 As the website I'm working is meant to be used by other users, I would like to use just the widget itself, because all the saving is done within the views, using form.save() -
Django Middleware Slow Performance
Our team has a production Django app that's been experiencing latency spikes throughout the day. Drilling into the requests during those time periods in Datadog, the recurring theme is that the Django middleware is running very slowly, which you can see in the flame graph. Compared to normal-latency periods, that's the main difference in performance, and this happens for all endpoints. You can see from the Heroku metrics that it occurs in an almost regular pattern. We haven't added any new middleware recently. Has anyone seen this before, or have any guidance on what could be causing this issue with the middleware? -
How to show a field in json format in django rest framework API?
I am working on DRF to build an API: serializers.py: class TemplateSerializer(FilterSerializerByOrgOwned, ValidatedModelSerializer): class Meta(): model = Template fields = '__all__' views.py: class TemplateListCreateView(ListCreateAPIView): serializer_class = TemplateSerializer queryset = Template.objects.all() pagination_class = ListViewPagination I am getting the output as such: Output: { "id": "47fc6a80-70c2-4a08-9343-0e788812de5f", "created": "2021-02-23T15:28:24.466906+01:00", "modified": "2021-02-23T15:34:03.568730+01:00", "name": "TemplateOne", "config": "OrderedDict([('general', OrderedDict([('timezone', 'Indian/Maldives'), ('maintainer', 'ManishtempOne'), ('description', 'Hey I am the tester of templateOne')]))])", "organization": "7f5e09e8-8ae7-42dd-8d83-c36080af7b05", }, In the above output, I have a field config which is displayed in a simple manner, I want to display that field in a JSON format. I tried different approaches found on StackOverflow, but didn't work for me. Is there any way out? -
Horizontal scrolling for pandas df to html not working in chrome but works with IE
As the title states I have a pandas df that I convert to an html table like so in my view: class TurnTableView(View): def get(self, request): template_name = "reference.html" queryset = Reference.objects.all() df = read_frame(queryset) df.set_index("PLAN", inplace=True) df.drop(columns="id", inplace=True) df.sort_index(axis=0, inplace=True) # expands the max width of a column in the html table pd.set_option('display.max_colwidth', 1000) # transposes the table then turns it into html table = df.T.to_html() context = { "reference": table } return render(request, template_name, context) then in my templates folder I have an html page called reference.html that looks like {% block jumbotron %} <div class="row justify-content-center"> <h3> Reference</h3> </div> {% endblock jumbotron %} {% block content %} <hr/> <br/> <div id="app" class="container-fluid"> <div class="row justify-content-center"> <div class="col-auto"> {% autoescape off %} {{ reference}} {% endautoescape %} </div> </div> <hr> <div class="row justify-content-center"> <div class="col-auto"> If any information in this table is incorrect, please contact the owners </div> </div> </div> {% endblock content %} As stated when I try to scroll all the way to the left on chrome I can't see everything within the first row, but this functionality works in IE. Any ideas of what may be wrong? -
Getting unlimited errors while running ChatterBot
Creating a ChatterBot inside my flask app. First of all i created a folder. cd into this folder. 2-created a virtuelenv called it "environment" and then activated it 3-pip install Flask 4-pip install chatterbot 5-pip install chatterbot-corpus Then i created a template, an app.py and a chatbot.py. I ran the code using: 'Python app.py' i started getting errors, the first one was: module 'time' has no attribute 'clock' i fixed this one by writing time_func = time.process_time () instead of time_func = time.clock in Ran my code again, i got AttributeError: 'str' object has no attribute 'storage' line 96 Is something wrong with the packages? are they outdated? how can i fix this problem. Would appreciate some help, thanks. -
Python Django download file to client machine using Ajax
I am trying to download a CSV file from the server to a client machine via a jQuery Ajax call, but for some reason the return response is not triggering the download. I know that there are several other questions which are quite similar, however, none of them are specifically using ajax with Django (that I've found), so I'm not sure if something needs to be done differently. The closest thing I could find was this one: download file using an ajax request which sets the window.location to a download.php file on success, but that doesn't really seem applicable. Everything I've been able to find, including the django documentation, uses some method that is very similar to what I have and yet every variation I've tried produces more or less the same result - nothing gets downloaded. I've confirmed that each step of the process is executing correctly except for the last one in the views.py. download_page.html: {% extends 'base.html' %} {% load staticfiles %} <script> $(function() { $('#download').on('click', function () { var pk = $(this).val(); $.ajax({ url: '/ajax/download/', data: {'pk': pk} }); }); }); </script> {% block 'body' %} .... <button id="download" class="btn btn-green" value="{{ view_id }}">Download</button> .... {% … -
How To Open and Read a File of Type FileField in Django?
I am trying to open a file that would be uploaded through the django FileField attribute. I also want to read the contents of the file and output it. But when I do open(obj.file), it gives me an error that read "expected str, bytes or os.PathLike object, not FieldFile" So, I am not sure how to get around this! I also tried using obj.file.read(),this does the job, but it gives these "\n" "\r" characters in the output. This is my views.py from django.shortcuts import render from .forms import ReaderForm def HomePage(request): text = None if request.method == "POST": form = ReaderForm(request.POST or None, request.FILES or None) if form.is_valid(): file = form.cleaned_data.get('file') obj = form.save(commit=False) obj.file = file obj.save() f = open(obj.file, "r") print(f.read()) else: form = ReaderForm() context = { "form": form, "text": text } return render(request, "reader/home.html", context) This is models.py from django.db import models from django.utils import timezone class Reader(models.Model): file = models.FileField(blank=True, null=True) date_uploaded = models.DateTimeField(default=timezone.now) class Meta: ordering = ['-date_uploaded'] verbose_name_plural = "Reader" This is my template file (home.html) {% extends 'reader/base.html' %} {% block head %} <title>File Reader</title> {% endblock %} {% block content %} <h1>File Reader</h1> <form method="POST" enctype="multipart/form-data"> {% csrf_token %} {{ form.as_p … -
unable to set new password using django-graphql-auth
I am trying to use the django-graphql-auth library but I got an issue using the mutation passwordReset. Here is my query : mutation { passwordReset( token: "eyJ1c2VybmFtZSI6InJvbWFyaWM5MSIsImFjdGlvbiI6InBhc3N3b3JkX3Jlc2V0In0:1lFLna:UYzR_982mIBnv6KiTqbMilAHpeSo5GV67-Y_DzBfoi4", newPassword1: "mypassword", newPassword2: "mypassword" ) { success, errors } } And I got "message": "Cannot query field \"passwordReset\" on type \"Mutation\". Did you mean \"sendPasswordResetEmail\"?" I don't mean sendPasswordResetEmail but passwordReset. Seeing the library documentations there : Documentations I think my query is good... Could you help me please ? Thank you very much ! -
Rename auth_group / auth_group_permissions / auth_permission db tables in Django 3.1.2
I am setting up a new django project and I would like to rename these tables to django_auth_group / django_auth_group_permissions / django_auth_permission but I am having trouble. I tried putting the following in __init__.py at the project root from this answer: from django.contrib.sessions.models import Session Session._meta.db_table = "django_auth_group" but was getting the following error: django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. I'm not sure this is even correct. I think I should be overwriting the model somewhere in my models directory so I tried: from django.contrib.auth.backends import ModelBackend class AuthGroup(ModelBackend): class Meta: swappable = 'AUTH_GROUP' db_table = 'django_auth_group' but this also doesn't seem to create the new table when I re-run my docker container. Any help would be appreciated. Thanks -
Django Rest FrameWork Reduce number of queries using group by
I am writing an api using Django Rest Frameworks. The api fetches a list of clients.A Clients has many projects. My api should returns the list of clients with number of projects completed, pending and total. My api works, but it has too many sql queries. The api is paginated class ClientViewSet(ModelViewSet): """ A simple view for creating clients, updating and retrieving """ model = Client queryset = Client.objects.all() serializer_class = ClientSerializer Now my client Serializer class ClientSerializer(serializers.ModelSerializer): total_projects_count = serializers.SerializerMethodField() on_going_projects_count = serializers.SerializerMethodField() completed_projects_count = serializers.SerializerMethodField() class Meta: model = Client fields = __all__ def get_total_projects_count(self, obj): return obj.total_projects_count() def get_on_going_projects_count(self, obj): return obj.on_going_project_count() def get_completed_projects_count(self, obj): return obj.completed_projects_count() Project has a client foreign key. I tried to fetch all products like below and group by using annotate. But annotate worked only on a single field. projects = Project.objects.filter(client__in=queryset).values('client', 'status') How to do group by on multiple fields and pass that extra argument to serializer. Or is there any better approach. I also tried prefetch_related but the total_projects_count was still exuecting new sql queries