Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Filter a Query Set by Reverse Foreign Key
If I have the following: class Asset(models.Model): name = models.TextField(max_length=150) project = models.ForeignKey('Project') class Project(models.Model): name = models.TextField(max_length=150) What argument do I pass to Project.objects.filter() to get all Projects that have no associated Assets. -
django runserver in http
I'm a little new to django so bear with me. I have an app that's running just fine in HTTPS. In settings.py I have this bit of line that enables it: SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') SECURE_SSL_REDIRECT = True SESSION_COOKIE_SECURE = True CSRF_COOKIE_SECURE = True Out of curiosity, instead of disabling this every time to test pages and applications, is there a way to enable http for only 127.0.1:8000 and not *.example.com? Or is this a bad practice to even attempt? -
I am getting same problem while generating test api key
Please help to resolve this error I opened Paytm dashboard and visited to test api details but it shows nothing. I want test api merchant Id and key. Please help -
Ordering a QuerySet by the first object in a prefetch_related query
If I have the following models, describing Books which can have several different Titles: class Book(models.Model): publisher = models.CharField(max_length=255) class BookTitle(models.Model): work = models.ForeignKey(Book, related_name="titles", on_delete=models.CASCADE) title = models.CharField(max_length=255) I'm fetching a QuerySet of Books, pre-fetching their BookTitles, like this: from django.db.models import Prefetch books = Book.objects.prefetch_related( Prefetch( "titles", queryset=BookTitle.objects.all() ) ) I'd like to be able to order the QuerySet by the first of the BookTitles. How would I do that? (This is a very simplified example of my actual code to explain the basic question.) -
Django IntegrityError at admin, null value of id violates the not-null constraint
I am fairly new to Django and I use Django 4.2 and PostgreSQL's latest version. I have this issue with my code, I have created models for a blog post. In the admin, I am trying to test it out to see if everything is working the way I expect it to, but I have reached an error. I am trying to add tags to the blog post. That is like tags that describe what kind of genre the blog post is. This is what my models.py file looks like: class BlogPost(models.Model): owner = models.ForeignKey(Profile, null=True, blank=True, on_delete=models.SET_NULL) id = models.UUIDField(unique=True, primary_key='True', editable=False) post_title = models.CharField(max_length=300, blank=True, null=True, verbose_name='Title of blog post') post_image = models.ImageField(upload_to='posts/%Y/%m/%d', default='static/images/avatar.jpg', blank=True, null=True, verbose_name='Blog post picture') tags = models.ManyToManyField('Tags', blank=True, verbose_name='Tags seperated by #') post_body = models.TextField(blank=True, null=True, verbose_name='body of user') votes_total = models.IntegerField(default=0, blank=True, null=True) created_time = models.DateTimeField(auto_now_add=True) def __str__(self) -> str: return self.post_title class Tags(models.Model): id = models.UUIDField(editable=False, unique=True, primary_key=True) tag = models.CharField(max_length=200) def __str__(self) -> str: return self.tag class Comments(models.Model): blogpost = models.ForeignKey(BlogPost, on_delete=models.CASCADE, blank=True, null=True) comment_body = models.TextField(blank=True, null=True) VOTE_TYPE = ( ('up', 'UP VOTE'), ('down', 'DOWN VOTE'), ) value = models.CharField(max_length=200, choices=VOTE_TYPE) id = models.UUIDField(editable=False, unique=True, primary_key=True) def __str__(self) -> … -
getting model object returned matching query does not exist
I want to display what user is posted from a Book model when others user visited his public_profile page, to do that, i had to use this method: def public_profile(request, slug): profile = get_object_or_404(Profile, slug=slug) book = Book.objects.get(slug=slug) context = { 'profile': profile, 'book': book } return render(request, 'public_profile.html', context) But using that method returned: Book matching query does not exist. in the public_profile template. my url to public_profile page: <p class="title is-4"><a href="{{ book.get_user_public_url }}"> {{ book.user }} </a></p> my model class Book(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) audio = models.FileField(upload_to='audio') title = models.CharField(max_length=50) category = models.ForeignKey(Category, on_delete=models.CASCADE) image = models.ImageField(upload_to='audio-image') introduction = models.TextField(max_length=500) slug = models.SlugField(unique=True) def get_user_public_url(self): return reverse('Public-Profile', kwargs={'slug': self.user.profile.slug}) class Profile(models.Model): user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) profile_photo = models.ImageField(upload_to='profile-image', default='static/dafault.jpg') twitter = models.URLField(blank=True, null=True, unique=True) website = models.URLField(blank=True, null=True, unique=True) linkedln = models.URLField(blank=True, null=True, unique=True) country = models.CharField(max_length=70, blank=True, null=True,) about = models.TextField(max_length=700, blank=True, null=True) slug = models.SlugField(max_length=100, unique=True) -
How to handle errors correctly in django?
I have a question, I know that it is possible to do global exception handling in python, but how to do it for all views, namely, I am interested in DetailView, but others will also come in handy.I understand that you need to give your own examples of solutions, but could you give an example of how this can be done, because I have no ideas at all how to do it. I want to do global error handling so that it can be done like this try: current_profile = Profile.objects.get(user_connected_id=current_user_object) except ObjectDoesNotExist SomeERROR....: logger.info('Object user_connected_id not found....') raise ObjectDoesNotExist And if I don't handle the error that appears, then I want it to work in the global handler. And if it's not difficult: what should I do correctly with errors in this handler, other than logging? I need the DetailView handler most of all -
AttributeError: 'NumberObject' object has no attribute 'indirect_reference'
I've been trying to create multiple pdfs and merge them using pypdf (Version 3.7.1). The static data such as images and fonts render out perfectly within the html but don't in pdfs. Following is my code and the error I'm encountering query_set = booklet.objects.filter(booklet_id = id) combine = [] for i in query_set: size = i.size options = { 'page-size': f'{size}', 'orientation': 'Landscape' if i.template_mode == "2" else "Portrait", 'margin-top': '0mm', 'margin-right': '0mm', 'margin-bottom': '0mm', 'margin-left': '0mm', 'encoding': "UTF-8", 'dpi': 900 } url = f"http://ipaddress/test?ids={get_ids}&uuid={i.uuid}&size={i.size}&mode={i.mode}&type_set={i.te_type}&template={i.name}&pre{id.id}" pdfkit.from_url(url, output_path=f"{file_path}/pdfs/{i.te_type}_{i.name}_{i.id}_.pdf", options=options, verbose=False) combine.append(f"{file_path}/pdfs/{i.te_type}_{i.name}_{i.id}_.pdf") merger = PdfWriter() for pdfs in combine: merger.append(pdfs) pdf_name = "base.pdf" merger.write(pdf_name) merger.close() return JsonResponse({"pdf" : pdf_name"}) I have tried cnfiguring the nginx file, thinking it could be the case but it is not I have tried modifying my code and relocating static folder to no success. I have tried rendering static images into a separate template but as i mentioned above they render in just fine but not in the pdf generated by pypdf -
Pycharm professional syntax highlighting for JS contained in a block inside a script tag
While writing JS for a Django application using the Django templating language, I am getting a weird formatting problem as below: Defining a domready block in the base.html file: <script> const csrftoken = Cookies.get('csrftoken'); document.addEventListener('DOMContentLoaded', (event) => { //DOM loaded {% block domready %} {% endblock %} }) </script> Using the block in another template file list.html: {% extends "base.html" %} {% block domready %} var page = 1; var emptyPage = false; var blockRequest = false; window.addEventListener('scroll', function (e) { var margin = document.body.clientHeight - window.innerHeight - 200; if (window.pageYOffset > margin && !emptyPage && !blockRequest) { blockRequest = true; page += 1; fetch('?images_only=1&page=' + page).then(response => response.text()).then(html => { if (html === "") { emptyPage = true; } else { var imageList = document.getElementById('image-list'); imageList.insertAdjacentHTML('beforeend', html); blockRequest = false; } }) } }); // start a synthetic scroll event on page load so that the next page loads in case the browser window is big enough const scrollEvent = new Event('scroll'); window.dispatchEvent(scrollEvent); {% endblock %} Pycharm's formatting for JS code is not working in the list.html file. Is there any way to force Pycharm Professional to highlight the syntax and format the JS code in the domready block … -
Invalid JSON error reported whenever a PointField is saved with the GooglePointFieldWidget in the Django admin
I am using Django 4.1 and GooglePointFieldWidget (from django-map-widgets) on PointFields (from django-geojson). The widget gets displayed as it should and I'm able to change the coordinates, set a marker on the map or even type in an address. All good. The problem is that I'm getting a validation error upon saving the object because GooglePointFieldWidget fills in the hidden textarea of the field with invalid JSON, something like POINT (26.2616671966018 65.06716787213769) when Django expects something like {'type': 'Point', 'coordinates': [26.2616671966018, 65.06716787213769]} How can I solve this validation error? -
Wagtail 500 server error with DEBUG=False
I deployed the Wagtail site on Ubuntu 22.04 following digitalocean's instructions (Nginx + Gunicorn). The site works without errors when DEBUG=True, but when I put False I get Server Error (500) when going to any page. At the same time admin panel works without errors, path to static files is correct. Error.log nginx are clean. What can be the problem? I have already tried everything. gunicorn.service: [Unit] Description=gunicorn daemon Requires=gunicorn.socket After=network.target [Service] User=user1 Group=www-data WorkingDirectory=/home/user1/projectdir ExecStart=/home/user1/projectdir/env/bin/gunicorn \ --access-logfile - \ --workers 5 \ --bind unix:/run/gunicorn.sock \ --env DJANGO_SETTINGS_MODULE=projectname.settings.production \ projectname.wsgi:application [Install] WantedBy=multi-user.target nginx config: server { listen 80; server_name mysite.com www.mysite.com; location = /favicon.ico { access_log off; log_not_found off; } location /static/ { alias /home/user1/projectdir/static/; } location /media/ { alias /home/user1/projectdir/media/; } location / { include proxy_params; proxy_pass http://unix:/run/gunicorn.sock; } } -
How do I fix this errors when running my tests
from django.conf import settings from django.utils.functional import cached_property from regulus_oscar.apps.catalogue.abstract_models import AbstractProduct class Product(AbstractProduct): def __str__(self): """ Appends the variant-identifier product attribute values to the string representation of child products, for use in multiple places, including on the front end and dashboard, and in emails to customers and the merchant. """ if self.is_child and self.variant_attribute_summaries: # The parent "__str__" method appends a summary of all the product # attribute values, whereas we only want to include a summary of the # variant-identifier ones return f"{ self.title if self.title else self.parent.title } ({ self.variant_attribute_summaries })" else: return super().__str__() def get_title(self): """ Appends the variant-identifier product attribute values to the title of child products, for use in multiple places, including on the front end and dashboard, and in emails to customers and the merchant. """ title = super().get_title() if self.is_child and self.variant_attribute_summaries: title = f"{ title } ({ self.variant_attribute_summaries })" return title get_title.short_description = "Title" @cached_property def variant_attribute_summaries(self): """ Returns a string of a product's variant-identifier ("Colour" and "Size") product attribute values. """ return ", ".join( [ product_attribute_value.summary() for product_attribute_value in self.get_attribute_values().filter( attribute__name__in=settings.NSH_VARIANT_PRODUCT_ATTRIBUTE_NAMES ) ] ) from regulus_oscar.apps.catalogue.models import * # noqa isort:skip Above is my models.py file and everything works as … -
Django- Object of type Product is not JSON serializable
Environment: Request Method: POST Request URL: http://127.0.0.1:8000/orders/create/ Traceback (most recent call last): File "C:\Users\shafquet Naghmi\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\exception.py", line 55, in inner response = get_response(request) File "C:\Users\shafquet Naghmi\AppData\Local\Programs\Python\Python310\lib\site-packages\django\utils\deprecation.py", line 138, in __call__ response = self.process_response(request, response) File "C:\Users\shafquet Naghmi\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\sessions\middleware.py", line 59, in process_response request.session.save() File "C:\Users\shafquet Naghmi\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\sessions\backends\db.py", line 82, in save obj = self.create_model_instance(data) File "C:\Users\shafquet Naghmi\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\sessions\backends\db.py", line 69, in create_model_instance session_data=self.encode(data), File "C:\Users\shafquet Naghmi\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\sessions\backends\base.py", line 94, in encode return signing.dumps( File "C:\Users\shafquet Naghmi\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\signing.py", line 150, in dumps return TimestampSigner(key, salt=salt).sign_object( File "C:\Users\shafquet Naghmi\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\signing.py", line 228, in sign_object data = serializer().dumps(obj) File "C:\Users\shafquet Naghmi\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\signing.py", line 125, in dumps return json.dumps(obj, separators=(",", ":")).encode("latin-1") File "C:\Users\shafquet Naghmi\AppData\Local\Programs\Python\Python310\lib\json\__init__.py", line 238, in dumps **kw).encode(obj) File "C:\Users\shafquet Naghmi\AppData\Local\Programs\Python\Python310\lib\json\encoder.py", line 199, in encode chunks = self.iterencode(o, _one_shot=True) File "C:\Users\shafquet Naghmi\AppData\Local\Programs\Python\Python310\lib\json\encoder.py", line 257, in iterencode return _iterencode(o, 0) File "C:\Users\shafquet Naghmi\AppData\Local\Programs\Python\Python310\lib\json\encoder.py", line 179, in default raise TypeError(f'Object of type {o.__class__.__name__} ' Exception Type: TypeError at /orders/create/ Exception Value: Object of type Product is not JSON serializable I am trying to serialize it using product=json.dumps(item['product'],cls=DjangoJSONEncoder) but i still get the same error. There are many questions like these but i am not able to connect the dot.Thanks def create_order(request): cart=Cart(request) if request.method=='POST': form=OrderCreateForm(request.POST) if form.is_valid(): order=form.save() for item in cart: #product=json.dumps(item['product'],cls=DjangoJSONEncoder) … -
How to get (Parent) objects by filtering its child model?
I have researched some (answer, answer)s but didn't get what I want. So I have two models: models.py class TvProgram(TimeStampedModel): name = models.CharField(max_length=512) class TvProgramSchedule(TimeStampedModel): tvprogram = models.ForeignKey( to=TvProgram, on_delete=models.CASCADE ) schedule = models.DateTimeField() I want to get Tvprograms (Parent) which its children's schedules are only for the 7 days, In my case I am getting tvprograms with its all children. Is there a way to retrieve all the Parent objects that are in the filtered querysets? -
How to pass class name with model to the Django Admin site register?
I have a couple of models and I created some classes in admin.py for the column list view as below: class AppleAdmin(admin.ModelAdmin): pass class OrangeAdmin(admin.ModelAdmin): pass class BananaAdmin(admin.ModelAdmin): pass class GrapesAdmin(admin.ModelAdmin): pass I'm trying to concatenate the model name with Admin (AppleAdmin) and pass the class name to the register parameter. for model in app_modules: ma = str(model.__name__+'Admin') if ma : try: admin.site.register(model, ma) except: pass Any help would be much appreciated. -
Django does not have access to environment variable
I want to deploy my Django site to DigitalOcean App Platform so I copy the settings from their official sample app (https://github.com/digitalocean/sample-django/blob/main/mysite/settings.py). And here's the problematic part: ... import os ... # Database # https://docs.djangoproject.com/en/4.2/ref/settings/#databases if os.getenv('DJANGO_DEVELOPMENT_MODE', 'False') == 'True': DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', } } elif len(sys.argv) > 0 and sys.argv[1] != 'collectstatic': if os.getenv('DATABASE_URL', None) is None: raise Exception('DATABASE_URL environment variable is not set') DATABASES = { 'default': dj_database_url.parse(os.environ.get('DATABASE_URL')), } When I fire up the development server, I get this error: ... raise Exception('DATABASE_URL environment variable is not set') Exception: DATABASE_URL environment variable is not set This tells me the if statement is evaluated to False even though I have this environment variable: I create a same System variable: But I get the same error. Now I go to Python shell and test this: Python 3.11.3 (tags/v3.11.3:f3909b8, Apr 4 2023, 23:49:59) [MSC v.1934 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> print(os.getenv('DJANGO_DEVELOPMENT_MODE', 'False') == 'True') True >>> print(os.getenv('DJANGO_DEVELOPMENT_MODE')) True >>> The environment variable looks fine. At this point I go back to settings.py and edit the if statement like this: ... … -
How can i fix the 'Web application could not be started' error in passenger_wsgi.py
So, i try to install django and python on my server and get this error. Error 'ModuleNotFoundError: No module named 'django'' File passenger_wsgi.py looks so: passenger_wsgi.py. I also check path to django and looks like here it is. django module is here Help, pls! I tried to reinstall all modules, but it also doesn't work. -
Invalid redirect_uri in instagram oauth
I am using python django and django-allauth to implement oauth to instagram. I think I have set all the required things in instagram app setting and got the application id and secret. This is my app setting and I have added in my project to work with this configuration. This is my views.py from django.views.generic import TemplateView from allauth.socialaccount.models import SocialAccount class ConnectInstagram(TemplateView): template_name = 'connect.html' And connect.html file looks like this {% load socialaccount %} <a href="{% provider_login_url 'instagram' %}"> Connect with Instagram </a> I have already registered social applications in django admin site with site and application id and secret. This is my settings.py file import os from dotenv import load_dotenv from pathlib import Path load_dotenv() # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent INSTAGRAM_APP_ID = os.getenv('INSTAGRAM_APP_ID') INSTAGRAM_APP_SECRET = os.getenv('INSTAGRAM_APP_SECRET') # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = os.getenv('SECRET_KEY') # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS = ['41b3-107-155-105-218.ngrok-free.app'] CSRF_TRUSTED_ORIGINS = ['https://41b3-107-155-105-218.ngrok-free.app'] # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', # 3rd party 'django.contrib.sites', 'allauth', … -
How to edit claims in ID Token supplied with openid scope in django Oauth toolkit?
When working with Django OAuth Toolkit, using OIDC if you supply openid as the scope in request you get a id_token with access token. This ID Token can initially be used to identify the user you have got access token for and also create a session. { "aud": "audience", "iat": 1681801110, "at_hash": "hash of included access token", "sub": "1", "given_name": "Dushyant", "preferred_username": "dsh", "iss": "authEndpointurl", "exp": 1681837110, "auth_time": 1681567804, "jti": "tokenid" } But I wish to modify the claims in this JWT ID Token, it reveals the primary key of the database of Authorization Server as the unique id of the user which i don't want in the claim called sub. I want to use another unique key as the value of sub claim. I tried overriding the response a Custom Backend: class CustomOAuthLibCore(OAuthLibCore): def __init__(self, server): super().__init__(server) def create_token_response(self, request): response_data = super().create_token_response(request) #Modify the response here return response_data And added this as the Custom Backend in the settings. "OAUTH2_BACKEND_CLASS":"pathto.CustomOAuthBackend", I haven't written any modification code for the response here since the token here is already created. I want to intercept this at earlier stage where the claims dict is being made. Where and how can I achieve this … -
How to insta load css files
I`m using select2 in my django project and i override some css styles in my own style.css which i load in header And when you refresh page or load page, page shows me default css styles from this package for like 0.5 seconds but for user this is really a lot and im wondering how to load css styles instant or something like that I know that i can add my custom css style in my settings like SELECT2_CSS =['custom_css.css',] but it will delete all css styles from original css How can i solve that problem I load my custom css like this template.html <link rel="stylesheet" href="{% static 'css/django-select2/select2-theme.css' %}"> <script> $(document).ready(function () { $("#id_presence_info__object_presence").select2(); }); </script> -
Djnago path attribute is set to root "/"
I have no idea how to fix it. The URL path that the cookie is valid for can be specified. If the domain and path match, then the cookie will be sent in the request. If the path attribute is set too loosely, then it could leave the application vulnerable to attacks by other applications on the same server If the path attribute is set too loosely, then it could leave the application vulnerable to attacks by other applications on the same server which may help attacker to establish the authenticated session with other applications hosted under default directory Set the appropriate path in cookie interface I have tried doing this in settings.py CSRF_COOKIE_PATH = '/test/' but if 'mydomain/test' path exists then only it works for this path. otherwise for any other path, it do not show csrf_token and when I keep it default CSRF_COOKIE_PATH = '/' it gives as below screenshot cookies section I tried CSRF_COOKIE_PATH = '/test/' what I actually want is to change path from '/' to any other path if I hit any path -
Download the file from the server or output an error if there is one
I have something like this Python code: with open('test.bin', 'rb') as file: file_content = file.read() response = HttpResponse(file_content, content_type='application/octet-stream') response['Content-Disposition'] = f'attachment; filename=test.bin' return response There may not be a file, so you should return an error somewhere, but how to do it so that it can be read on Ajax and output to the user. $.ajax({ url: 'test', type: 'GET', headers: { 'X-CSRFToken': getCookie('csrftoken'), }, xhrFields: { 'responseType': 'blob' }, success: function(data, status, xhr) { var contentDisposition = xhr.getResponseHeader('Content-Disposition'); var matches = /filename=([^;]+)/ig.exec(contentDisposition); var fileName = (matches[1] || 'untitled').trim(); var blob = new Blob([data], {type: xhr.getResponseHeader('Content-Type')}); var link = document.createElement('a'); link.href = window.URL.createObjectURL(blob); link.download = fileName; link.click(); }, error: function(xhr, status, error) { }, }); But the fact is that because of the xhrFields field, the data is perceived as a blob, so in xhr.responseText(type string) I get [object Blob] How do I get the file downloaded and the error output in case of an error? -
How to use filter to check if strings stored in a Django array field are a substring of the input string
Here is how my model looks like. class TestModel(models.Model): test_field= ArrayField(models.CharField(max_length=255, null=True, blank=True),default=list) I want to filter objects which have any substring of the input string as an entry in their test_field. Example: Assume a TestModel object1 has the ['good','morning'] stored in its test_field. object2 has ['run','crew']. Input String is 'Good Evening'. The output should return a query set containing object1. -
how to call value of request.session in django forms
i didnot get item list in outward forms which comes from store items database enter image description here -------------------------forms.py---------------------------- class OutwardForm(forms.Form): item = forms.ModelChoiceField(queryset=StoreItems.objects.none()) quantity = forms.IntegerField() name = forms.CharField() emp_id = forms.IntegerField() def __init__(self, *args, **kwargs): self.warehouse_id = kwargs.pop('warehouse_id', None) super().__init__(*args, **kwargs) if self.warehouse_id: self.fields['item'].queryset = StoreItems.objects.filter(warehouse_id=request.session['warehouse_id']) def clean(self): cleaned_data = super().clean() # process cleaned_data as before return cleaned_data ---------------------views.py---------------------------- def outward(request): warehouse_id = request.session.get('warehouse_id') message = "" form = OutwardForm(request.POST or None, warehouse_id=warehouse_id) if request.method == 'POST': # form = OutwardForm(request.POST) form.request = request if form.is_valid(): item = form.cleaned_data['item'] quantity = form.cleaned_data['quantity'] name = form.cleaned_data['name'] emp_id = form.cleaned_data['emp_id'] opr_name = request.user.username if item.quantity >= quantity: item.quantity -= quantity item.save() record_transaction(item, quantity, name, emp_id, 'outward', opr_name, warehouse_id) message = "Success" form = OutwardForm() else: message = "Quantity must not be less than available quantity" else: form = OutwardForm() context = { # 'items': StoreItems.objects.filter(warehouse=warehouse_id), 'form': form, 'message': message } return render(request,'outward.html',context) -------------------------outward.html---------------------- <h1>Outwoard Item</h1> {% csrf_token %} Item Name : {{form.item}} Quantity : {{form.quantity}} Name of User : {{form.name}} Employee ID : {{form.emp_id}} -
Optimize Supabase insertion
I'm writing a code which is fetching around 100k data from Supabase and inserting it into default Django database and it's taking too much time. Please help me in making it faster. I am using two Supabase databases; one contains data and the other is the default database of Django. I'm also using Multithreading in this code. from django.db import connections from django.core.management.base import BaseCommand import json from concurrent.futures import ThreadPoolExecutor from api.GAmodel import Organization,Client from django.utils import timezone from api.GoogleConsoleModel import GC_Search_AB_Queries, GC_Search_AB_Pages, GC_Search_AB_Country, GC_Search_AB_Device class Command(BaseCommand): help = 'Inserts JSON data into the ModelName model' def handle(self, *args, **options): start=timezone.now() other_db_connection = connections['other'] # Define the SQL query to retrieve table names sql_query = """ SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_name IN ('_airbyte_raw_search_analytics_by_query', '_airbyte_raw_search_analytics_by_page','_airbyte_raw_search_analytics_by_device','_airbyte_raw_search_analytics_by_country') ORDER BY table_name; """ org_name="abc" org_table,created_org=Organization.objects.get_or_create(company=org_name) try: with other_db_connection.cursor() as cursor: cursor.execute(sql_query) rows = cursor.fetchall() # Extract table names from the query results table_names = [row[0] for row in rows] with ThreadPoolExecutor() as executor: futures = [] for table_name in table_names: futures.append(executor.submit(self.process_table, table_name, org_table)) # Wait for all futures to complete for future in futures: future.result() except Exception as e: print(f"Error occurred: {e}") end = timezone.now() print(f"Total time taken: {end-start}") …