Django community: Django Q&A RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django serve views asynchronously
Django 3.2.8 (a relatively old version) @csrf_exempt def xhrView(request, ms, status=200): sleep(ms / 1000) response = HttpResponse( f'{status} DUMMYXHRRESPONSE IN {ms}ms' ) response['Access-Control-Allow-Origin'] = '*' response.status_code = status return response I am hosting an api server for generative data testing with django. I have views for example serving purposes like: fetch or xhr js test generate fake json based on api query generate random long text but due to the python lang nature GIL, django is not capable of serving views asynchronously, which for example, if userA is visitng xhr/10000, any other user won't be able to even access the server in that duration. in those api views I don't need to access database, is there a way to serve the view above asynchronously? p.s. I have channels installed and is using websocket somewhere else for social messaging, the proj is set to asgi. I have tried consumers.py import asyncio from channels.db import database_sync_to_async from channels.generic.http import AsyncHttpConsumer from django.http import HttpResponse class XhrConsumer(AsyncHttpConsumer): @database_sync_to_async def sleep_ms(self, ms): return asyncio.sleep(ms / 1000) async def handle(self, body): ms = int(self.scope['url_route']['kwargs'].get('ms', 0)) await self.sleep_ms(ms) response = HttpResponse(f'{self.get_status()} DUMMYXHRRESPONSE IN {ms}ms') response['Access-Control-Allow-Origin'] = '*' await self.send_response(response) def get_status(self): return getattr(self, 'status', 200) # … -
Unable to do db index from python django
I tried to indexing my db from python django model class. But while doing migrate it throw error like this. psycopg2.errors.ReadOnlySqlTransaction: cannot execute CREATE EXTENSION in a read-only transaction Please help to resolve this issue. Is it because of any access problem. My db is in azure. I am able to read and write data to the db. -
Cant override get_queryset with group by expression in django admin
I need to override get queryset method to display the total amount of orders with such status, order_unit @admin.register(models.OrderSummary) class OrderSummaryAdmin(admin.ModelAdmin): # change_list_template = 'admin/order_summary_change_list.html' date_hierarchy = 'created' show_full_result_count = False list_display = ( "order_unit", #ForeignKey "status", #ForeignKey ) list_filter = ( ('created', DateRangeFilter), ('order_unit__name', MultiSelectFilter), ('status__name', MultiSelectFilter), ) def get_queryset(self, request): qs: QuerySet = super().get_queryset(request) grouped_by_status_and_name = qs.values('status__name', 'order_unit__name')\ .annotate(total=Count('id')).order_by('order_unit__name') x = str(grouped_by_status_and_name.query) return grouped_by_status_and_name But this is falls with TypeError("Cannot call select_related() after .values() or .values_list()") Adding select_related_field helps, but i need to group by. Combination of this two(select_rel and values() is disallowed). How can I do this? PS: I am on Django 3.1 -
How to automatically increment Django IntegerField
I have a field in my model ‘’’ Step=models.IntegerField() ‘’’ I call it normally in the template with the following: ‘’’ {{user.step}} ‘’’ Now I want this field to automatically increase after every year What is the best approach here? -
django rest framework authenticate keep returning NONE
authenticate keep returning none even I'm sure from the name and the password in database and I write them right . I also check these questions, 1 and 2 but didn't help . here is my code : @api_view(['POST']) def user_login(request): print("im in login") print(request) if request.method == 'POST': name = request.data.get('name') password = request.data.get('password') print("username : ",name,"password : ",password) user = authenticate(name=name, password=password) print("user : ",user) if user is not None: # Authentication successful login(request, user) return Response({'message': 'Login successful'}) else: # Authentication failed return Response({'message': 'Invalid credentials'}) else: # Render the login form return Response({'message': 'Invalid request method'}) here is my try to authenticate: and here is user in database : I don't know what I did wrong . -
Django ORM count by date
I have Reservation model that has two fields: start_date = models.DateField() end_date = models.DateField() I need to present occupancy report based on total number of room-nights in the month, assuming each reservation takes one room during the day. So either I need to: annotate each reservation with number of days (but only in the queried month) or for each date lookup number of reservations: filter(start_date__lte=date, end_date__gte=date) I've done solution 2 in python: month, year = 9, 2023 cal = calendar.Calendar().itemonhtdates(year, month) cal = [date for date in cal if date.month = month] res = Reservation.objects.filter(Q(start_date__range=cal) | Q(end_date__range=cal)) occupied_rooms = 0 for d in date: occupied_room += res.filter(start_date__lte=d, end_date__gt=d).count() But python solution is too slow. Is it possible to achieve same result on database level? -
Is there a way to return 3 files together with some data calculations from one Django request?
I have a Django request doing some ML calculations. The request takes 3 files as input and generates 3 files as an output together with some temperature data calculations that I need. If I try using **rest_framework.response **then I get: TypeError: keys must be str, int, float, bool or None, not tuple from rest_framework.response import Response return Response(thermal_response["data"], status=thermal_response["statusCode"]) If I try using JSONResponse I get: TypeError: Object of type ndarray is not JSON serializable from django.http import JsonResponse JsonResponse(thermal_response["data"], safe=False) -
Configuring Django App with Docker, Traefik, Custom TSL - How to?
I am trying to setup a django-app in a docker container using traefik as reverse proxy with custom tsl certificates in a docker-compose.yml. But I do not amanage to get this going. Using the below configuration, I manage to serve the app as http, but https does not work. docker-compose.yml: version: '3.9' services: traefik: image: "traefik:v2.10" container_name: traefik hostname: traefik command: #- "--log.level=DEBUG" # - --api.insecure=true - --providers.docker=true - --providers.docker.exposedbydefault=false - --entrypoints.web.address=:80 - --entrypoints.websecure.address=:443 - --providers.file.directory=/configuration/ - --providers.file.watch=true ports: - "80:80" - "443:443" # - "8080:8080" volumes: - "/var/run/docker.sock:/var/run/docker.sock:ro" - "./configuration/:/configuration/" networks: - traefik-public dbdevdanjgo: image: mariadb restart: always environment: MYSQL_ROOT_PASSWORD: danjgocou MYSQL_DATABASE: mydbdevdanjgo MYSQL_USER: userdanjgo MYSQL_PASSWORD: ${DB_PASSWORD} volumes: - /data/myapp/mariadb/ecrf/data:/var/lib/mysql - /data/myapp/mariadb/ecrf/logs:/var/log/mysql myapp: image: myrepo/myapp2:v1.0 command: bash -c 'sleep 30; gunicorn --bind 0.0.0.0:8000 myapp.wsgi' expose: - 8000 environment: - DEBUG=${DEBUG} - DJANGO_ALLOWED_HOSTS=${APP_DOMAIN} - CSRF_TRUSTED_ORIGINS=https://${APP_DOMAIN} - DB_PASSWORD=${DB_PASSWORD} - SECRET_KEY=${SECRET_KEY} labels: - traefik.enable=true - traefik.docker.network=traefik-public - traefik.constraint-label=traefik-public - traefik.http.routers.myapp-http.rule=Host(`${APP_DOMAIN}`) - traefik.http.routers.myapp-http.entrypoints=http - traefik.http.routers.myapp-http.middlewares=https-redirect - traefik.http.routers.myapp-https.rule=Host(`${APP_DOMAIN}`) - traefik.http.routers.myapp-https.entrypoints=https - traefik.http.routers.myapp-https.tls=true networks: - traefik-public restart: unless-stopped networks: traefik-public: external: false -
how to link urls path with views in django
`I am developing an ecommerce website on Django I need help on how to link URLs with the views. Here is the URL: from django.urls import path from . import views urlpatterns = [ path('search/', views.search, name='search'), path('<slug:slug>/', views.category_detail, name='category_detail'), path('<slug:category_slug>/<slug:slug>/', views.product_detail, name='product detail'), ] This is the view: from django.db.models import Q from django.shortcuts import render, get_object_or_404 from .models import Category, Product def category_detail(request, slug): category = get_object_or_404(Category, slug=slug) products = category.products.all() return render(request, 'store/category_detail.html', { 'category': category, 'products': products, }) enter image description here Attached is the error I get. I am expecting to see the web page.` -
Docker project is working fine on local environment but not working on production on ec2 instance
I am beginner to docker, I am able to run project in local environment but same thing is not working on ec2 instance I have already changed public id's to reflect changes, here are the configurations I have done so far File Structure Docker File env file Nginx Configuration File Nginx file in Docker Setting.py docker compose.yml entry point file Build Successful on server I have tried the public ip added in configuration but I am unable to access and also there is no logs in the console I tried different configurations but nothing works although on ec2 instance I have allowed all inbound ports -
Why is my cache not invalidated when using cachalot and Django?
My project uses Django version 4.1.9 and cachalot 2.5.3 (latest version). I just added the CACHES (BACKEND: "django.core.cache.backends.redis.RedisCache") option in Django and added the UpdateCacheMiddleware and FetchFromCacheMiddleware middlewares plus added "cachalot" to my installed_apps. Now when I patch/ put anything I am getting a correct and updated response (200 with the changes) but when I try to GET the resource it still shows the old response (200 without the changes). Why doesn't Django cachalot invalidate the cache on patching the objects? -
Validating if objects have been selected in a ManyToManyField in Django?
I am trying to create a def clean(self): function, to validate if my objects have been selected. This is my Code so far: class Situation(models.Model): considered_malfunctions = models.ManyToManyField( Functions, related_name='Situations', limit_choices_to={'is_important': True}, reason = models.TextField(max_length=255, null=True, blank=True) so my clean function should check if an "is_important"-object from Functions is not selected. Then the TextField in reason must be filled. If every object got selected, there is no need for a text in reason. -
graph not working properly in django, getting syntax error
I am new to Django, I want that user should be able to see the count of total user registered in that month, and will count chnages on every month view.py monthly_data = ( Clinic.objects .filter(updated__range=(start_of_month, end_of_month)) .annotate(month=TruncMonth('updated')) .values('month') .annotate(count=Count('register_clinic_user_id')) .order_by('month') ) labels = [data['month'].strftime('%B %Y') for data in monthly_data] data = [{'month': data['month'].strftime('%B %Y'), 'count': data['count']} for data in monthly_data] graph_data = json.dumps(data) print("------------------------------------", graph_data) print("----------------------------------------", labels) js new Chart(ctx2, { type: "line", data: { labels: ["Jan","Feb","Mar","Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], datasets: [ { label: {% for label in labels %} {{label}}, {%endfor%}, tension: 0.4, borderWidth: 0, pointRadius: 0, borderColor: "#16b385", borderWidth: 3, backgroundColor: gradientStroke1, fill: true, data: {% for graph_datas in graph_data %} {{graph_datas}}, {%endfor%}, maxBarThickness: 6, }, ], }, options: { responsive: true, maintainAspectRatio: false, plugins: { legend: { display: false, } }, interaction: { intersect: false, mode: 'index', }, scales: { y: { grid: { drawBorder: false, display: true, drawOnChartArea: true, drawTicks: false, borderDash: [5, 5] }, ticks: { display: true, padding: 10, color: '#b2b9bf', font: { size: 11, family: "Open Sans", style: 'normal', lineHeight: 2 }, } }, x: { grid: { drawBorder: false, display: false, drawOnChartArea: false, drawTicks: false, borderDash: … -
Django normalize() is showing 1E+2
I'm trying to create a function in my model to display product info, and in my model I have the following: class Product(models.Model): price = models.DecimalField(max_digits=10, decimal_places=2) @property def product_label(self): return f'The product price is {self.price.normalize()}' Using normalize() is working fine if the price is something like 10.50 The output is as the following, which is what I exactly want: The product price is 10.05 BUT when I have a price like 10.00, the output displayed shows 1E+1 instead of 10 The product price is 1E+1 Is there any idea how can I fix this issue? -
Aliyun esc + uwsgi + nginx + django: Website down with no logs - what steps can I take to debug?
The configured uwsgi and nginx are as followsenter image description here and enter image description here server open port enter image description here The website is currently inaccessible, including static resources, and the previous log files are no longer output what should I do? Both uwsgi and nginx are running normally enter image description here -
ClientError at /api/document/ An error occurred (403) when calling the HeadObject operation: Forbidden
I'm getting this error while I'm trying to upload a fil into s3, The following is my bucket policy { "Version": "2012-10-17", "Statement": [ { "Sid": "AllowGetAndHead", "Effect": "Allow", "Principal": "*", "Action": [ "s3:GetObject", "s3:PutObject", "s3:PutObjectAcl" ], "Resource": "arn:aws:s3:::monover-052023-s3bucket/*" }, { "Sid": "S3PolicyStmt-DO-NOT-MODIFY-1684257818955", "Effect": "Allow", "Principal": { "Service": "logging.s3.amazonaws.com" }, "Action": [ "s3:GetObject", "s3:PutObject", "s3:PutObjectAcl" ], "Resource": "arn:aws:s3:::monover-052023-s3bucket/*" } ] } does this has any error that cause this error please help me to fix it I tried a couple of ways to fix but not working also does this can be occurred due to any code issue if so where might it get happens please share your thoughts also Here is my setting.py where the connection is made, I double-checked the AWS credentials and also no issues find DEFAULT_FILE_STORAGE = "storages.backends.s3boto3.S3Boto3Storage" AWS_ACCESS_KEY_ID = "AKIAZO7XJSQ54CWZNR6K" AWS_SECRET_ACCESS_KEY = "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" AWS_STORAGE_BUCKET_NAME = "django-monover-textract" AWS_QUERYSTRING_AUTH = False AWS_S3_FILE_OVERWRITE = False -
Django + React js not able to render homepage correctly
I am trying to create a web application with Django and Reactjs. My goal is to create a homepage with username and password prompt and a submit button. When I run the program the webpage is rendering blank page and the expected behavior is to show username and password form on the webpage written in typescript.It shows a blank page with following response prasku@Praskus-Air nekikx_main % python3 manage.py runserver Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). May 25, 2023 - 08:22:11 Django version 4.2.1, using settings 'nekikx_main.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C. [25/May/2023 08:22:14] "GET / HTTP/1.1" 200 634 [25/May/2023 08:22:15] "GET /static/js/main.js HTTP/1.1" 200 0 [25/May/2023 08:22:15] "GET /static/js/index.js HTTP/1.1" 200 295 [25/May/2023 08:22:15] "GET /static/js/test.js HTTP/1.1" 200 1175 urls.py from django.contrib import admin from django.urls import path from django.conf.urls.static import static from . import views urlpatterns = [ path('admin/', admin.site.urls), path('', views.home, name='home'), ] urlpatterns += static('/static/', document_root='frontend/build/static') views.py from django.shortcuts import render def home(request): return render(request, 'home.html') home.html {% load static %} <!DOCTYPE html> <html> <head> <!-- Include React and other dependencies --> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script> </head> <body> <!-- Placeholder for React … -
Sharing Pika Connection Singleton Object between gunicron workers on django application
Hi I am trying to connect to email queue from django application. I have created an EmailPublisher which will be a singleton object which extends RabbitMQProducer object which has actual code of connecting to rabbitmq service. Now I want to share the rabbitmq connection between gunicorn workers how can I do this? Please help. class RabbitMQProducer(ABC): def __init__(self): self.credentials = pika.PlainCredentials( settings.RABBITMQCONFIG['username'], settings.RABBITMQCONFIG['password'], ) self.parameters = pika.ConnectionParameters( host=settings.RABBITMQCONFIG['host'], port=settings.RABBITMQCONFIG['port'], virtual_host=settings.RABBITMQCONFIG['virtual_host'], credentials=self.credentials, heartbeat=600, blocked_connection_timeout=300, client_properties={ 'connection_name': self.get_connection_name(), } ) self.connection = None self.channels = {} self.connect() def connect(self): if not self.connection or self.connection.is_closed: self.connection = BlockingConnection(self.parameters) self.close_channels() self.declare_channels() def declare_channels(self): for i in range(self.get_channel_count()): self.channels[i] = self.assign_channel() def send(self, message): try: self.connect() self.thread_safe_publish(message) except Exception as e: Log.e(f"Failed to send message to RabbitMQ: {e}") def assign_channel(self): if not self.connection or self.connection.is_closed: self.connect() return None channel = self.connection.channel(channel_number=None) channel.exchange_declare( exchange=self.get_rabbitmq_exchange_name(), exchange_type=self.get_rabbitmq_exchange_type(), durable=True, ) return channel def thread_safe_publish(self, message): try: random_channel_number = CommonUtils.get_random_number(0, self.get_channel_count() - 1) channel = self.channels[random_channel_number] if not channel or channel.is_closed: channel = self.assign_channel() if channel: self.channels[random_channel_number] = channel self.channels[random_channel_number].basic_publish( exchange=self.get_rabbitmq_exchange_name(), routing_key=self.get_rabbitmq_routing_key(), body=json.dumps(message), properties=pika.BasicProperties( delivery_mode=2, # make message persistent ) ) event_key = self.get_event_key() self.process_data_events(event_key) except Exception as e: Log.e(f"Failed to send message to RabbitMQ: {e}") def process_data_events(self, event_key): try: … -
Django Rest Framework UpdateAPIView is saying a field is required but the field is there
I have this model that represents an Ad: class BigAd(models.Model): DA = 'DA' WE = 'WE' MO = 'MO' INTERVAL = [ (DA, 'Day'), (WE, 'Week'), (MO, 'Month'), ] IM = 'IM' VI = 'VI' TYPES = [ (IM, 'Image'), (VI, 'Video'), ] title = models.CharField(max_length=50, default=None) campaign = models.ForeignKey(Campaign, on_delete=models.CASCADE) audience = models.ForeignKey(AdAudience, on_delete=models.CASCADE, default=None) active = models.BooleanField(default=False) body = models.TextField(max_length=255, null=True, blank=True) ad_image = models.FileField(upload_to=bigad_directory_path, null=True, blank=True) ad_type = models.CharField(max_length=2, choices=TYPES, default=IM) big_image = models.FileField(upload_to=bigad_directory_path, null=True, blank=True) video = models.FileField(upload_to=bigad_video_path, null=True, blank=True) link = models.URLField(max_length=256, null=True, blank=True) budget_interval = models.CharField(max_length=2, choices=INTERVAL, default=DA) budget = models.DecimalField(max_digits=30, decimal_places=0, default=200) lowest_bid = models.DecimalField(max_digits=20, decimal_places=2) highest_bid = models.DecimalField(max_digits=20, decimal_places=2) includes_products = models.BooleanField(default=False) product_list = ArrayField(models.IntegerField(), null=True, blank=True) user = models.ForeignKey(User, on_delete=models.CASCADE, default=None) created_at = models.DateTimeField(auto_now_add=True, verbose_name="created at") updated_at = models.DateTimeField(auto_now=True, verbose_name="updated at") class Meta: verbose_name = "big ad" verbose_name_plural = "big ads" db_table = "big_ads" ordering = ["created_at"] def __str__(self): return self.title def get_absolute_url(self): return self.slug and this serializer for serializing the data: class BigAdSerializer(serializers.ModelSerializer): campaign = ShortCampaignSerializer() class Meta: model = BigAd fields = [ "id", "title", "campaign", "audience", "ad_image", "ad_type", "big_image", "active", "body", "video", "link", "budget_interval", "budget", "lowest_bid", "highest_bid", "user", "includes_products", "product_list", "active", "created_at", "updated_at", ] and this view for … -
How to store next/return URL in Django FormView
I have a Django FormView that has two entry points from different edit buttons on different URLs. Once the user is done editing, I would like to return then to the URL they were originally on. To do this, I need to know the URL the user originally came from. So far, I have done the following: include a next parameter in the call to the FormView, then add that next parameter to the form action so that when the form is posted I can retrieve it e.g. <form method="post" action="http://127.0.0.1:8000/main/2/item/update?next=read_view"> ... </form> However, on the form post I cannot seem to get hold of the action to get the next parameter. Is there a standard way to handle this in Django? My approach feels a bit clunky. -
Not able to migrate, Foreignkey in models is causing the issue
so I have the two following models that I'm trying to migrate. class Organisation(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(max_length=256) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) status = models.CharField(max_length=64,choices=STATUS,default='active') logo_file_path = models.CharField(max_length=2048) and class Team(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(max_length=256) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) status = models.CharField(max_length=64,choices=STATUS,default='active') org_id = models.ForeignKey(Organisation(),to_field=id, on_delete=models.CASCADE) I need Team.org_id linked to Organisation.id, so I'm trying to use it as a foreign key. but while trying to make migration it is giving me the following system error: SystemCheckError: System check identified some issues: ERRORS: db_app.Team.org_id: (fields.E312) The to_field 'db_app.Team.id' doesn't exist on the related model 'db_app.Organisation'. Note: db_app is my django app name. I guess the to_field argument in the foreign key is causing the issue. I'm not sure though. I have tried it on different models in a different project the error remains the same. -
How do I correctly add one model to another in Flutter?
I'm really confused on how do I add director model to my show model. I'm writing Android application in Flutter, that uses local Django Rest API. This is Director Model in Django class Director(models.Model): name = models.CharField(max_length=20) surname = models.CharField(max_length=20) country = models.CharField(max_length=20) image = models.ImageField(null=True, blank=True, upload_to="images/") This is Shows Model in Django class Show(models.Model): name = models.CharField(max_length=50) annotation = models.TextField(max_length=1000) language = models.CharField(max_length=20, null=True) image = models.ImageField(null=True, blank=True, upload_to="images/") director = models.ForeignKey(Director, on_delete=models.CASCADE, null=True) This is Director Model in Flutter import 'package:flutter/widgets.dart'; class DirectorModel with ChangeNotifier { final int id; final String name; final String surname; final String image; DirectorModel({required this.id, required this.name, required this.surname, required this.image}); factory DirectorModel.fromJson(dynamic json) { return DirectorModel( id: json['id'], name: json['name'] as String, surname: json['surname'] as String, image: json['image'] as String, ); } static List<DirectorModel> directorFromSnapshot(List snapshot) { return snapshot.map((data) { return DirectorModel.fromJson(data); }).toList(); } } This is Shows model in Flutter import 'package:flutter/widgets.dart'; import 'package:kulises_app/models/directors.dart'; class ShowsModel with ChangeNotifier { int id; String name; String annotation; String language; DirectorModel director; String image; ShowsModel( {required this.id, required this.name, required this.annotation, required this.language, required this.director, required this.image}); factory ShowsModel.fromJson(dynamic json) { return ShowsModel( id: json['id'], director: DirectorModel.fromJson(json['director']), name: json['name'] as String, annotation: json['annotation'] … -
How to add more param to Serialization Model for using at to_presentation
Here is my so class DataFieldSerializer(serializers.ModelSerializer): class Meta: model = DataField fields = ["field", "topic", "category", "group", "alias", "units"] def __init__(self, *args, **kwargs): self.extra_param = kwargs.pop('extra_param', None) super().__init__(*args, **kwargs) def to_representation(self, instance): data = super().to_representation(instance) print("Extra Param:", self.extra_param) This is how I call the DataFieldSerializer result_data = DataFieldWithUnitsSerializer(data_fields, many=True, extra_param='example').data When I run the code I got this result Extra Param:None I guess it is not the right way to add more param into a model serializer. To sum up, I want to find away to pass an extra param to the Serializer Model -
Why is a shallow copy used with self.cart in this Django Python code?
in this python code, def __iter__(self): """ Iterate over the items in the cart and get the products from the database. """ product_ids = self.cart.keys() # get the product objects and add them to the cart products = Product.objects.filter(id__in=product_ids) cart = self.cart.copy() for product in products: cart[str(product.id)]['product'] = product for item in cart.values(): item['price'] = Decimal(item['price']) item['total_price'] = item['price'] * item['quantity'] yield item Is the Python copy function a shallow copy? Why is it shallow copying self.cart instead of just using self.cart directly? using self.cart directly instead of using variable cart. -
Django allauth telegram provider not working
Has anyone successfully implemented using allauth's telegram provider? I've followed the documentation and everything but it still does nothing.