Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django annotation based on Count not working, always return 1
In models.py I have defined: class Order(models.Model): ... class Operation(models.Model): order = models.ForeignKey(Order, on_delete=models.CASCADE, related_name="operations") ... I have a queryset of orders where the first order has two operations. orders = order.annotate(mycount=Count('operations')) print(orders[0].operations.count()) print(orders[0].mycount) The previous piece of code returns: 2 1 Hence orders.filter(mycount__gte=2) erroneously returns an empty set. I can see from old posts that this was already a problem in the past, and that it seems like it got fixed as the forum discussions are old (here for example) . -
Can I get the related data directly when using a join query in django?
Well considering two models: class School(models.Model): name = TextField() class Student(models.Model): school = ForeignKey(School related_name=students ) firstname = TextField() And the query: School.objects.filter(Q(name="oldschool") & Q( Q(students__firstname="hello") | Q(students__firstname="testname") )) I retrieve the schools. However there is a join/subquery obviously executed, yet I do not get the student information. I also wish to get the student information for which the "first name is set". Can I make django orm actually fill in a students_set, so I do not have to do multiple lookups later? (having to iterate over the schools and check per school). -
python manage.py runserver No such file or directory
enter image description hereenter image description here I have learned the lesson and applied all the steps, but the page did not appear to me as in the lesson. It is expected that the server should work, but an error is showing. What is the reason? -
what is the default engine value used in django rest framework while using postgres database [closed]
ImproperlyConfigured at /api/token settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details. Request Method: POST Request URL: http://localhost:8000/api/token Django Version: 5.0.6 Exception Type: ImproperlyConfigured Exception Value: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details. Exception Location: /home/k/PycharmProjects/ProjectPMS/.venv/lib/python3.10/site-packages/django/db/backends/dummy/base.py, line 20, in complain Raised during: rest_framework_simplejwt.views.TokenObtainPairView Python Executable: /home/k/PycharmProjects/ProjectPMS/.venv/bin/python Python Version: 3.10.12 http://localhost:8000/api/token i am accessing this endpoint inorder to obtain token for authorization in postman -
Django local server deployment on windows
I have a Django project that has to be installed on local servers for multiple customers. I've looked into utilizing Docker, but I can't find any solid information about how to use it on local hosting. My customers use computers running Windows. A friend advised that I utilize a virtual machine to run the Linux Server, then install my project with all dependencies, produce an image of that custom OS, then install it on more virtual computers. The issue that bothers me is consistency in installing and maintaining dependencies. I don't know whether you have any better suggestions. On the subject of virtual machines, my plan was for them to start automatically as soon as the computer turned on and operate the server that would be accessed via the Windows operating system. I'm concerned about whether autotation is possible, whether the server can be accessed by other computers, and whether the client machines are powerful enough to handle the virtualisation. I have installed before, the old school way (straight to the OS), but is a hustle. Im afraid if the server fails, reapeting the whole process of intalling dependences and configurations might take a lot of time while a client … -
Websocket handshake result in 404 not found
All http(s) request are working fine but whenever I try to make a websocket connection it results in the following: Request URL: https://my-domain.com/ws/listen?jwt=<token> Request Method: GET Status Code: 404 Not Found This is my project configuration: nginx upstream geej_app_server { server unix:/webapp/run/gunicorn.sock fail_timeout=0; } server { server_name my-domain.com; access_log /webapp/logs/access.log; error_log /webapp/logs/error.log; location /static/ { alias /webapp/geej_backend/static/; } location /media/ { alias /webapp/geej_backend/media/; } location / { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; if (!-f $request_filename) { proxy_pass https://geej_app_server; } } location /ws/ { proxy_pass https://geej_app_server; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "Upgrade"; proxy_set_header Host $host; } listen 443 ssl http2; listen [::]:443 ssl http2; ssl_certificate /etc/ssl/cert.pem; ssl_certificate_key /etc/ssl/key.pem; ssl_client_certificate /etc/ssl/cloudflare.crt; ssl_verify_client on; } server { listen 80; listen [::]:80; server_name my-domain.com; return 302 https://$server_name$request_uri; } supervisor [program:geej] command = /webapp/env/bin/gunicorn_start user = guser stdout_logfile = /webapp/logs/supervisor.log redirect_stderr = true environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8 gunicorn_start (bash file for starting) #!/bin/sh NAME='geej' DJANGODIR=/webapp/geej_backend SOCKFILE=/webapp/run/gunicorn.sock USER=geejuser GROUP=webapp NUM_WORKERS=3 DJANGO_SETTINGS_MODULE=geej.settings DJANGO_ASGI_MODULE=geej.asgi TIMEOUT=120 cd $DJANGODIR source ../env/bin/activate export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE export PYTHONPATH=$DJANGODIR:$PYTHONPATH RUNDIR=$(dirname $SOCKFILE) test -d $RUNDIR || mdkir -p $RUNDIR exec ../env/bin/gunicorn -k uvicorn.workers.UvicornWorker ${DJANGO_ASGI_MODULE}:application \ --name $NAME \ --workers $NUM_WORKERS \ --timeout $TIMEOUT \ --user=$USER --group=$GROUP \ --bind=unix:$SOCKFILE \ --log-level=debug \ --log-file=- routing.py … -
How to Version Control a PostgreSQL Table?
Backend: Python 3.11 and Django 5.0.6 Database: PostgreSQL 15 Our app deals with reporting from the database. We don't store report SQL queries in the codebase; instead, we store them in DB tables. However, developers change SQL queries from time to time. How can we track such changes made in the database, similar to using Git? For example, I want to see the query changes made in the database one year ago. How can I do that? Does anyone have any approach to do this? I tried to set up Liquibase, but it lacks support/documentation for a Python + Django app. Currently, what we are doing is we created a repo for the database, manually generating insert statements from the table and pushing them into the repo. But this is a hectic procedure. Some approaches that may work include using PostgreSQL triggers or logs. There are only some specific static tables that I want to track because some tables have data in the millions. If possible, I need an implementation similar to a version control tool like Git. Does anyone have a better approach to this problem? -
What does __code mean in Django?
What does __code mean in Django? I'm beginner find this code, but I don't now what does name__code mean. def get_queryset(self): names = self.request.query_params.get("name", None) if names: # qs = Hero.objects.filter(name=names) for name in names.split(self.names_separator): qs = Hero.objects.filter(name__code=name) # res = Hero.objects.get(name="Bad") # print(res) return qs I tried to find info in enthernet, but I can't -
Django decorator and middleware issue
My decorator looks like this def require_feature(feature_name): def decorator(view_func): print(f"process_view - view_func: {view_func}") # Debugging @wraps(view_func) def _wrapped_view(request, *args, **kwargs): return view_func(request, *args, **kwargs) _wrapped_view.required_feature = feature_name return _wrapped_view return decorator and the middleware looks like this class EntitlementMiddleware(MiddlewareMixin): def __init__(self, get_response) -> None: self.get_response = get_response def __call__(self, request) -> Any: if request.user.is_authenticated: request.user.features = get_user_features(request.user) else: request.user.features = [] return self.get_response(request) def process_view(self, request, view_func, view_args, view_kwargs): print(f"process_view - view_func: {view_func}") # Debugging required_feature = getattr(view_func, "required_feature", None) if required_feature and not request.user.features.filter(name=required_feature): raise PermissionDenied("You do not have access to this feature") return None # if none is returned then view is called normally def process_response(self, request, response): return response and this is how I am using it in my viewset class MyViewSet(ValidatePkMixin, viewsets.ViewSet): authentication_classes = [TokenAuthentication] permission_classes = [IsAuthenticated] queryset = My.objects.all() @method_decorator(require_feature("Basic")) def list(self, request): pass the decorator sets the required_feature when server starts. the middleware gets called when I make a /get call but this required_feature = getattr(view_func, "required_feature", None) returns None what am I missing here? -
Not loading static(CSS,JS,BOOTATRAP) files after applying "Empty Cache and Hard Reload"
I am working on django website,i made some changes in jquery file and then apply "Empty Cache and Hard Reload" in browser ,but now all the static files are not reflecting in browser, before clearing the cache all the files were working properly, What should be the issue here and how can I solve this I am unable to find the issue ,i don't think that the issue with any url, static or location of files as they were working properly before applying "Empty Cache and Hard Reload" ,and the error in console is "404" for all the static files like " GET http://127.0.0.1:8000/static/js/bootstrap.bundle.min.js net::ERR_ABORTED 404 (Not Found) 127.0.0.1/:19 GET http://127.0.0.1:8000/static/js/jquery-2.0.0.min.js net::ERR_ABORTED 404 (Not Found) " # settings.py DEBUG = True ALLOWED_HOSTS = [] # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'Category', 'accounts', 'store', 'cart', ] STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static') # urls.py urlpatterns = [ path('admin/', admin.site.urls), path('',index,name='index'), path('store/',include('store.urls')), path('cart/',include('cart.urls')), path('accounts/',include('accounts.urls')), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) # base.html {% load static%} <script src="{% static 'js/bootstrap.bundle.min.js' %}" type="text/javascript"></script> <link href="{% static 'css/bootstrap.css' %}" rel="stylesheet" type="text/css"/> <!-- Font awesome 5 --> <link href="{% static 'fonts/fontawesome/css/all.min.css' %}" type="text/css" rel="stylesheet"> -
How to retrieve project directory in MerginMaps?
I intend to develop a Django project by including MerginMaps Python API Client. I need to pull project history by entering project directory as a function parameter. How i can retrieve project destination in Mergin Maps -
How to make Django to increase a field by db value?
For example, I have model: class CacheMetaData(models.Model): count = models.IntegerField(default=0) def inc(self): self.count += 1 self.save() If I define method inc() like above, then Django will execute a SQL statement like: UPDATE the_table set count = 1 where id = 1 This way lead to race condition so that count might not be correctly increased. I want Django to execute: update the_table set count = count + 1 So that count will always be correctly increased. Can Django do that? If yes, how to define the field count? -
Django formset duplication
I have used inline formsets to create a form but for some reason the fields are duplicating and I can't figure out where I have made a mistake- Ignore the bad styling! Here is my CreateView from Views.py class RecipeInline(): form_class = RecipeForm model = Recipe template_name = "recipes/recipe_form.html" def form_valid(self, form): named_formsets = self.get_named_formsets() if not all((x.is_valid() for x in named_formsets.values())): return self.render_to_response(self.get_context_data(form=form)) self.object = form.save() for name, formset in named_formsets.items(): formset_save_func = getattr(self, 'formset_{0}_valid'.format(name), None) if formset_save_func is not None: formset_save_func(formset) else: formset.save() return redirect('recipes-home') def formset_variantingredients_valid(self, formset): variantingredients = formset.save(commit=False) for variantingredient in variantingredients: variantingredient.recipe = self.object variantingredient.save() def formset_images_valid(self, formset): images = formset.save(commit=False) for image in images: image.recipe = self.object image.save() class RecipeCreate(LoginRequiredMixin, RecipeInline, CreateView): def get_context_data(self, **kwargs): ctx = super(RecipeCreate, self).get_context_data(**kwargs) ctx['named_formsets'] = self.get_named_formsets() return ctx def get_named_formsets(self): if self.request.method == "GET": return { 'variantingredients': VariantIngredientFormSet(prefix='variantingredients'), 'images': ImageFormSet(prefix='images'), } else: return { 'variantingredients': VariantIngredientFormSet(self.request.POST or None, self.request.FILES or None, prefix='variantingredients'), 'images': ImageFormSet(self.request.POST or None, self.request.FILES or None, prefix='images'), } class RecipeUpdate(RecipeInline, UpdateView): def get_context_data(self, **kwargs): ctx= super(RecipeUpdate, self).get_context_data(**kwargs) ctx['named_formsets'] = self.get_named_formsets() return ctx def get_named_formsets(self): return { 'variants': VariantIngredientFormSet(self.request.POST or None, self.request.FILES or None, instance=self.object, prefix='variantingredients'), 'images': ImageFormSet(self.request.POST or None, self.request.FILES or None, instance=self.object, prefix='images'), … -
Django serializers unable to serialize a queryset into JSON
Trying simple Django serializer. I use a queryset, with specific values. Then I pass the queryset or the queryset.values() to the serializer. Why doe it give 500 error? @csrf_protect def geoLookup(request, **kwargs): country = kwargs.get('Country') city = kwargs.get('Place') queryset = GeoNames_location.objects.filter(geoCountry_code=country, feature_class='P', geoAsciiName__istartswith=city).values_list("geoAsciiName", "geoLongitude", "geoLatitude", "geo_timezone", "geo_population", "geoCountry_code").order_by('-geo_population')[:5] data = serializers.serialize("json", queryset.values()) # tried queryset and queryset.values() parsed_data = json.loads(data) pretty_json = json.dumps(parsed_data, indent=4) return JsonResponse(json.loads(pretty_json), safe=False) [24/Jul/2024 06:07:52] "GET /edit_chart_form HTTP/1.1" 200 18929 Internal Server Error: /geolookup/US/p Traceback (most recent call last): File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner response = get_response(request) ^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\decorators.py", line 188, in _view_wrapper result = _process_exception(request, e) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\decorators.py", line 186, in _view_wrapper response = view_func(request, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "G:\My Drive\myProgram\runtime\devui\ui\views.py", line 81, in geoLookup data = serializers.serialize("json", queryset.values()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\serializers\__init__.py", line 134, in serialize s.serialize(queryset, **options) File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\serializers\base.py", line 113, in serialize concrete_model = obj._meta.concrete_model ^^^^^^^^^ AttributeError: 'dict' object has no attribute '_meta' [24/Jul/2024 06:07:55] "GET /geolookup/US/p HTTP/1.1" 500 105779 -
Django REST framework serializer.is_valid() saves files to MEDIA_ROOT although .is_valid() is False
I have a project which supports file uploads via Django forms and also Django REST framework, these files are used are stored in a model 'Document'. The issue is that when a file that fails validation is uploaded via the REST framework the file is still saved to its 'upload_to' parameter (MEDIA_ROOT) (No 'Document' model instance is created as expected), this doesn't happen when uploading the same file via Django forms. Some testing seems to point to "serializer.is_valid()" being the thing that saves the file dispite "serializer.is_valid()" is false in the case i am refering to. My ideal outcome is that when a unacceptable file is uploaded via REST framework, that file is not present in the MEDIA_ROOT folder. Please leave a answer/comment if you have any advice. My code is below. Models.py: from django.db import models from apps.common.models import BaseModel from datetime import datetime import os from django.core.exceptions import ValidationError def file_storage_handler() -> str: """This function provides a file path to a Document model's FileField based on the current date. If the file path doesn't exist it creates it.""" currentDate = datetime.now().date() path = f"uploaded_documents/{currentDate.year}/{currentDate.month}/{currentDate.day}/" if not os.path.exists("media/"+path): os.makedirs("media/"+path) return path def validate_file(file): # I dont believe that the … -
OAuth2 where to store client id and secret when Application is created on server startup
I am using django-oauth-toolkit for authorization of my Django app, and for development, each dev deploys their server on Kubernetes with a MySQL database also deployed on the side as a StatefulSet. Many times me (or other devs who develop the application) have to remove their database and reinstall their k8s deployment. Usually (in a non k8s deployment and what is there in the quickstart guide), you would deploy your app, register the new client application through the UI provided by the django-oauth-toolkit, and then you get a one time generated client secret that you have to copy immediately otherwise it will be gone and you have to recreate the client. But this is inconvenient as on every new fresh install we have to keep doing this, and update the client_secret in the apps that use the authorization server with the new value. So I found a way to auto-register an OAuth2 client application as follows on post-migrate (this is a snippet, something like this) from oauth2_provider.models import Application @receiver(post_migrate) def initialize_client_applications(): Application.objects.create( client_type="confidential", authorization_grant_type="password", name="client_name", client_id='myComplexClientIdString", client_secret='myComplexClientSecretString", user=User.objects.get(name="someuser") ) But, as you can see, the client_secret is hard coded and therefore quite insecure. How can I do this using … -
Django 5.0: Configuring local development and production environments?
This question, in various forms, has been asked a few times. However, most answers on this topic are ten or more years old. The Two Scoops books I have are for Django 3.x. I think it might be useful to, perhaps, have updated answers relevant to Django 5.0 and hosting options available today. Here's one of the many older discussions I have reviewed: Django: How to manage development and production settings? I am particularly interested in the following scenario: Local development on Windows using PyCharm Using PostgreSQL from the start (rather than SQLite) Production deployment to Heroku, Render or PythonAnywhere I have my preferences when it comes to such things as the directory structure for the local environment. The standard layout for a Django-created application named "mypicturesite" is: mypicturesite/ code/ mypicturesite/ __init__.py settings.py urls.py wsgi.py <directories for other apps in the site> manage.py db.sqlite3 venv/ <various work directories>/ With this you end-up with paths that look like this: z:/mypicturesite/code/mypicturesite My approach looks like this: mypicturesite/ code/ # The entire codebase, including virtual environment site/ # This now makes sense; this contains the site code __config/ # These are the site configuration scripts __init__.py settings.py urls.py wsgi.py <directories for other apps … -
Multi Tenant Docker App with a Authentication Server and APIs
Hi I am working on a web project and my current stack is as follows. I have nginx for http server, nextjs for frontend and django as backend. Simple stuff. Where you can register a company and each company can have multiple users. As the project grew I wanted to instead of having a kitchen sink sort of backend app where there is one DB and all the users and companies are in it. I want to separate them but I am also going to dockerize so each tenant will have a docker app and a docker postgres for that docker app. I am also going to create another djano app just to register the tenants so that I can book keep. So it will look like below --NGINX --domain1.app --domain1.db --domain2.app --domain2.db --domain-registeration-app --frontend-nextjs-app domain-registeration-app app book keeps all the tenants and forks new docker instances for a new tenant and maybe handles payments for each tenant. Now comes the issue. This would work if a user is only in one tenant and will likely remember the subdomain for the tenant and can go to that tenants app by typing tenant1.myapp.com in urk and nginx would route that to … -
How to "fix" user input on the django admin page datetime input
I have a model Itineraries that has a tabularInline model ItineraryDetails that has destination, arrival, and departure times. I want to allow users to input 09302025 and 700 for each datetime field but I can't seem to get it to work. Django admin looks for valid datetime objects before saving and since these are not valid, they can't be saved. I've tried save_model and save_related, but they won't accept the invalid datetime format -
Difference interpretating env variable in django and celery
I had issue with env variable with backslash. I had: NETWORK=\\TI\folder But in Django and Celery this variable will be interpreted in different ways: Django: \\TI\folder Celery: \TIfolder Why I has difference ? Celery and Django has same env file and environment. Python 3.8 -
Queryset annotated with Count is counting wrong occurrences
I want to count the number of times a name is present on a queryset: items = items.values("name").annotate(count=Count("name")).order_by("-count") This should return a query set like this: [ { "name": "Item 1", "count": 1 }, { "name": "Item 2", "count": 4 }, { "name": "Item 3", "count": 12 }, ] This is returning some wrong counts, for example, items that should have 1 occurrence is counting 17, and items that should have 3 occurrences is counting 51, for some reason is multiplying it by 17. How can I solve this? -
VoIP push notifications with django to iOS
Currently, I am using fcm-django to send fcm notifications to both android and iOS and for iOS, i set header apns-push-type: voip. Is it enough for a notification to be treated as voip push notification. On client side, i am using react-native-voip-push-notification, but in the listeners, i am not receiving any notification object. So, How to properly send a voip push from django and handle it on react native side. -
Retrieving image with javascript to display Django
I am trying to retrieve an image via Javascript function that calls a function from my views that has the image. I need help with populating the image: Views: def view_company_logo(request): print('GETTING LOGO') client = Client.objects.get(user=request.user) logo ='' try: logo= client.customer.first().logo.logo print('GOT LOGO') return JsonResponse({'logo':logo, 'media_url':settings.MEDIA_URL}, safe=False) except Exception as e: print(e) return JsonResponse({'logo':logo}, safe=False) HTML File: I am using the image id "company_logo" here: In the bottom, I am using Javascript: <script type="text/javascript"> function get_company_logo() { $.ajax({ url: '/get/company_logo/', method: 'GET', success: function (data) { console.log(data['logo']) display_image(data['logo']) } }) } function display_image(image){ document.getElementById("company_logo").src = image.url; } get_company_logo(); </script> Called by URL: url(r"^get/company_logo/$", views.view_company_logo), How do I get the image to display? -
page-break not working on headings ( python, weasyprint )
I want to create a pdf out of html content. I use beautifulsoup to get html string and then create PDF using weasyprint and python out of it. I have issue when i want to show headings ( specifically h3 ) on a new page ( though this doesn't work for any of the h tags ). Now, I have this code in html {% block report %} <style> h3 { page-break-before:always; } </style> <div class="container" style="padding-top:45px"> <div class="row"> <div class="col-12"> <h1 id="h1-title" class="mb-5">{{report.title|safe}}</h1> <div class="control-head"> <h3 id="1.-first-section">1. First Section</h3> <hr> <p>{{obj.some_content_one|safe}}</p> </div> <div class="control-head"> <h3 id="2.-second-section">2. Second Section</h3> <hr> <p>{{obj.some_content_two|safe}}</p> </div> <div class="control-head"> <h3 id="3.-third-section">3. Third Section</h3> <hr> <p>{{obj.some_content_three|safe}}</p> </div> <div class="control-head"> <h3 id="4.-fourth-section">4. Fourth Section</h3> <hr> {{obj.some_content_four|safe}} </div> </div> </div> </div> {% endblock report %} And when i use page-break-after in h3 tags, weasyprint only renders content until this first h3 including the h3.string... If i use page-break-before: always; then weasyprint renders first "control-head" and {{obj.some_content_one|safe}} and stops rendering second at the end of the page where second h3 is ( it basically renders second control-head h3.string and content from {{obj.some_content_two|safe}} until it fills the page and then stops rendering pdf any further).. I tried using page-break-after/before on … -
test if any object refer to by a base model has a value specified in django orm
Well consider two models: class School(models.Model): name = models.TextField(default="hello world") class Student(models.Model): key = models.TextField(default="somedata") a = models.ForeignKey(Vendor) So a many to one relationship from School to Student How would I get (from School's perspective) all School which have a Student that set "key" to the required value? Especially in the already existing query: School.objects.filter(Q(name="hello world") | Q(queryshould_here)) I've tried A.objects.filter(Q(name="hello world") | Q(student__key="test")) However that (obviously) fails with: django.core.exceptions.FieldError: Unsupported lookup 'v' for ManyToOneRel or join on the field not permitted.