Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django channels authentication with http post and use websockes after
I am working on a project where we have been using crud with a rest api, standard stuff. Now we want to switch to websockets as we want to have realtime updates. Think airline reservation calender. When someone clicks on a box in calender it needs to be blocked on every connected user. I see a lot of examples of people giving examples of authentication over the websocket. But I find creation of web-socket before login to a system wasted full. So ideally I would like to authenticate the user via JWT over an HTTP POST and get a token and if use it authenticated then create a websocket with the token that i get from JWT. Before I get into details. Is this the write way to do or should one create a websocket connetion right away and do the whole api stuff over websocket including the initial auth(username ,password stuff) . Thanks -
i got TypeError: UserManager.create_superuser() missing 1 required positional argument: 'username'
I want to create super user just from email and password so it should create super user without username. I am using custom user model and when i try to create superuser i got this error TypeError: UserManager.create_superuser() missing 1 required positional argument: 'username' This is my manager.py file from django.contrib.auth.base_user import BaseUserManager class UserManager(BaseUserManager): use_in_migrations = True def _create_user(self, email, password, **extra_fields): if not email: raise ValueError('The given email must be set') email = self.normalize_email(email) user = self.model(email=email, **extra_fields) user.set_password(password) user.save(using=self._db) return user def create_user(self, email, password=None, **extra_fields): extra_fields.setdefault('is_superuser', False) return self._create_user(email, password, **extra_fields) def create_superuser(self, email, password, **extra_fields): extra_fields.setdefault('is_superuser', True) extra_fields.setdefault('is_staff', True) if extra_fields.get('is_superuser') is not True: raise ValueError('Superuser must have is_superuser=True.') if extra_fields.get('is_staff') is not True: raise ValueError('Superuser must have is_staff=True') return self._create_user(email, password, **extra_fields) This is my models.py class User(AbstractUser) """ Abstract User of django auth user model. """ uuid = models.UUIDField(default=uuid.uuid4, unique=True, null=False) email = models.EmailField(_("email address"), unique=True) middle_name = models.CharField(max_length=120, blank=True) image = models.ImageField(upload_to='user_profile/', blank=True) department = models.CharField(blank=True, choices=Department.choices, max_length=2) designation = models.CharField(max_length=120, blank=True) last_logout = models.DateTimeField(null=True) is_superuser = models.BooleanField(default=False) view = models.TextField(blank=True) USERNAME_FIELD = 'email' REQUIRED_FIELDS = [] -
Django Cloned Project does not give tables after migrating
I cloned a project that I want to modify and experiment with, and I have all the necessary credentials. However, I'm encountering issues while following the steps outlined in the ReadMe.md file since I cloned it: Clone the project. Create a .env file and add the following information: Database variables: DATABASE_NAME: Database name DATABASE_USER: Database user DATABASE_PASSWORD: Database password DATABASE_HOST: Database host DATABASE_PORT: Database port Authentication variables for the Telnyx API: Telnyx_v2_key: Telnyx API v2 key x-api-token: Telnyx API v1 token x-api-user: Telnyx API v2 user email Create a virtual environment: python -m venv ./venv Activate the environment: source venv_telnyx-invoicing/bin/activate.csh Install requirements: pip install -r requirements.txt Create a logs directory: mkdir logs Make migrations: python manage.py makemigrations Migrate: python manage.py migrate I’m currently stuck at the migration step. When I try to run python manage.py makemigrations, I receive the following error: Traceback (most recent call last): File "C:\Users\DominykasPavlijus\Desktop\Telnyx-Invoicing-1\venv\Lib\site-packages\django\db\backends\utils.py", line 105, in _execute return self.cursor.execute(sql, params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ psycopg2.errors.UndefinedTable: relation "Project_tenant" does not exist LINE 1: ...nant"."id", "Project_tenant"."billing_group" FROM "Project_t... My settings.py file appears to be configured correctly: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': os.getenv('DATABASE_NAME'), 'USER': os.getenv('DATABASE_USER'), 'PASSWORD': os.getenv('DATABASE_PASSWORD'), 'HOST': os.getenv('DATABASE_HOST'), 'PORT': os.getenv('DATABASE_PORT'), } } This is the model that … -
Mock patching a callable upload_to in a Django ImageField
In Django I have a model and a test similar to the following, both in a photos module (Django app). models.py def get_user_photo_path(instance, filename): return str(uuid.uuid4()) class UserPhoto(models.Model): uploader = models.ForeignKey(User, on_delete=models.CASCADE) image = models.ImageField(upload_to=get_user_photo_path) upload_timestamp = models.DateTimeField(auto_now_add=True) tests.py @mock.patch('photos.models.get_user_photo_path') def test_image_upload_path(self, mock_photo_path): mock_photo_path.return_value = 'mocktest' user_photo = UserPhoto.objects.create( uploader=self.parking_user, # these self variables are defined in setUp() image=self.image ) expected_path = 'mocktest' self.assertTrue(user_photo.image.name.endswith(expected_path)) # Assertion fails # For demonstration, this assertion DOES pass from .models import get_user_photo_path self.assertEquals(get_user_photo_path(), 'mocktest') This does not work, and I'm not sure why. But if I import the get_user_photo_path function directly in the test and call it, it does return 'mocktest'. I can only guess it has something to do with the way the ImageField processes its arguments, but from the source code nothing stands out to me as particularly strange or unusual. I got around it by patching photos.models.uuid.uuid4 instead, but if someone could explain why the above patch didn't work, I would be very grateful! -
Django SSO integration for JumpCloud
Newbie question. I am trying to configure my company admin website to use JumpCloud for SSO auth. I can't find any simple solution. Is there a conventional/standard practice way to integrate OIDC into a simple Django website? Secondly, if my web application runs in a private network (ie No public access), does the IDP have to have access to my web application to return the auth code (back channel)? -
Failed lookup for key [site_header] when extending admin/change_list.html
i tried to extend change_list.html template from using this two guide https://hakibenita.com/how-to-turn-django-admin-into-a-lightweight-dashboard & https://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-vs-replacing-an-admin-template this is my view @admin.register(summery) class StaisticsSummery(ModelAdmin): change_list_template ="admin/stat_summary_change_list.html" def changelist_view(self, request,extra_context=None): extra_context = extra_context or {} filterUser = request.GET.get("q","") filterStart = request.GET.get("startDate","") filterEnd = request.GET.get("endDate","") if filterStart=="": filterStart = "1300-01-01" if filterEnd=="": filterEnd = "1500-12-29" result = list(User.objects.values("username").annotate( total_Balance=Sum('statistics__balance',default=0,filter=Q(statistics__created_at__gte=filterStart)|Q(statistics__created_at__lte=filterEnd)), ...) ).order_by("username")) if(filterUser!="" and filterUser is not None): result =result.filter(username=filterUser) extra_context["result"]=result return super().changelist_view(request,extra_context) i request when there was'nt any start and end to querystring everything looks works fine and all result are valid i use the given query in django shell an everything were valid but somehow opt context variable is changing when trying to query with dates via the browser is there any better way to debug this problem related to context object -
render_to_string function not displaying context variables
I am trying to implement the render_to_string function in a Django project. I have an HTML template (strategy.html) containing many Django Template Language variables and 'if conditions' and would like to save the rendered output in a DB model. The output is saved in the db model but the context variables are ignored. The action has to be triggered once the 'Save' button is clicked (see code below). Important note: the DTL variables and 'if conditions' are correctly displayed when using the render function by clicking the 'Generate' button. Here is the views.py file: from django.shortcuts import render, redirect from django.contrib.auth.decorators import login_required from django.template.loader import render_to_string from strategiesAPI.models import strategiesList @login_required def index(request): if request.method == "POST": # Get form information starttime = request.POST.get("starttime") endtime = request.POST.get("endtime") trigger = request.POST.get("trigger") entrieslimit = request.POST.get("entrieslimit") direction = request.POST.get("direction") pattern = request.POST.get("pattern") stoploss = request.POST.get("stoploss") takeprofit = request.POST.get("takeprofit") tradeliquid = request.POST.get("tradeliquid") # Context variables for the render and render_to_string function content = { "starttime": starttime, "endtime": endtime, "trigger": trigger, "entrieslimit": entrieslimit, "direction": direction, "pattern": pattern, "stoploss": stoploss, "takeprofit": takeprofit, "tradeliquid": tradeliquid } # Action based on the button pressed inside the index.html template # GENERATE button if request.POST['button'] == 'Generate': # Write … -
Apache WSGI module Fails to Start with net start apache2.4, but Works with httpd.exe
I’m currently facing an issue with my Apache server setup on Windows. I’ve been able to run Apache successfully using httpd.exe, but when I attempt to start it using net start apache2.4, the service fails to start, and I receive an error in the Event Viewer (no corresponding Apache error log though) System Details Apache Version: Apache 2.4 (64-bit) Python Version: Python 3.13 mod_wsgi Version: 5.0.1 (64-bit) Django App Path: C:/webapp WSGI Module Path: C:/Users/{me}/AppData/Local/Programs/Python/Python313/Lib/site-packages/mod_wsgi/server/mod_wsgi.cp313-win_amd64.pyd What I’ve Tried 1. Running httpd.exe Directly: Apache starts successfully and serves my Django application as expected. No issues with the Django app or the WSGI module when starting manually. **2. Running httpd -t ** Returns Syntax OK. 3. Windows Event Viewer Error: When starting Apache with net start apache2.4, I see the following error in the Event Viewer: The Apache service named reported the following error: >>> httpd.exe: Syntax error on line 194 of C:/Apache24/conf/httpd.conf: Cannot load C:/Users/{me}/AppData/Local/Programs/Python/Python313/Lib/site-packages/mod_wsgi/server/mod_wsgi.cp313-win_amd64.pyd into server: The specified module could not be found. . 4. Verified File Paths: I have confirmed that the mod_wsgi file exists in the exact location specified in the httpd.conf file: c:\Apache24\bin>dir "C:\Users\{me}\AppData\Local\Programs\Python\Python313\Lib\site-packages\mod_wsgi\server" Volume in drive C is Windows Volume Serial Number is 8205-BA06 Directory of … -
ODBC Driver Error: File Not Found When Running Python Script in Docker Container
I'm running a Python script (sharepoint_to_json.py) inside a Docker container that connects to a SQL Server database using the ODBC driver. My goal is to convert data from an Excel (.xlsx) file into JSON format within my server, using the sharepoint_to_json.py script. This process should run inside the Docker container after establishing a connection to a SQL Server database. I have already installed the msodbcsql17 driver in the container, and my connection to the database works when running other scripts. However, when I try to run the sharepoint_to_json.py script, I encounter the following error: Ocorreu um erro ao processar o arquivo Excel: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib '/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.9.so.1.1' : file not found (0) (SQLDriverConnect)") Here's the relevant part of my Dockerfile: FROM python:3.12 RUN apt-get update && apt-get install -y curl gnupg2 apt-transport-https unixodbc unixodbc-dev python3-distutils build-essential && curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - && curl https://packages.microsoft.com/config/debian/10/prod.list > /etc/apt/sources.list.d/mssql-release.list && apt-get update && ACCEPT_EULA=Y apt-get install -y msodbcsql17 Set the working directory in the container WORKDIR /app Upgrade pip, setuptools, wheel to ensure the latest versions RUN pip install --upgrade pip setuptools wheel Copy the requirements.txt file COPY requirements.txt . Install the dependencies RUN pip install -r requirements.txt Copy … -
How to set environment variables in a Google Cloud VM (Ubuntu) for Django project without exposing sensitive information?
I am using a virtual machine on Google Cloud with Ubuntu as the operating system, and I have deployed a Django backend on it. In my settings.py, I use environment variables like this: EMAIL_HOST_USER = env('EMAIL_HOST_USER') EMAIL_HOST_PASSWORD = env('EMAIL_HOST_PASSWORD') I would like to know how to properly set environment variables on the VM so that my Django application can access them. Specifically: How do I create environment variables on the VM (Ubuntu)? How can I ensure that sensitive data (like EMAIL_HOST_PASSWORD) is not exposed in plain text, but still accessible by my Django app? Any best practices for securely managing these environment variables in a Google Cloud VM would be appreciated. Thanks! -
Trying to test multiple test files with multiple test classes but PyTest doesn't recognize other files
I've been scouring all over the internet in order to find solution for this problem. I've been trying to make sure that PyTest recognizes multiple test files to account for the multiple test files and the classes inside them in order to build a full test suite. Here's the config file: [pytest] asyncio_mode=auto asyncio_default_fixture_loop_scope="class" DJANGO_SETTINGS_MODULE = personal_blog.settings testpaths = tests Here's the test class that it recognizes: test_members.py import pytest import factory from django.contrib.auth import get_user_model from factories import MemberFactory from faker import Faker from django.urls import reverse from django.contrib.auth.models import Permission fake = Faker() # Create your tests here. User = get_user_model() # Basic Tests class TestMembers: #Confirms that a user has been created @pytest.mark.django_db def test_if_user_exists(db): user = MemberFactory() assert user.username is not None assert user.email is not None # Checks if the password fails @pytest.mark.django_db def test_set_check_password_fail(db): # basic_user.set_password("password") user = MemberFactory() assert user.password != 'Wrong' # Checks if the password fails @pytest.mark.django_db def test_set_check_password_success(db): user = MemberFactory() assert user.password == 'password' # Checks if the user is not a contributor by default. @pytest.mark.django_db def test_is_not_contributor_by_default(db): user = MemberFactory() assert user.is_contributor is False # Checks if the User is a contributor @pytest.mark.django_db def test_is_contributor(db): user = MemberFactory(is_contributor … -
Django Static Image Not Displaying When Using Variable for Image Path
I'm trying to load and display a static image in a Django template. I have a dictionary named post that contains an image value, for example, "mountain.jpg". I checked, and the dictionary has the correct data. However, when I try to set the image path dynamically using post.image, the image is not displayed. If I hardcode the URL, the image shows up correctly. Here is my image tag in the template // not working <img src="{% static "blog/images/"|add:post.image %}" alt="Mountain" /> // working <img src="{% static "blog/images/mountain.jpg" %}" alt="Mountain" /> -
Why does VScode keep popping the "SyntaxError" message, even before i am done typing?
syntax error massage, this is the error message suggestions that keeps popping up and interrupting my code flow, most times i have to comment my lines of code, and that is not ideal, so removing it would be the best. i tried going to settings to look for it , but could not find anything. I even had to go to the Json file, but to no avail, i really need help soonest -
Can I reuse output_field instance in Django ORM or I should always create a duplicate?
I have a Django codebase that does a lot of Case/When/ExpressionWrapper/Coalesce/Cast ORM functions and some of them sometimes need a field as an argument - output_field. from django.db.models import FloatField, F some_param1=Sum(F('one_value')*F('second_value'), output_field=FloatField()) some_param2=Sum(F('one_value')*F('second_value'), output_field=FloatField()) some_param3=Sum(F('one_value')*F('second_value'), output_field=FloatField()) some_param4=Sum(F('one_value')*F('second_value'), output_field=FloatField()) some_param5=Sum(F('one_value')*F('second_value'), output_field=FloatField()) Sometimes I find myself wondering why am I always creating the same instance of any Field subclass over and over again. Is there any difference if I just pass one instance and share it between expressions? F.E from django.db.models import FloatField, F float_field = FloatField() some_param1=Sum(F('one_value')*F('second_value'), output_field=float_field) some_param2=Sum(F('one_value')*F('second_value'), output_field=float_field) some_param3=Sum(F('one_value')*F('second_value'), output_field=float_field) some_param4=Sum(F('one_value')*F('second_value'), output_field=float_field) some_param5=Sum(F('one_value')*F('second_value'), output_field=float_field) I coulnd't find it in a documentation and the source code is not documented well regarding this parameter. P.S. The example is fake, just imagine a big annotate function that does a lot of processing using Case/When/ExpressionWrapper/Coalesce/Cast and has a lot of duplicated Field instances as output_field. -
Discrepancy in Record Count Between Django ORM and Raw SQL Query
I'm encountering an issue where the count of records returned by a Django ORM query does not match the count returned by a raw SQL query. Here is the relevant part of my Django view: start_date = datetime(2024, 10, 19, 0, 0, 0) end_date = datetime(2024, 10, 19, 23, 59, 59) dbug = Reservations.objects.all().filter(updated_at__range=(start_date, end_date)) print(dbug.count()) Above returns 6529 The Django settings.py contains: TIME_ZONE = 'Asia/Tehran' USE_TZ = False I have tried SQL query same bellow: SELECT COUNT(*) FROM "consultant_reservations" WHERE updated_at BETWEEN '2024-10-19 00:00:00' AND '2024-10-19 23:59:59'; count ------- 6540 (1 row) Here is discrepancy within SQL query result (which is 6540) I have tried in psql terminal and Django ORM result (which is 6529) Please let me presenting an example: Trying SQL query same as: SELECT * FROM "consultant_reservations" WHERE updated_at BETWEEN '2024-10-19 00:00:00' AND '2024-10-19 23:59:59' LIMIT 4; Result: id | idd | voip_number | client_id | client_mobile | reserve_duration | status | reserve_timestamp | created_at | updated_at | consultant_id_id | mobile_id | created_by | updated_by -------+---------+-------------+-----------+---------------+------------------+----------+------------------------+------------------------+------------------------+------------------+-----------+------------+------------ 76407 | 2011050 | 2217 | 1101151 | 09355648120 | 3600 | reserved | 2024-10-19 19:30:00+00 | 2024-10-14 08:40:03+00 | 2024-10-19 20:28:01+00 | 5052 | 2395781 | 3445 | 0 … -
Are consecutive django model save calls safe?
I'm having trouble with a field that seems to sometimes not update. Just want to know if the following is unsafe on a Django model instance. obj.field1 = True obj.save() obj.field2 = True obj.save() Since I'm not calling obj.update_from_db() is there any risk the second save resets/overrides field1 ? There are no post_save hooks involved, the surrounding code is synchronous and the DB backend is PostgreSQL. Also, is save() synchronous (in the sense that the DB update will happen before the function returns)? -
WebSocket Connection Issue with Django Channels in Gunicorn
I tried to implement live chat using Django Channels and Daphne. It works fine on my local server, but now I want to implement it in production using Gunicorn, nginx. However, when I reload and restart the and run, I'm getting an error (as shown in the image below) it in service. I tried modifying my services, but it still doesn't work. I can't figure out the cause since there is no specific error. Can someone help me with this? Any ideas would be much appreciated this is what I tried Service Gunicorn [Unit] Description=Tris application daemon services Requires=tev.socket After=network.target [Service] User=dswdcaraga Group=www-data WorkingDirectory=/opt/apps/tev Environment=DJANGO_SETTINGS_MODULE=tev.settings ExecStart=/opt/apps/env/bin/daphne -u /run/tev/tev.sock tev.asgi:application --bind 0.0.0.0:8000 [Install] WantedBy=multi-user.target Nginx server { server_name caraga-tris-staging.dswd.gov.ph; listen 80; return 301 https://caraga-tris-staging.dswd.gov.ph$request_uri; } server { server_name caraga-tris-staging.dswd.gov.ph; listen 443 ssl; #ssl_certificate /etc/ssl/certs/caraga/server-cer.pem; #ssl_certificate_key /etc/ssl/certs/caraga/server-key.key; ssl_certificate /etc/ssl/certs/caraga/ssl_nginx/server-cer.pem; ssl_certificate_key /etc/ssl/certs/caraga/ssl_nginx/server-key.key; location / { include proxy_params; proxy_pass http://unix:/run/tev/tev.sock; #WebSocket support proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "Upgrade"; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } proxy_headers_hash_max_size 1024; proxy_headers_hash_bucket_size 128; asgi.py configuration import os from django.core.asgi import get_asgi_application from channels.routing import ProtocolTypeRouter, URLRouter #from django.core.asgi import get_asgi_application from channels.auth import AuthMiddlewareStack from main import routing import main.routing os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tev.settings') django.setup() … -
How to run a Python Django project that is deployed on a local machine?
I attempted to run a deployed Django project on my local machine by setting up a virtual environment and installing all required dependencies. I also modified the production.py settings, specifically the ALLOWED_HOSTS and CSRF_TRUSTED_ORIGINS, to include local addresses like http://127.0.0.1:8000. However, despite these changes, the project only displayed a public IP and did not allow me to access it locally. I would like guidance on additional adjustments needed in the production.py or elsewhere to successfully run the project on my local machine. -
Django - TemplateDoesNotExist at /accounts/login/
I have looked at similar posts, but am really struggling with templates and urls.py - at least thats where I think my problem is. I'm trying to use the default login page that comes with django. I get this error message, I've tried so many things but still cannot not get to work, can some please help? This is the error message: TemplateDoesNotExist at /accounts/login/ registration/login.html Request Method: GET Request URL: http://134.209.220.170/accounts/login/?next=/timwise/upload/ Django Version: 5.0 Exception Type: TemplateDoesNotExist Exception Value: registration/login.html Exception Location: /usr/local/lib/python3.10/dist-packages/django/template/loader.py, line 47, in select_template Raised during: django.contrib.auth.views.LoginView Python Executable: /usr/bin/python3 Python Version: 3.10.12 Python Path: ['/home/django/django_project', '/usr/bin', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '/usr/local/lib/python3.10/dist-packages', '/usr/lib/python3/dist-packages'] Server time: Wed, 23 Oct 2024 01:53:02 +0000 Template-loader postmortem Django tried loading these templates, in this order: Using engine django: django.template.loaders.filesystem.Loader: /home/django/django_project/templates/registration/login.html (Source does not exist) django.template.loaders.app_directories.Loader: /usr/local/lib/python3.10/dist-packages/django/contrib/admin/templates/registration/login.html (Source does not exist) django.template.loaders.app_directories.Loader: /usr/local/lib/python3.10/dist-packages/django/contrib/auth/templates/registration/login.html (Source does not exist) this is the template area of my settings.py: TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', # 'DIRS': [BASE_DIR, "/home/django/django_project/templates"], 'DIRS': [os.path.join(BASE_DIR, 'templates')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] this is my class view that is trying to redirect to the default django login page: class FileUploadView(LoginRequiredMixin, CreateView): #entire … -
Django TypeError: Cannot filter against a non-conditional expression while filtering by foreign key
I've looked through multiple similar questions, but couldn't get the logic, and my manipulations were unsuccessful. I have a forum app, its structure is "forum->subforum->topic->comments", corresponding models for subforums, topics and comments. I have to implement a filter on each subforum page (where the list of corresponding topics is displayed), by which I can configure the search of/among the topics by several conditions. Particularly I have to implement a custom filter, and my idea was to create a filter for empty topics, that is, for topics only with initial comments and without responses. Through a desperate struggle I've finally understood how to implement reversed foreign key connection in querysets, but now I can't use a queryset expression as a filter, as it returns TypeError: Cannot filter against a non-conditional expression. Here are the specifics: filters.py: import django_filters from django.db.models import Count from forum.models import Topic class TopicFilter(django_filters.FilterSet): date_span = django_filters.DateRangeFilter(field_name='created', label='Период создания') comments = django_filters.CharFilter(field_name='comment__content', lookup_expr='icontains', label='Комментарии') subject = django_filters.CharFilter(lookup_expr='icontains', label='Тема обсуждения') creator = django_filters.CharFilter(field_name='creator__username', lookup_expr='icontains', label='Создатель темы') is_empty = django_filters.BooleanFilter(method='filter_empty', label='Темы без комментариев') class Meta: model = Topic fields = ['subject', 'creator', 'date_span', 'comments', 'is_empty'] def filter_empty(self, queryset, value): comment_count_gte_1 = Topic.objects.annotate(comment_count=Count('comments')).filter(comment_count__gte=1) comment_count_lt_1 = Topic.objects.annotate(comment_count=Count('comments')).filter(comment_count__lt=1) if value is True: … -
Optimizing Django QuerySet with Nested Aggregations
I’m working on optimizing a complex Django query where I need to perform nested aggregations and conditional annotations across multiple related models. I want to fetch the top 5 most active users based on their interactions with posts, while also calculating different types of engagement metrics (like views, comments, and likes). My models: class User(models.Model): name = models.CharField(max_length=100) class Post(models.Model): author = models.ForeignKey(User, on_delete=models.CASCADE) title = models.CharField(max_length=255) created_at = models.DateTimeField() class Engagement(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) post = models.ForeignKey(Post, on_delete=models.CASCADE) type = models.CharField(max_length=50) # 'view', 'like', 'comment' created_at = models.DateTimeField() Here is what my code looks like: from django.db.models import Count, Q some_date = ... top_users = ( User.objects.annotate( view_count=Count('engagement__id', filter=Q(engagement__type='view', engagement__created_at__gte=some_date)), like_count=Count('engagement__id', filter=Q(engagement__type='like', engagement__created_at__gte=some_date)), comment_count=Count('engagement__id', filter=Q(engagement__type='comment', engagement__created_at__gte=some_date)), total_engagements=Count('engagement__id', filter=Q(engagement__created_at__gte=some_date)) ) .order_by('-total_engagements')[:5] ) It works, however the query performance is not ideal. With large datasets, this approach leads to slow query execution times and I wonder whether using multiple Count annotations with filter conditions is efficient. Is there a more optimized way to write this query, or any best practices I should consider for improving performance, especially when dealing with large amounts of data? Any insights or suggestions would be really helpful! -
Django dynamically adding integer fields to forms
Hi I have a very simple application that I am making in order to learn Django. I have a SQLite table that has two columns, the first is the 'id' generated by Django, the second is a charfield containing the name of a product_name. The way I am trying to get the program to work is as follows: Get value list of values in the table. This query_set gets assigned to bases Loop through the each tuple in the bases list and use these values to create form fields. So the product_name will be the Label and the id will be the id of the field. I will later use Jquery on these fields to get the values. I have the following code: from django import forms from django.db import models from django.core.exceptions import ValidationError from .models import Department, Test_list, Products, Current_tests class Batch(forms.Form): bases = Products.objects.all().values_list('product_name','id') for i in bases: for j in [0,1]: bn = i[0] bnid = i[1] name = forms.IntegerField(label=f"{bn}",widget=forms.NumberInput(attrs={"id":bnid})) The code actually works but instead of getting a number of text inputs equal to the rows in my database, it only shows the last row. I believe this is because while in the loop, I … -
Django + jQuery: No Image Provided Error with 400 31 Response When Uploading Image via AJAX
I'm working on a Django project where I'm uploading an image to be processed by a machine learning model. The frontend uses jQuery to send the image via an AJAX request. However, the server keeps returning the error: {"error": "No image provided."} and Accuracy: NaN%. Additionally, I receive the response 400 31 in the browser console. Frontend (AJAX and HTML Code): <script> $(document).ready(() => { $("input[id='image']").on('change', function (event) { let input = this; var reader = new FileReader(); reader.onload = function (e) { $('#banner').css('width', '350px') $('#banner').addClass('img-thumbnail') $('#banner').attr('src', e.target.result); } reader.readAsDataURL(input.files[0]); }) $('#process').click(() => { $('.buttons').hide() $('.loader').show() $('#title').html("Processing...") let image = $('#image').prop('files')[0] var data = image['name']; console.log(data) $.ajax({ url: "http://127.0.0.1:8000/api/", type: "POST", dataType: 'json', data: { image: data, csrfmiddlewaretoken: '{{ csrf_token }}' }, headers: { 'Authorization': 'z*qm$(e9k0wa--t!^ux(xn15vk$)h7c$%3%vflo^@_++bg&uw(', // CSRF token 'Content-Type': 'application/json' // JSON }, success: function (xhr) { alert("Error while processing") }, error: function (xhr) { $('#title').html("Result") let result = (xhr.responseText).split("-"); let disease = result[0]; let accuracy = result[1]; $('.loader').hide() $('#disease').html("Result: " + disease) $('#accuracy').html("Accuracy: " + parseInt(accuracy).toFixed(2) + "%") $('#graph').attr('src', '{% static "graph.png" %}') $('.result').show() } }); }); }); </script> Django Views.py: from django.shortcuts import render from django.views.decorators.csrf import csrf_exempt from django.http import HttpResponse, JsonResponse from diab_retina_app import process … -
Rendering MultiValueField including help_text and label for each subfield
I am using a JSONField to represent a config that is used in multiple forms. Instead of the default Textarea widget, I want to render multiple fields each with their own label and help_text. I am able to achieve this by implementing a Form just for the config but it seems it should also be possible (and cleaner) to represent the config as a MultiValueField. Unfortunately I cannot for the life of me figure out how to render the label and the help_text when following this approach as that information seems no longer available when the widget is rendered. What am I missing? class ConfigWidget(forms.MultiWidget): template_name = 'config_widget.html' def __init__(self, attrs=None): widgets = [ forms.TextInput(attrs=attrs), forms.TextInput(attrs=attrs), ] super().__init__(widgets, attrs) def decompress(self, value): if isinstance(value, dict): return [value['foo'], value['bar']] return [None, None] class ConfigField(forms.MultiValueField): widget = ConfigWidget def __init__(self, *args, **kwargs): fields = ( forms.CharField(label='Foo', help_text='Foo help.'), forms.CharField(label='Bar', help_text='Bar help.'), ) super().__init__(fields=fields, *args, **kwargs) def compress(self, data_list): if data_list: return { 'foo': data_list[0], 'bar': data_list[1], } -
Django test client redirects logged users to login page
I know this questions has already been asked, but none of the answers works for me. Whenever I try to use the client login in Django tests, it basically gets ignored and I get a redirect to the login page. After a few test I noticed that it logs me in correctly, but kicks me out when I use it. Here is a stripped down version of my code that still reproduces this. from django.auth import get_user() class MapListViewTest(TestCase): @classmethod def setUpTestData(cls): # Create a user cls.user = get_user_model().objects.create(username="testuser") cls.user.set_password("password") cls.user.save() # [...] def setUp(self): self.client.login( username=self.user.get_username(), password=self.user.password ) def test_map_list_view_renders(self): """Test that the MapListView renders successfully and includes a list of maps.""" logger.info(get_user(self.client)) # <-- <django.contrib.auth.models.AnonymousUser object at 0x7377d5589540> self.client.login( username=self.user.get_username(), password=self.user.password ) logger.info(get_user(self.client)) # <-- <Profile: testuser> response = self.client.get(reverse("map_list"), follow=True) logger.info(get_user(self.client)) # <-- <django.contrib.auth.models.AnonymousUser object at 0x7377d5589540> self.assertEqual(response.status_code, 200) # <-- OK self.assertTemplateUsed(response, "map_list.html") # <-- lands on 'account/login.html' (I put it twice here in the snippet, but it is only in the setUp in the real code) I don't know if this is of any relevance, but the redirection changes the address from http://localhost/... to http://testserver/..., and I am working in a docker container. Also, the …