Django community: Django Q&A RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Suggest Front-End Technology for Django monolith Site to Webservice architecture Conversion
Converting Django monolith Site to Webservice architecture. Which front-end tecnology I sould use. Few in my mind are Angular: Framework, with most of the features inbuilt, but steep learning curve React: Liberary, easy to start with, lots of package available Vue: Quick and easy implemenation. Major selection factors are easy to implement and future prospects Please suggest I tried with angular, found it steep learning curve, vue.js is good but not very popular and react.js is library, which require additional package installation for site to work. -
Django: ordering numerical and string value with order_by
My problem is, that obviously the data is ordered by UTF-8 codes since it's a Charfield, with the predictable results .. it sort the data like this; My Data is: 1,8,7,2a, 4, 2, 11, 10... I want this ordered list: 1,2,2a,4,7,8,10,11 .. I really don't know how I can order this properly .. -
Typeerror at /blog/: expected string or bytes-like object, got 'QuerySet'
I am Trying To Create A Basic Blog Website with multiple urls working in django. For the Blog Page I have created a Custom template tag which will return the first few words from the content data of blog from Models. But when i am trying to execute it always displaying the same error 'expected string or bytes-like object, got 'QuerySet' This Is My :Models.py from django.db import models # Create your models here. class blogdata(models.Model): post_id=models.AutoField(primary_key=True) title=models.CharField(max_length=50,default="") heading=models.CharField(max_length=500 ,default="") content=models.CharField(max_length=900 ,default="",null=True) date=models.DateField() thumbnail=models.ImageField(upload_to='static/',default="") def __str__(self): return self.title This is my post_tags.py import re from django import template from ..models import blogdata register=template.Library() @register.simple_tag def FewFirst(): my_str=blogdata.objects.values_list('content') result = re.findall(r'\w+', my_str)[:10 ] result = " ".join(result) return result Note: Directory structure and file names are correct idk the main reason behind this error -
Jenkins unable to connect to github private repo
I'm running Jenkins on an AWS Amazon Linux 2 EC2 instance. I have setup the instance and have jenkins running as well. I'm currently trying to add the job to deploy my private repo (Django web app) to the EC2 instance. I have used ssh-keygen and placed the public key in my github SSH keys and the private key in my credentials within Jenkins. I have been researching and almost every source does exactly what I've been doing. Any ideas on how to resolve this? I know that github changed some of their login capabilities in the last few years, so i'm not sure if these posts are outdated. This is the error I'm getting You can see my credentials config below: And my github ssh key here: -
Django nested OuterRef
Trying to understand and get working nested OuterRef on simple example (just for this test) class User(AbstractUser): pass class Issue(models.Model): due_date = models.DateField() class UserIssue(models.Model): issue = models.ForeignKey(Issue, on_delete=models.CASCADE) user = models.ForeignKey(User, on_delete=models.CASCADE) assigned_to = models.DateField() class Calendar(models.Model): calendar_date = models.DateField() @classmethod def report(cls): main_qs = cls.objects.all() second_subquery = ( Issue.objects.filter(due_date=OuterRef(OuterRef("calendar_date"))) .values("due_date") .annotate(count=Count("due_date")) .values("count") ) first_subquery = ( UserIssue.objects.filter(assigned_to=OuterRef("calendar_date")) .annotate(today_due_date=Sum(second_subquery)) .values("today_due_date") ) qs = main_qs.annotate(today_due_date=first_subquery) print(qs.query) return qs When call Calendar.report() got django.db.utils.ProgrammingError: column "users_calendar.id" must appear in the GROUP BY clause or be used in an aggregate function and have no idea how to fix it. Printed SQL is SELECT "users_calendar"."id", "users_calendar"."calendar_date", (SELECT SUM((SELECT COUNT(U0."due_date") AS "count" FROM "users_issue" U0 WHERE U0."due_date" = ("users_calendar"."calendar_date") GROUP BY U0."due_date")) AS "today_due_date" FROM "users_userissue" V0 WHERE V0."assigned_to" = ("users_calendar"."calendar_date") GROUP BY V0."id") AS "today_due_date" FROM "users_calendar" and when manually add GROUP BY "users_calendar"."id"; then it works so my question is how to achieve the same using ORM? Thanks -
Elastic BeansTalk: Yum does not have this package available for installation
this is 01_packages.config file inside .ebextensions folder packages: yum: git: [] postgresql93-devel: [] commands: 01_python_devel_install: command: 'yum install -y python39-devel' 02_postgresql_install: command: 'yum install -y postgresql93 postgresql93-devel' when i do eb deploy I get error in the logs (cfn-init.txt, to be precise): 2023-06-23 00:34:37,835 [INFO] -----------------------Starting build----------------------- 2023-06-23 00:34:37,853 [INFO] Running configSets: _OnInstanceBoot 2023-06-23 00:34:37,856 [INFO] Running configSet _OnInstanceBoot 2023-06-23 00:34:37,859 [INFO] Running config AWSEBBaseConfig 2023-06-23 00:34:38,510 [INFO] Command clearbackupfiles succeeded 2023-06-23 00:34:38,514 [INFO] Running config AWSEBCfnHupEndpointOverride 2023-06-23 00:34:38,533 [INFO] Command clearbackupfiles succeeded 2023-06-23 00:34:38,535 [INFO] ConfigSets completed 2023-06-23 00:34:38,535 [INFO] -----------------------Build complete----------------------- 2023-06-23 00:36:16,303 [INFO] -----------------------Starting build----------------------- 2023-06-23 00:36:16,309 [INFO] Running configSets: Infra-EmbeddedPreBuild 2023-06-23 00:36:16,311 [INFO] Running configSet Infra-EmbeddedPreBuild 2023-06-23 00:36:16,311 [INFO] ConfigSets completed 2023-06-23 00:36:16,311 [INFO] -----------------------Build complete----------------------- 2023-06-23 00:41:25,793 [INFO] -----------------------Starting build----------------------- 2023-06-23 00:41:25,800 [INFO] Running configSets: Infra-EmbeddedPreBuild 2023-06-23 00:41:25,803 [INFO] Running configSet Infra-EmbeddedPreBuild 2023-06-23 00:41:25,805 [INFO] Running config prebuild_0_backend 2023-06-23 00:41:28,430 [ERROR] postgresql93-devel is not available to be installed 2023-06-23 00:41:28,430 [ERROR] Error encountered during build of prebuild_0_backend: Yum does not have postgresql93-devel available for installation Traceback (most recent call last): File "/usr/lib/python3.9/site-packages/cfnbootstrap/construction.py", line 579, in run_config CloudFormationCarpenter(config, self._auth_config, self.strict_mode).build(worklog) File "/usr/lib/python3.9/site-packages/cfnbootstrap/construction.py", line 244, in build changes['packages'][manager] = CloudFormationCarpenter._packageTools[manager]().apply(packages, File "/usr/lib/python3.9/site-packages/cfnbootstrap/rpm_tools.py", line 76, in apply raise … -
My modelserializer is not working properly
I had this previoys serializer that was working fine. But i was asked to change it into a model serializer and now it is not passing all tests. This is the original version: from rest_framework import serializers from rest_framework.validators import UniqueValidator from .models import User class UserSerializer(serializers.Serializer): id = serializers.IntegerField(read_only=True) username = serializers.CharField( validators=[ UniqueValidator( queryset=User.objects.all(), message="A user with that username already exists.", ) ], ) email = serializers.EmailField( validators=[UniqueValidator(queryset=User.objects.all())], ) password = serializers.CharField(write_only=True) full_name = serializers.CharField(max_length=50, required=False) artistic_name = serializers.CharField(max_length=50) def create(self, validated_data: dict) -> User: return User.objects.create_user(**validated_data) def update(self, instance: User, validated_data: dict) -> User: for key, value in validated_data.items(): setattr(instance, key, value) if key == 'password': instance.set_password(value) instance.save() return instance And this is MY version: from rest_framework import serializers from rest_framework.validators import UniqueValidator from .models import User class UserSerializer(serializers.ModelSerializer): def create(self, validated_data): return User.objects.create_user(**validated_data) class Meta: model = User fields =[ "id", "username", "email", "full_name", "artistic_name" ] extra_kwargs = {"password": {"write_only": True}} For some reason, everytime i run tests for my version there're three errors which are: 1# AssertionError: False is not true : Check is password is being updated in PATCH route /api/users/8/ 2# AssertionError: Items in the first set but not the second: E 'password': … -
Python Setup On Windows
Please a Step by Step on Setting Up Python and Installing Django and Running Environment variable i installed python proper added Path and Installed Django but still the my Django is not running properly, it shows Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings -
kombu.exceptions.OperationalError: [Errno 111] Connection refused
I´m trying to use django with celery and rabbitmq on containers Docker. It works well without docker, but when I run on containers I recieve this erro : kombu.exceptions.OperationalError: [Errno 111] Connection refused. Here is my docker-compose: version: '3.4' services: rabbitmq: image: rabbitmq:3.12.0-management container_name: 'rabbitmq' ports: - 5672:5672 - 15672:15672 restart: always celery: build: context: . command: celery -A loan worker --loglevel=info depends_on: - rabbitmq restart: always loan: image: loan build: context: . dockerfile: ./Dockerfile ports: - 8000:8000 depends_on: - rabbitmq - celery I saw some people saying to put from .celery import app as celery_app __all__ = ('celery_app',) on init.py, but I already put on it. -
I'm testing my django project with a pytest. argument of type 'News' is not iterable
While testing content i got TypeError: argument of type 'News' is not iterable There is two asserts in my test. i'm testing detail view of news page for anonymous user 1 checks if the news is in the response context of the page 2 i check ordering by date of comments second part works correctly, but the first gives TypeError: list indices must be integers or slices, not News fixtures: @pytest.fixture def news(): news = News.objects.create( title='Новость', text='Невероятное событие', date=datetime.today(), ) return news @pytest.fixture def two_comments(author, news): now = timezone.now() list_of_comments = [] for index in range(2): comment = Comment.objects.create( news=news, author=author, text=f'Tекст {index}', ) comment.created = now + timedelta(days=index) comment.save() list_of_comments.append(comment) return list_of_comments test code: @pytest.mark.django_db def test_comments_order(two_comments, news, client): detail_url = reverse('news:detail', args=(news.id,)) response = client.get(detail_url) object_list = response.context['news'] assert news in object_list news = response.context['news'] print(news) list_comments = news.comment_set.all() assert list_comments[0]. created < list_comments[1].created code of error: @pytest.mark.django_db def test_comments_order(two_comments, news, client): detail_url = reverse('news:detail', args=(news.id,)) response = client.get(detail_url) > assert news in response.context self = [[{'True': True, 'False': False, 'None': None}, {'csrf_token': <SimpleLazyObject: <function csrf.<locals>._get_val at ...'object': <News: Новость>, 'news': <News: Новость>, 'view': <news.views.NewsDetail object at 0x000001CD39C53D30>}, {}]] key = <News: Новость> def __getitem__(self, key): if … -
Dynamic Foreign Key Links in Django Model
I have models in Django. Instances of one of the model can have 'parents' from the same table or any other table within the project or no parents at all. If I pass the parent as an argument to the model class, can I then 'process' the parent in the save() function and set the foreign key value in save()? To clarify, my issue is mainly that I don't know what 'model type' the parent will be, so I need the model am working on to determine this before setting the ForeignKey value. Pseudo code to explain myself: model_instance_being_created = ModelType( parent = parentinstance other_attributes ) model_instance_being_created.save() In models.py: class ModelType(models.Model): #bunch of attributes parent = models.ForeignKey('self', null=True, blank=True, on_delete=models.CASCADE) def save(self, *args, **kwargs): parent_type = self.parent.__class__ self.parent = parent.objects.get(id=self.parent.id) super().save(*args, **kwargs) -
how to read django test database from pandas.read_sql
I have functions that create some pandas dataframes from database I want to test the data in these dataframe but when I try to read raw sql from test database using pandas.read_sql() or mysql cursor excute it always returns empty DataFrame class PlacementTestCase(TestCase): @classmethod def setUpTestData(cls): p = Placement.objects.create(name='placement_test') def PlacementTestCase(self): db = sql.connect( host=settings.DATABASES["test_db"]["HOST"], database=settings.DATABASES["test_db"]["NAME"], user=settings.DATABASES["test_db"]["USER"], password=settings.DATABASES["test_db"]["PASSWORD"] ) df = pd.read_sql("SELECT * FROM myapp_placement", con=db) # I want to run tests on df but it returns Empty DataFrame... -
How do I display a queryset from Django ListView in relation to a model field in an html template?
models.py JOB_STATUS_CHOICES = ( ("draft", "draft"), ("submitted", "submitted"), ("published", "published"), ) class JobPost(models.Model): job_title = models.CharField(max_length=200) job_description = models.TextField() job_status = models.CharField(max_length=20, choices=JOB_STATUS_CHOICES, default='draft') views.py class UserDashboardView(ListView): model = JobPost context_object_name = 'jobs' template_name = 'users/user_dashboard.html' def get_queryset(self): queryset = super().get_queryset() queryset = queryset.filter(user=self.request.user) return queryset urls.py urlpatterns = [ path('user_dashboard/', UserDashboardView.as_view(), name='user_dashboard'), ] user_dashboard.html <div class="dashboard"> <h1>Dashboard</h1> <div class="card"> {% for job in jobs %} <h2>{{job.job_status}}</h2> <div class=""> {{job.job_title}} </div> {% empty %} <div class=""> <h4>No items in the list</h4> </div> {% endfor %} </div> </div> How can I order the queryset in an html page users/user_dashboard.html in relation to the fields job_status and job_title in the below format: Draft Job post title Submitted Job post title Published No item in the list (when there is no item to display) My result enter image description here -
Django Rest view optimize queryset having multi level depth
I have a view that requires data from many related objects at 3rd, 4th level relations. Here goes the view: class OrderViewSet(ModelViewSet): queryset = Order.objects.select_related().prefetch_related().order_by('-id') Now in the serializer I have to fetch the stock for a related item of this order and then perform certain calculations on it. class OrderListSerializer(ModelSerializer): shortages = SerializerMethodField() def get_shortages(self, obj): item = obj.order_line.product.item stock = ( order_line.product.item.item_instances.filter(stock_quantity__gt=0) .aggregate(stock=Coalesce(Sum("stock_quantity"), 0, output_field=FloatField())) .get("stock", 0) ) required_stock = self.calculate_required_stock() if stock < required_stock: ..... In the above piece of code the line order_line.product.item.item_instances represents 4 models and if I populate the queryset's prefetch_related with ('order_line__product__item') and ('order_line__product__item__item_instances') still the queries sent to the DB are in thousand. How can I improve this situation. -
I want to send the data from Next.js form to Django server
views.py User = get_user_model() @csrf_exempt def register(request): if request.method == 'POST': username = request.POST.get('username') password = request.POST.get('password') email = request.POST.get('email') print(request.POST) print(request.POST.get('username')) print(request.POST.get('password')) print(request.POST.get('email')) # Perform validation if not username or not password or not email: return JsonResponse({'message': 'Username, password, and email are required'}) # Create a new user try: User.objects.create_user(username=username, password=password, email=email) return JsonResponse({'message': 'Registration successful'}) except: return JsonResponse({'message': 'Registration failed'}) return JsonResponse({'message': 'Invalid request method'}) register.tsx const handleRegister = async (e: any) => { e.preventDefault(); try { const response = await axios.post( "http://localhost:8000/api/register", JSON.stringify({ username, password, email, }), { headers: { "Content-Type": "application/json", bodyParser: false, }, } ); // Handle the successful registration response console.log(response.data); setError("Registration successful"); } catch (error: any) { // Handle registration error setError(error.response); } }; On network tab, I got the data that I entered in Payload but the server didn't receive it as I tried to print the data. Try somnething on google as django-cors-headers but it's still not working. I was trying to get the data from the form then send it to django server. -
How to display products from category in dajngo?
I want to filter product by category I have made two categories one main category and category. Product is filtering according to main category but not according to category please someone tell me. def categories(request): main_categories = MainCategory.objects.all() context = { 'main_categories': main_categories } return render(request, 'allProducts.html', context) def showProductsMainCategoryWise(request): main_category = request.GET.get('main_category') if main_category == None: products = Product.objects.order_by('id') else: products = Product.objects.filter(main_category__name=main_category) main_categories = MainCategory.objects.all() context = { 'products': products, 'main_categories': main_categories } return render(request, 'allProducts.html', context) def showProductsCategoryWise(request): main_category = request.GET.get('main_category') category = request.GET.get('category') if main_category == None and category == None: products = Product.objects.order_by('id') else: products = Product.objects.all().filter( Q(main_category__name=main_category), Q(category__name=category)) main_categories = MainCategory.objects.all() categories = Category.objects.all() context = { 'categories': categories, 'products': products, 'main_categories': main_categories } > return render(request, 'allProducts.html', context) <a href="{% url 'showProductsCategoryWise' %}?main_category={{cat.main_category.name}} and category={{cat.name}}"> <li>{{cat.name}}</li> </a> -
How to aggregate and sum values of a nested JSONField with Django?
I have a Django model with a JSONField called data that store a nested dictionary with timestamps, and I would like to aggregate all objects with the same datetime and sum the values of keys ba and bl if they belong to the same unix timestamp. models.py class Trade(TimestampedModel): data = models.JSONField(default=dict) datetime = models.DateTimeField(null=True) What is the best method to do that ? { "1648512000": { # key is a Unix timestamp "ba": 883091.0, "bl": 860, "bp": 3337.98, "sa": 729903.0, "sl": 725, "sp": 3338.82 }, "1648512300": { "ba": 652184.0, "bl": 634, "bp": 3339.4, "sa": 458526.0, "sl": 464, "sp": 3340.77 },... -
Override "all_fields" attribute within Django Rest Framework serializer through the ViewSet
I am trying to build an API endpoint in DRF with a class-based approach. Below are the code snippets from the Viewset and Serializer files. organization_viewset.py: class organizationViewSet(viewsets.ViewSet): def list(self, request): organizations = get_all_organizations(op_format="OBJECT") org_serializer = OrganizationSerializer(organizations, many=True) return Response(org_serializer.data) organization_serializer.py: class OrganizationSerializer(serializers.ModelSerializer): class Meta: model = Organization fields = '__all__' What I want to do next is, at the time of creating an instance of OrganizationSerializer, I want to pass a parameter as below: org_serializer = OrganizationSerializer(organizations, many=True, all_fields=False) if the all_fields value is True then I want the serializer to return all fields. Else the serializer will return the below fields. fields = ('id','orgName', 'orgAltName', 'orgPlant', 'orgAddress1') To solve this, I tried writing a second serializer class with the selected fields. However, I would prefer using a single serializer class to switch between selected and all fields. -
SSL handshake failed with host when using Cloudfare with Railway.app
When I connected a Cloudfare proxy with my Django app hosted on Railway.app, I get the error:. I bought my domain from Godaddy. Please note that it was working fine a while ago and this problem started when I made a small bug fix in my models.py (should be unrelated) and redeployed my site. I believe this is because I have not installed the Origin Certificate on Railway.app. On Cloudfare, my SSL/TLS encryption mode is Full. If so, how do I install this certificate on Railway.app which is my origin server. Any help would be appreciated. -
How to delete session data from django_session table when user closes browser
I am using Django Session Authentication. I set SESSION_EXPIRE_AT_BROWSER_CLOSE = True as i want users to login each time they close the browser. All works fine user closes browser session got deleted from browser but session data is still there in the django_session table having expire_date with 14 days validity. In this case how will i run cron job to delete useless session data ? I wanted to run cron which will identify expired session then delete them but even if session data is useless i cant delete because expire date have 14 days validity. -
pywin32 installation error in python django
when I try to install "pywin32" in virtualenvironment through vscode terminal in linux(aws server), it shows error. But successfully installed in local machine. python version : 3.8.8 django version : 3.2 os : linux try to install pypiwin32 -
Bootsrap modal Making alignment issue
I am using Django and bootstrap-4 and having an issue with the bootstrap modal's position. I have placed the modal outside a section and trying to get it on click of a HREF <section> <p class="small">By signing up you are agreeing to our <a data-target="#myModal" data-toggle="modal" style="font-weight: 600;" class="ml-1">Terms & Condtions</a>.</p> </section> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> <div class="modal-body"> By using our e-commerce website, you agree to comply with all applicable laws and regulations. </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Save changes</button> </div> </div> </div> </div>``` this is the whole code of the file, I want the `modal` to position correctly based on the screen. so that the `modal` can be seen without any issues at any `scroll position`. [![this shows how the final result looks like](https://i.stack.imgur.com/K3SYx.png)](https://i.stack.imgur.com/K3SYx.png) -
Django how to anotate number of attempts in Queryset
I have the model like: class Order(models.Model): order_id = models.CharField(max_length=100) title = models.TextField(max_length=600) title_2 = models.TextField(max_length=100) attempts = models.ForeignKey('self', blank=True, null=True, on_delete=models.PROTECT) driver = models.ForeignKey(Driver, blank=True, null=True, on_delete=models.SET_NULL) updated_at = models.DateTimeField(blank=True, default=datetime.datetime.now()) And what I want to achive in my anotation I need a number of attempt based on older updated_at value. But active order have attempts=None. How I can correctly anotate the number of attempt ? -
How to customize `/static/` location for Jupyter Lab on NGINX
I have a server (Ubuntu 20) running NGINX for reverse proxy. I have a Django app accessible thru the the https://URL/app location, and it's static content is aliased at /var/www/static-collected. This works fine. I'm attempting to setup Jupyter Lab at https://URL/jupyter, but it's unable to find the /static content, ie the CSS and images (Jupyter logo). I've setup Jupyter Lab with NGINX plenty of times, but never with another app that is using /static. I've tried copying the Jupyter static files (located @ .../dist-packages/jupyter_server/static) to the static location used by the Django app, thinking Jupyter would look there given the NGINX setup. I've also tried editing the c.LabServerApp.static_dir property in the config to point directly at the Jupyter static files but no luck so far. I'm not sure if this should be solved thru NGINX config, Jupyter config, or both. Any ideas would be appreciated! Here's my NGINX config: server { root /var/www/html; server_name URL; # managed by Certbot location /jupyter/ { proxy_pass http://localhost:8888/; 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-Forwarded-For $proxy_add_x_forwarded_for; proxy_cache_bypass $http_upgrade; } location /app/ { proxy_pass http://localhost:8000/; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection upgrade; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } … -
How to count pages from .doc wuthpython on django
I'm using django. In the view I have created a function that passes a file to it and the function returns the number of pages and the type of file. Works correctly. Now I want to add that it can also count the pages of ".doc" but I have tried it in several ways and I can't. I have tried to do it with "win32com" but when installing it gives me an error. If it's not possible, how should I do it? Maybe first convert it to ".docx" and then count the pages? This is my function: def count_pages(file_content, filename): file_extension = os.path.splitext(filename)[1] if file_extension.lower() == ".pdf": pdf_reader = PyPDF2.PdfReader(file_content) num_pages = len(pdf_reader.pages) file_type = "PDF" elif file_extension.lower() == ".docx": text = docx2txt.process(BytesIO(file_content)) num_pages = text.count("\f") + 1 file_type = "Word" elif file_extension.lower() == ".pptx": prs = Presentation(BytesIO(file_content)) num_pages = len(prs.slides) file_type = "PowerPoint" elif file_extension.lower() == ".xlsx": wb = load_workbook(BytesIO(file_content)) num_pages = len(wb.sheetnames) file_type = "Excel" else: num_pages = 0 file_type = "Unknown" return num_pages, file_typ