Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
How to join tables with ManyToManyField type in Django?
I have models like this: class Education(models.Model): title = models.CharField(default=None, max_length=100) content = models.TextField(default=None) price = models.ManyToManyField(Price) class Price(models.Model): cost = models.CharField(default=None, max_length=20) created_at = models.DateTimeField(auto_now=True, null=True, blank=True) And i want to inner join between two tables and access to all fields of both. -
unpacking data in django rest framework
I am confused about unpacking the data or validated data in django serializers. For eg I am reading the code snippets from the drf documentation and it is following. class UserSerializer(serializers.ModelSerializer): profile = ProfileSerializer() class Meta: model = User fields = ['username', 'email', 'profile'] def create(self, validated_data): profile_data = validated_data.pop('profile') user = User.objects.create(**validated_data) Profile.objects.create(user=user, **profile_data) return user Here what I know is if we put ** in front of the **kwargs, it will unpack the kwargs dictionary and we got the values only. But in the above line Profile.objects.create(user=user, **profile_data) profile_data will be unpacked, so that means we got the value for the fields. But what I think is, it is supposed to be like address=address,mobile=mobile etc something like that while using create function. Will **profile_data will result to that what I have mentioned?? Also, when I do print(**profile_data), it will give me an error. How does it unfold then?? -
Django auth ldap open new connection for each view
I'm using Django LDAP authentication, with django-auth-ldap library. Classic authentication with user/pwd is also supported: AUTHENTICATION_BACKENDS = ['django_auth_ldap.backend.LDAPBackend', 'django.contrib.auth.backends.ModelBackend', ] It works fine, I can log in and log out. But we noticed that when a user is logged in and change view, a new connection to LDAP server is made (checked with netstat command). Is there any setting that I could play with to either close connection or use same connection without opening a new one? I don't even understand why it needs to connect several times, since the ldap user is registered in the local database as 'classic' user, with permissions and groups. Any help on how this works would be appreciated, thanks. Django 3.2.8, Django-auth-ldap 2.1.1, Python-ldap 3.2.0, Python 3.7 -
How to implement and run the cloudguru profiler with the django-app in the server?
I have my system running in the EC2 instance. I have added the settings for cloudguru profiler. Now, i need to run in the server. How should i run it ?? -
Django queryset searching for firstname and lastname with startswith
I have one django app, In which I search for a name from first_name and last_name. Which is working for me using Q tag. from django.db.models import Q def find_user_by_name(query_name): qs = User.objects.all() for term in query_name.split(): qs = qs.filter( Q(first_name__icontains = term) | Q(last_name__icontains = term)) return qs But I'm facing one issue regarding ordering the data not coming as I want. For example: search_word = 'Kia' The result I got after the search is: { "first_name": "Katja", "last_name": "Tukiainen", }, { "first_name": "Kia", "last_name": "Reijonen", }, { "first_name": "Sanna", "last_name": "Kiander", } ] But I want a result in which first starts with Kia first, Result like: { "first_name": "Kia", "last_name": "Reijonen", }, { "first_name": "Katja", "last_name": "Tukiainen", }, { "first_name": "Sanna", "last_name": "Kiander", } ] Please help me regarding this, I try this with startswith but not working for me. Thanks in advance. -
How can I save my excel file in the file I created with BASE_DIR?
here is my code How can I save my pandas_simple file here in the file I created with BASE_DIR? (This should be with Python) -
Why it doesn't show any data when I used ```filter()``` at views?
In addition of the answer that I received yesterday: How to make many to one field reference in Django 3.2? It showed to me another problem. Using filter() method it doesn't show to me any data at the view. Example: When I add a task it doesn't show it on the view. To get the data on the view I used all_items = todo.objects.all().order_by("created"), but when I put the filter method It didn't show anything: all_items = todo.objects.filter(user=user).order_by("created") Not sure why is this error. Would appreciate any response! Thanks -
HTML text disappers when I move the cursor on it
Problem: Yesterday everything worked fine. Today I've opened my project and I've seen the next picture: It's when I didn't have height: 100% in .random-category-img class. Yesterday, when I set height: 100% I would cover a black background by picture. However, now I see such a picture: When my cursor is anywhere, text disappear(it moves to the place where the red arrow is, but if I move cursor on pictures, this text will move from red arrow to black arrow. I don't know what happened and don't know how to fix it. HTML template: <div class="random-categories-container"> <div class="random-categories-loop"> {% for category in random4Categories %} <a class="random-category" href="{% url 'bookByCategorySlug' category.slug %}"> <img class="random-category-img" src="{{category.image.url}}" alt=""> <div class="random-category-title">{{ category }}</div> </a> {% endfor %} </div> <a class="allEditionsButton" href="{% url 'listOfCategories' %}">Усі категорії</a> </div> Css file .random-category, .random-categories-container, .random-categories-loop { display: grid; z-index: 1; } .random-categories-container { padding: 20px 0 0; grid-template-rows: 2fr 1fr; } .random-categories-loop { grid-template-columns: repeat(4, 1fr); column-gap: 40px; z-index: 1; } .random-category { font-size: 24px; align-items: center; background: black; border-radius: 20px; transition: 0.3s ease-in-out; } .random-category-img { width: 100%; /* height: 100%; */ opacity: 0.6; border-radius: 20px; } .random-category:hover { transform: scale(1.05); } .random-category-title { display: flex; justify-self: … -
Cant upload Image with CreatView Django
views.py class PostCreatView(LoginRequiredMixin, CreateView): model = Posts fields = ['image', 'caption'] template_name = 'home/creat-post.html' success_url = '/home/' def form_valid(self,form): form.instance.user = self.request.user return super().form_valid(form) models.py class Posts(models.Model): caption = models.CharField(max_length=2200) date_posted = models.DateTimeField(default=timezone.now()) image = models.ImageField( upload_to='PostsImages') user = ForeignKey(User, on_delete=models.CASCADE ,related_name='UserPosts') def __str__(self): return f"Post {self.id} ({self.user.username})'s" def save(self, *args, **kwargs): super().save(*args, **kwargs) img = Image.open(self.image.path) #img = make_square(img, 1080, 1080) img.save(self.image.path) class Meta: ordering = ['-date_posted'] creat-post.html {% extends "home/base.html" %} {% load crispy_forms_tags %} {% block content %} <div> <form method="POST"> <fieldset> <legend >Creat Post</legend> {% csrf_token %} {{ form|crispy }} </fieldset> <button class="btn btn-primary" type="submit">Post</button> </form> </div> {% endblock %} So I created a creatview to add create new post. However when I go to the link and add photo then hit submit, it gives me as I haven't uploaded an image. However, I am able to add images to a new Post from the admin normally. -
How to extend 'users/me/' fields in djoser
Is it possible to extend the user field in djoser? I'm using djoser jwt token authentication in Django restframework. And as it it is, the functionality of djoser comes from djoser.url and djoser.urls.jwt. Only username, email and password are the parameters I want users to provide during registration. After registration, they can provide other details. When using JWT plainly without djoser, I would create a model for profile and provide the fields that register individuals should complete after registration. But with djoser, user's details can be obtained from /users/me/ endpoints. This is the first time I'm using djoser package, and it seems to me that /users/me/ functions as the endpoint for user's profile already as it's explained in their documentation https://djoser.readthedocs.io/en/latest/base_endpoints.html#user-resend-activation-e-mail I still want to collect information from users such as their data of birth, country, marital status and others. But I want to provide them with profile page which has fields where thay can provide those details. My question now is, is it possible to use djoser users/me/ endpoint to accomplish that? If yes, how? It's not very clear how to accomplish that in the documentation. -
Django dynamically created temporary image not displaying
I want to dynamically create an image based on user inputs and display this to the user. views.py: class TreatmentTemplateView(TemplateView): template_name = "../templates/patient/treatment_detail.html" def get_context_data(self, *args, **kwargs): context = super().get_context_data(*args, **kwargs) context["patient_id"] = self.kwargs["patient_id"] result = find_treatment(context["patient_id"]) context = result[0] context["patient"] = result[1] context['image'] = result[2] print(context['image']) return context It is at find_treatment(context["patient_id”]) that the image is generated by subfunction of find_treatment fig = sns_plot.get_figure() img_in_memory = BytesIO() fig.savefig(img_in_memory, format="png") #dunno if your library can do that. image = base64.b64encode(img_in_memory.getvalue()) And the template contains: <img src="data:image/png;base64,{{image}}" /> The image is not displaying however, is this an issue with how i am referencing the image in the template? -
decompress gzipped data from multipart/form-data with django
I'm having quite a lot of trouble trying to decompress gzipped data in django. I've tried a number of the solutions proposed in Download and decompress gzipped file in memory? but i think i'm running into difficulty in how it interacts with Django I'd like to be able to upload data.csv.gz and then if it is a gzip, extract out the compressed data into a django File to continue along its routine (Saving to FileField) What I have so far in my serializer def create(self, validated_data): file: File = validated_data.get("file") ext = file.name.split(".")[-1].lower() if ext == "gz": compressedFile = io.BytesIO() compressedFile.write(file.read()) decompressed_fname = file.name[:-3] decompressedFile = gzip.GzipFile(fileobj=compressedFile) with open(decompressed_fname, "wb") as outfile: outfile.write(decompressedFile.read()) outfile.flush() with open(decompressed_fname, "rb") as outfile: file = File(outfile) ext = decompressed_fname.split(".")[-1].lower() ... When I do this, outfile is empty when I check its contents on disk, and throws an error in later routines f.seek(0) ValueError: seek of closed file I get a similar error if I use shutil instead too if ext == "gz": compressedFile = io.BytesIO() compressedFile.write(file.read()) decompressed_fname = file.name[:-3] import shutil shutil.copyfileobj(gzip.GzipFile(fileobj=file), open(decompressed_fname, "wb")) with open(decompressed_fname, "rb") as outfile: file = File(outfile) ext = decompressed_fname.split(".")[-1].lower() the curl command i'm using: curl http://0.0.0.0:8000/upload/ -X 'POST' … -
How can I set the DEBUG level in a Django application
I am new to logging and am having difficulty setting the logging level to debug. I have created my own logger # logger.py from pathlib import Path import logging # Create a custom logger logger = logging.getLogger(__name__) logger.propagate = False logging.basicConfig(level=logging.DEBUG) # Create handlers c_handler = logging.StreamHandler() f_handler = logging.FileHandler('my_log_file.log') c_handler.setLevel(logging.DEBUG) f_handler.setLevel(logging.DEBUG) # Create formatters and add them to handlers c_format = logging.Formatter('myapp: %(message)s') f_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') c_handler.setFormatter(c_format) f_handler.setFormatter(f_format) # Add handlers to the logger logger.addHandler(c_handler) logger.addHandler(f_handler) def log(frame, obj): """Create log message and return the object.""" path = Path(frame[0][1]) module = path.stem line = frame[0][2] message_text = f'{obj} <{module}> {line}' logger.warning(message_text) return obj I can create log messages in my application with log(inspect.stack(), f'Something happened here {myobj.name}: {myobj.value}') but it will not create messages unless I use the warning level logger.warning(message_text) in the logger. If I change it to logger.debug(message_text) nothing happens I searched the application for WARNING, DEBUG and level but can find nothing that should affect my logger I assume there is another logger that is over-ridding my level (Django perhaps) Can anyone help? -
i want to populate data in my django modelform but i m not sure about that how?
class update_question(View): def post(self, request, pk): question = Question.objects.get(id=pk) form = QuestionForm(request.POST, instance = question) if form.is_valid(): form.save() context = {'form':form} return render(request, 'file/updateQ.html', context) def get(self, request, *args, **kwarg, ): print(kwarg) form = QuestionForm() context = {'form':form} return render(request, 'file/updateQ.html', context) How to get object in get method to populate data in my QuestionForm -
Dynamic key fields
If a new input tag generated by user, then a dynamic field should be created in database. Example: When Booking railway ticket if user gives number of passenger’s then automatically field should be created like their name , age ,address should be got as input and stored in data base. The question is how to create key dynamically during runtime according to the user input. -
How to add middle IDs in third table with ManyToManyField in Django when using Serialaizer?
I have tables like this: class Education(models.Model): title = models.CharField(default=None, max_length=100) content = models.TextField(default=None) price = models.ManyToManyField(Price) class Price(models.Model): cost = models.CharField(default=None, max_length=20) And from client get a request and try to save it in three tables, I have no problem with two tables Education and Price but because I'm using serializer i don't know how to update third table(middle). def post(self, request): cost = request.POST.get('cost') price = Price.objects.create(cost=cost) education_serializer = EducationSerializer(data=request.data) if education_serializer.is_valid(): education_serializer.save() Good, here i have updated two tables, In the following i don't know how update third table. If i didn't use serializer i have could write it like this: education.price.add(price) -
How to generate S3 presigned URL with boto3 resource instead of client
I using Django and S3Boto3Storage S3Boto3Storage creates connection attribute using boto3's resource method. connection = getattr(self._connections, 'connection', None) if connection is None: session = self._create_session() self._connections.connection = session.resource( 's3', region_name=self.region_name, use_ssl=self.use_ssl, endpoint_url=self.endpoint_url, config=self.config, verify=self.verify, ) return self._connections.connection But only Client class has the generate_presigned_url method. So, how can I get presigned URL with S3Boto3Storage.connection (by the way connection is ServiceResource)? PS I know that I can get presigned URLs by using Model.FileField.url attribute, but I need operate with temporary files that no exist in DB. In the internet many examples how to get presigned URL with client but nothing with s3 = boto3.resource('s3'). -
How to filter reverse foreign key with more one conditions by prefetch_related in Django
I created tow models class GroupModel(models.Model): group_name = models.CharField(max_length=2, null=False, default="A") class MemberModel(models.Model): name = models.CharField(max_length=8, null=False, default="") group = models.ForeignKey( to=GroupModel, on_delete=models.CASCADE, related_name="members", db_column="member", ) isActive = models.BooleanField(null=False, default=False) country = models.CharField(max_length=8, null=False, default="CN") I run queryset = GroupModel.objects.prefetch_related( Prefetch("members", MemberModel.objects.filter(isActive=False)) ).distinct() I got the all groups with the field isAcitve is Flase in MemberModel. Now, I want to get all groups with the field isActive is False and country is CN in MemberModel. I run queryset = GroupModel.objects.prefetch_related( Prefetch("members", MemberModel.objects.filter(isActive=False, country="CN")) ).distinct() But I get the result is [{'group_name': 'GROUP_A', 'id': 1, 'members': []}, {'group_name': 'GROUP_B', 'id': 2, 'members': []}, {'group_name': 'GROUP_C', 'id': 3, 'members': [{'country': 'CN', 'isActive': False, 'name': 'ENWK'}, {'country': 'CN', 'isActive': False, 'name': 'LKMP'}]}] I want to get [{'group_name': 'GROUP_C', 'id': 3, 'members': [{'country': 'CN', 'isActive': False, 'name': 'ENWK'}, {'country': 'CN', 'isActive': False, 'name': 'LKMP'}]}] What can I do? -
How do write this function view in Auth view LoginView?
I have written function based view i need this to be written in class based view LoginView views.py def manager_login(request): current = User.objects.filter(is_manager = True) if request.method == 'POST': pm_form = AuthenticationForm(data=request.POST) if pm_form.is_valid(): username = pm_form.cleaned_data.get('username') password = pm_form.cleaned_data.get('password') user = authenticate(username=username,password=password) if user is not None: if user in current: login(request,user) return redirect(reverse('pm_dashboard')) else: messages.error(request,"Invalid Username or Password") else: messages.error(request,"Invalid Username or Password") return render(request, 'app/pm_login.html',context={'form':AuthenticationForm(),}) -
How to disable upstream buffering Nginx + Docker + Gunicorn?
I'm trying to use Nginx as proxy server between client and Gunicorn. Nginx, Gunicorn (Django) are Docker's containers. Problem is I can't disable buffering of upstream when I send large file from Client to Django App. TTFB time is quite small, thus my progress bar (which use xhr.upload.progress event) becomes 100% very fast (less than second). Then I have to wait 30 sec while file will be upload to server. Here are my settings. Please, help. I've tried many configurations trying to set buffer size to zero etc., check many answer on StackOverflow, but nothing helps. docker-compose.yaml ... services: db: image: postgres:12.4 volumes: - postgres_data:/var/lib/postgresql/data/ restart: always ports: - ${DB_PORT}:${DB_PORT} env_file: - ./.env backend: build: ./src/backend volumes: - RUDZASV0021:/code/storage/RUDZASV0021 - logs:/code/logs restart: always depends_on: - db env_file: - ./.env nginx: build: context: . dockerfile: ./src/frontend/Dockerfile volumes: - ./docker-settings/default.conf:/etc/nginx/conf.d/default.conf:ro restart: always ports: - 80:80 - 443:443 depends_on: - backend Backend Dockerfile FROM python:3.8.7-slim WORKDIR /code COPY . . RUN pip install -r /code/requirements.txt RUN apt-get update && apt-get install -y mc CMD gunicorn entrypoints.wsgi:application --workers=4 --worker-class=gevent --timeout=90 --graceful-timeout=10 --bind 0.0.0.0:8000 Nginx Dockerfile FROM nginx:1.20.0 WORKDIR /frontend COPY ./src/frontend/dist . WORKDIR /cert COPY ./cert/device.key . COPY ./cert/device.crt . Nginx default.conf upstream hello_django … -
Django CSRF Token error or missing with Ajax POST request
I am trying to integrate ajax into a web application with the Django framework. I am however having a hard time trying to make a simple ajax call to work. I want to make a DB connection using a form (where users input the DB credentials), call the API, then return the output (whether successful or not). Here's my views.py that is used to handle the API: # -- START from HERE ! class TestConnectionAPI(views.APIView): ''' Test DB Connection from TARGET DB ''' def post(self, request): dbs = (request.data['host'], request.data['port'], request.data['dbname'], request.data['user'], request.data['password'], request.data['schema_name']) try: x = dc.DbConnection(*dbs) x.create_conn() data = x.check_conn() result = { 'message' : 'Success', 'server' : f'Connection established from {data}', 'results':{ 'host':dbs[0], 'port':dbs[1], 'dbname':dbs[2] }, } return Response(result, status=status.HTTP_200_OK) except Exception as e: return Response({'Message':str(e)}, status=status.HTTP_400_BAD_REQUEST) This is my connection.html to display the form (complete code: here) : ... <form method="post"> <div class="modal-body"> <div class="form-group"> <label for="hostname">Hostname</label> <input type="text" class="form-control" id="hostname" name="hostname" aria-describedby="hostname"> </div> <div class="form-group"> <label for="port">Port</label> <input type="number" class="form-control" id="port" name="port" placeholder="e.g., 5432"> </div> <div class="form-group"> <label for="database">Database name</label> <input type="text" class="form-control" id="dbname" name="dbname" placeholder="Enter database name"> </div> <div class="form-group"> <label for="username">Username</label> <input type="text" class="form-control" id="username" name="username" placeholder="Enter username"> </div> <div class="form-group"> <label for="password">Password</label> … -
I want to use email field as a main field for users, vendors. But for django-admin I want to use username field usual. Need suggest
models.py from django.contrib.auth.models import AbstractUser from django.db import models from django.db.models.fields import EmailField class CustomUser(AbstractUser): email = models.EmailField(('email address'), unique=True) mobileno = models.IntegerField(blank=True, null=True) is_customer = models.BooleanField(default=False) is_vendor = models.BooleanField(default=False) USERNAME_FIELD = 'email' REQUIRED_FIELDS = [] class VendorDetails(models.Model): vendoruser = models.OneToOneField(CustomUser, on_delete=models.CASCADE) aadhar_number = models.CharField(max_length=200) pan_number = models.CharField(max_length=200) store_name = models.CharField(max_length=200) brand_name = models.CharField(max_length=200) admin.py from django.contrib import admin from customer.models import CustomUser, VendorDetails # Register your models here. admin.site.register(CustomUser) admin.site.register(VendorDetails) when attempting ----- python manage.py createsuperuser----(File "D:\Yabesh\Django\env\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 189, in handle self.UserModel._default_manager.db_manager(database).create_superuser(**user_data) TypeError: create_superuser() missing 1 required positional argument: 'username') need help. can I write usermanager for this? Thanks in advance. -
Django ORM fill 0 for missing date
I'm using Django 2.2. I want to generate the analytics of the number of records by each day between the stand and end date. The query used is start_date = '2021-9-1' end_date = '2021-9-30' query = Tracking.objects.filter( scan_time__date__gte=start_date, scan_time__date__lte=end_date ) query.annotate( scanned_date=TruncDate('scan_time') ).order_by( 'scanned_date' ).values('scanned_date').annotate( **{'total': Count('created')} ) Which produces output as [{'scanned_date': datetime.date(2021, 9, 24), 'total': 5}, {'scanned_date': datetime.date(2021, 9, 26), 'total': 3}] I want to fill the missing dates with 0, so that the output should be 2021-9-1: 0 2021-9-2: 0 ... 2021-9-24: 5 2021-9-25: 0 2021-9-26: 3 ... 2021-9-30: 0 How I can achieve this using either ORM or python (ie., pandas, etc.)? -
How can i list all the functions in the class models using terminal?
from django.db import models How can i list all the functions in the class models using terminal? -
Vimeo video to domain-level privacy not working in Django
I have an app built on django framework where I want to embed my own vimeo videos and I want the video to play in my website only. For that I restricted the video to domain-level privacy. But still it's showing error in my website as Sorry Because of its privacy settings, this video cannot be played here.