Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
django-haystack elasticsearch spanish special chars (accents)
im using django-haystack 3.3.0 over django 4.x and use elasticsearch 7.17.24 as backend. I'm dealing with texts in spanish, so for example I'm index the text: El corazón del mar and when I'm try to search: corazon i got 0 results, but when I'm search corazón or Corazón I got n>0 results any idea? thanks in advance -
getting phone number of user in a django app from google with social_core
Hello I am working on a Django app that have Customuser model and i just save users phone number in the model for some reasons this is my model and manager : class CustomUserManager(BaseUserManager): def create_user(self, phone_number, password=None, **extra_fields): if not phone_number: raise ValueError('The Phone Number must be set') user = self.model(phone_number=phone_number, **extra_fields) user.set_password(password) user.save(using=self._db) return user def create_superuser(self, phone_number, password=None, **extra_fields): extra_fields.setdefault('is_staff', True) extra_fields.setdefault('is_superuser', True) return self.create_user(phone_number, password, **extra_fields) class CustomUser(AbstractBaseUser, PermissionsMixin): phone_number = models.CharField(max_length=15, unique=True) email = models.EmailField(unique=True, null=True, blank=True) is_active = models.BooleanField(default=True) is_staff = models.BooleanField(default=False) is_superuser = models.BooleanField(default=False) score = models.PositiveIntegerField(default=10, null=True, blank=True) objects = CustomUserManager() USERNAME_FIELD = 'phone_number' REQUIRED_FIELDS = [] def save(self, *args, **kwargs): super().save(*args, **kwargs) Profile.objects.get_or_create(user=self) def add_score(self, score): self.score += score print(self.score) self.save() def decrease_score(self, score): self.score -= score self.save() def generate_referral_code(self): code_length = 19 characters = string.ascii_letters + string.digits unique_code = ''.join(random.choice(characters) for _ in range(code_length)) while ReferralCode.objects.filter(code=unique_code).exists(): unique_code = ''.join(random.choice(characters) for _ in range(code_length)) ReferralCode.objects.create(user=self, code=unique_code) def get_referral_code(self): try: return self.referralcode.code except ReferralCode.DoesNotExist: return None def __str__(self): return self.phone_number i edit my setting.py file for having a pipleline file like this : SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = config("GOOGLE_CLIENT_ID") SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = config("GOOGLE_SECRET") SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPE = [ 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/user.phonenumbers.read', ] SOCIAL_AUTH_PIPELINE = ( 'social_core.pipeline.social_auth.social_details', 'social_core.pipeline.social_auth.social_uid', 'social_core.pipeline.social_auth.auth_allowed', … -
AJAX Posts Reverse Order After Refresh the Page When Multiple Posts are Created in the Same Minute from Same User
I'm working on a social network project using Django for the backend and JavaScript for the frontend. Users can post new content, and the global feed is updated both dynamically via Ajax and by refreshing the page. Everything works fine, except when a user creates multiple posts within the same minute. Initially, the newest post is displayed at the top, but when I refresh the page, the order of posts gets reversed for that specific user, where older posts appear above newer ones. I'm using the created_at field (with DateTimeField) to order the posts. Here's a snippet of the key code that handles fetching the global stream and updating the DOM: function updateStream(posts) { const postContainer = document.querySelector('#post-list'); posts.forEach(post => { let postElement = document.getElementById(`id_post_div_${post.post_id}`); if (!postElement) { postElement = document.createElement('div'); postElement.id = `id_post_div_${post.post_id}`; postElement.className = 'post'; postElement.innerHTML = ` <p> <strong><a href="/profile/${post.author_username}" id="id_post_profile_${post.post_id}" class="post-profile"> ${post.author}</a></strong>: <span id="id_post_text_${post.post_id}" class="post-txt">${sanitize(post.content)}</span> <span id="id_post_date_time_${post.post_id}" class="post-date-time">${formatDate(post.created_at)}</span> </p> <div class="comments" id="comments_${post.post_id}"></div> <div id="id_comment_input_div_${post.post_id}" class="add-comment"> <input type="text" id="id_comment_input_text_${post.post_id}" placeholder="Add a comment" /> <button id="id_comment_button_${post.post_id}" onclick="addComment(${post.post_id})">Submit</button> </div> `; // Add new post to the correct position postContainer.appendChild(postElement); } }); } What I’ve Tried: Adding a fallback to sort by post_id when posts have the same created_at, but … -
How to block migrations that delete a view in django?
When we change the type of a column in Django and run a migration, it does not finish if any view is using the column. However, when we remove a column from the table and run a migration, Django does not interrupt the migration. How can I configure this? Can we create a custom migration or even a trigger in the database? -
Redirecting a user to a specific address after logging in to Django
The problem is as follows: after the user has entered his data into the form on the page /accounts/login, upon successful login, he should be directed to the page corresponding to his username ``/username`'. At the same time, this redirection is carried out to the admin panel and each admin will have their own models displayed. I don't understand how to steal the name of the current user, and then allow only this user to enter the admin panel corresponding to his name. For example, the user entered the name ntz and some password, after that he should get to the /ntz/ - admin page of this user. There is no way other users can get there. One user - one admin panel. Here's what's in the code: #urls.py from django.urls import path, include from .views import main, CustomLoginView from PKM.admin import pkm_admin_site from NTZ.admin import ntz_admin_site urlpatterns=[ path('', main, name='main'), path('ntz/',ntz_admin_site.urls), path('pkm/', pkm_admin_site.urls), path('accounts/', include('django.contrib.auth.urls')), path('accounts/login/', CustomLoginView.as_view(), name='login') ] #login.html {% load static %} <!DOCTYPE html> <html lang="ru"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>{% block title %}Log in to your personal account{% endblock %}</title> </head> <body> <div class="login-container"> <h2>Login to your personal account</h2> <form method="post"> {{ form.as_p … -
How to fix this state management and improve conditional rendering of react elements
I'm pretty dang new to react, and I'm trying to understand general best practices for state management, especially when state conditionally renders different react elements. I am looking for general best practices on how to properly structure the below code in order to manage state effectively and properly conditionally render my page's content. Below is a quick code snippet showing how I am getting information about an "upstream" element, and using that information to conditionally render different form options in a "downstream" element. The general idea is that based on the type of document the user uploads, that then determines the different settings options they have available to them. I can tell that I'm writing spaghetti code, but I'm not sure how to refactor it/reorganize the code to make it cleaner. Below is a code snippet showing how I conditionally render different elements based on the fileType stored in inputType. <div className="card-body" style={{ padding: "10px" }}> {(inputType === 'txt' || inputType === 'docx') && <TextExtractHandler id ={id} />} {inputType === 'image' && <div>Image Input Handler Placeholder</div>} {inputType === 'video' && <div>Video Input Handler Placeholder</div>} {(inputType === 'csv' || inputType === 'xlsx') && <div>Spreadsheet Input Handler Placeholder</div>} </div> Here is my … -
Django constraint on derived fields
I am trying to add a non-overlaping constraint on a Postgres specific field DateRange field: class MyModel(models.Model): timespan = DateRangeField(blank=False, null=False) status = models.CharField(max_length=8, choices=status_choices, blank=False, null=False) class Meta: constraints = [ ExclusionConstraint( name='exclude_overlapping_offer', expressions=[ ('timespan', RangeOperators.OVERLAPS), (models.Q(status='ACCEPTED'), RangeOperators.EQUAL), ], ) Basically I want to prevent having overlapping entries in the database that have status ACCEPTED. The migration runs fine, but then when I try to save the model, I get an error: AttributeError: 'Q' object has no attribute 'replace_expressions' There is a reply on a bug report that says that Q objects are not allowed in the expressions of the constraint: https://code.djangoproject.com/ticket/34805 Is there some other way to have the constraint on a derived field? -
Is there a more efficient way to handle dynamic field creation in Django REST Framework serializers?
I am working with Django REST Framework and trying to dynamically add fields to a serializer based on data selected from the UI. Specifically, I need to add fields for each commodity associated with a KindTournamentSerializer instance. These fields should represent the quantity of each commodity. from rest_framework import serializers class KindTournamentSerializer(CelestialSerializer): voucher_id = serializers.CharField(label=_("Ration number")) voucher_size = serializers.CharField(label=_("Ration size")) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # Dynamically determine commodities from the instance if self.instance: selected_fields = kwargs.get('selected_fields', []) if 'voucher_size' in selected_fields: # Get the first instance to extract commodities first_instance = self.instance[0] if isinstance(self.instance, TournamentSerializer) else self.instance commodities = first_instance.rations.all() # Create fields for each commodity for ration in commodities: commodity_name = ration.name sanitized_name = commodity_name.replace(" ", "_").replace("-", "_") field_name = f"commodity_{sanitized_name}" # Add the SerializerMethodField commodity_field = serializers.SerializerMethodField(label=_(commodity_name)) self.fields[field_name] = commodity_field # Dynamically bind the method for the field setattr(self, f'get_{field_name}', lambda obj, name=commodity_name: self.get_commodity_value(obj, name)) def get_commodities_data(self, obj): """Aggregate quantities of commodities from the latest voucher.""" latest_voucher = obj.latest_voucher commodity_data = {} if latest_voucher: commodities = latest_voucher.commodities.all() for commodity in commodities: name = commodity.name quantity = commodity.quantity if name not in commodity_data: commodity_data[name] = 0 commodity_data[name] += quantity return commodity_data def get_commodity_value(self, obj, commodity_name): """Retrieve the … -
Django-filer. Sorting files in the admin panel
Is there any way to change the sorting of files in admin panel by download date? So that the most recently downloaded files are always at the top of the list. I didn't find any suitable settings in the django-filer documentation https://django-filer.readthedocs.io/ -
How do I create clickable text as a URL in Django Admin?
I have a Django REST API app, and I'm trying to create a clickable link from text. So far, I've only found solutions where the URL itself is clickable, and that’s how the current functionality works. However, what I want to achieve is having some text (for example, "ad"), and then having a separate field for the URL. When a user clicks on the text ("ad"), they should be redirected to the URL provided in the other field. So I have in model this two fields: cites = models.CharField(max_length=100, verbose_name="Cites", blank=True) cites_url = models.URLField(max_length=200, verbose_name="Cites URL", blank=True) And in admin.py: from django.utils.html import format_html class AnimalAdmin(admin.ModelAdmin): actions = ['export_to_excel'] inlines = [AnimalImageInline, AnimalFileInline] fields = [ 'cites', 'cites_url', ' ] readonly_fields = ['img_preview', 'klasse_name'] autocomplete_fields = ['category'] list_display = ('cites_link') def cites_link(self, obj): # Return a clickable link with the text from cites and URL from cites_url if obj.cites_url and obj.cites: return format_html('<a href="{}" target="_blank">{}</a>', obj.cites_url, obj.cites) elif obj.cites_url: return format_html('<a href="{}" target="_blank">Click here</a>', obj.cites_url) return "No CITES URL provided" cites_link.short_description = "CITES Link" In Django Admin, you can enter text like "ad" in one field and a URL like https://www.ad.nl in another field. However, in the current version, the … -
request from Django to djangorestframework from the same Docker Container timeouts
I have 2 containers with docker compose: services: web: container_name: web build: context: . dockerfile: Dockerfile command: bash -c "python manage.py makemigrations && python manage.py migrate && python manage.py collectstatic --no-input && gunicorn mysite.wsgi:application --bind 0.0.0.0:8000" volumes: - .:/app - static:/app/static env_file: - .env ports: - "8000:8000" nginx: build: ./nginx volumes: - static:/app/static ports: - "80:80" depends_on: - web here is the nginx config: upstream django { server web:8000; } server { listen 80; location / { proxy_pass http://django; proxy_set_header X-Forwarded-Proto $scheme; } location /static/ { alias /app/static/; } # Increase client max body size to 50M client_max_body_size 50M; } In the web container I am running django app where is also djangorestframework. If I try the API endpoints with postman it works fine. Problem is when from Django view I try call the API endpoint it timeouts. Any idea what is wrong? Thank you for you time. -
Can't create two sessions- one fails one works
I have a post request in django views from which I am calling a selenium webdriver, I am running it on linux I have set the DISPLAY in the os environment Now when I call the request it does open the selenium browser normally but when the driver is still open and i call the request again the selenium browser cant create the session DJANGO uses wsgi application. Switching to asgi may not work. I was expecting to have two drivers open but it fails on the second one. Threading is not an issue here class Test(generics.CreateAPIView): def create(self, request, *args, **kwargs): searchDict = dict(request.data) profile = searchDict['profile'] threading.Thread(target=self.runDriver, args = (profile,)).start() return Response(status=status.HTTP_200_OK) def runDriver(self, profile): br = Driver() useragent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0' profile_name = profile username = 'samsan' password = '##1234sAMIUL.' br.createUCDriver(useragent, profile_name) dr = br.ucDriver dr.get('https://www.google.com/') time.sleep(10) dr.get(dr.current_url) This is the Driver class class Driver: def __init__(self): self.driver = None self.ucDriver = None self.waitTime = 10 # self.logger = Logger() # self.executable = input("Please enter chrome executable path: ") self.executable = "seleniumDriver/chromedriver-mac-arm64/chromedriver" def createUCDriver(self, user_agent=None, profile_name='Profile 2'): # os.environ['DISPLAY'] = ':10.0' options = webdriver.ChromeOptions() if user_agent: … -
How to access image files sent usingajax in django backend using
I'm having a problem with sending files from ajax to django. I collect template form using new formData() and append additional data to it, then i try to submit using ajax. On submission, i print out the submitted data in web console and i clearly see keys corresponding to dictionary containing the names of chosen images (images collected in form using input tag) and i also see other string data but when it reaches django end i print json.loads(request.body) and i only see the string data while the keys that corresponded to images are now empty dictionaries . And also request.FILES is empty. js code function accept(formdata){ const data1=Object.fromEntries(formdata.entries()); console.log(data1);//prints all content and i see image names in it $.ajax( {type:'POST', url:document.getElementById('url').getAttribute('data-url'), data:JSON.stringify({"formdata":data1}), headers:{ "X-Requested-With":"XMLHttpRequest", "X-CSRFToken":getCookie("csrftoken"), }, success:(data) =>{ console.log('data'); }, dataType:'json', contentType:false, processData:false } ); } function set_table(event){ event.preventDefault(); const table = document.getElementById('tb'); const list=table.children; console.log(list.length); let arr=[]; for (let row =0; row<list.length;row++ ){ let rows=list[row]; if (rows.getAttribute('name') !='head'){ let data={ "agent":rows.querySelector('#personnel').value, "number":rows.querySelector('#number').value , "work":rows.querySelector('#for').value }; arr.push(JSON.stringify(data)); } } //console.log(event.target.data); const formdata=new FormData(event.target); formdata.append('tab',arr); accept(formdata); } django views def project_creation_page(request): context={"cat":project_cat} is_ajax = request.headers.get('X-Requested-With') == 'XMLHttpRequest' if is_ajax: print(34) if request.method=='POST': data=json.loads(request.body) print(data) for file ,files in request.FILES: print(file, … -
JSON Serialize for Following User
I have model.py as below. class User(AbstractUser): pass def serialize(self): return { "userid" : self.user.id, "username": self.username } def __str__(self): return f"{self.id} {self.username}" class Profile(models.Model): id = models.AutoField(primary_key=True) user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="user_profile") follower = models.ManyToManyField(User, blank=True, related_name="following") def __str__(self): return f"{self.user.username}" def serialize(self): return { "user_id": self.user.id, "user": self.user.username, "followers": self.follower.count(), "followers_usr": [user.username for user in self.follower.all()], "following": self.user.following.count(), "following_usr": [user.username for user in self.user.following.all()] } I would like to create a profile page showing user profile, follower and following count also list of users in follower and following. I'm using return JsonResponse in my views.py. Everything works well until "following_usr". I tried using "following_usr": self.user.following.all() and got this error: TypeError at /profile/1 Object of type QuerySet is not JSON serializable When I try "following_usr": [user.username for user in self.user.following.all()] got this error: AttributeError at /profile/1 'Profile' object has no attribute 'username' What is the proper way to do this? -
is it possible to use the azure blob storage as a postgresql database server in a django project?
I want to apply the PostgreSQL database in a azure blob storage In my Django Project. Because azure blob storage is cheaper than the SQL database on azure. Is technically possible ? -
Django Templates Not Rendering (Only Admin Page Shows)
Problem: My Django project displays only the default admin page when I run the server (python manage.py runserver). None of my custom HTML templates are rendering. What I've Tried: I've reset the URLs and views in all apps, including the main app. Expected Behavior: I expect to see my custom HTML pages displayed when the server starts. -
fastcgi_buffers vs. proxy_buffers when serving Django API using Daphne
I'm running two containers in a docker network (either through docker-compose or AWS ECS): Daphne for ASGI to Django Nginx for forwarding requests to Daphne and serving static files This is working fine. I can request API endpoints, use the Django admin site, load static files, etc. The problem is that some API responses are enormous (>10MiB), and while the endpoints returning these massive responses are very optimized and fast themselves, I'm getting timeouts due to buffering of the the response. I can tell by logs such as: [warn] 28#28: *252 an upstream response is buffered to a temporary file /var/cache/nginx/proxy_temp/1/00/0000000001 while reading upstream, client: 1.2.3.4, server: _, request: "GET /my-app/123/very-big-response/ HTTP/1.1", upstream: "http://172.17.0.2:9000/my-app/123/very-big-response/", host: "app.acme.com", referrer: "https://app.acme.com/" I have spent the past few hours reading about various nginx buffer settings, and while the docs explain fully what the options are and mean, I cannot find clear and reliable information on: Strategies for determining ballpark values for these parameters Which exact nginx directives to use To reiterate, I have two containers: #1 (daphne/django), and #2 (nginx). Container #1 (daphne/django) uses supervisord to run daphne. (Note before I continue: I'm fully aware of some other deviations from best practices here, like … -
Is this possible way to save the token in the existing table and use it for login logout change password forgot password also expirations works fine?
Should use the existing table for token to be stored and also for the reset end forget password token to be stored in the same table with expirations To implement a secure password change mechanism that involves storing JWT tokens in the database for verification, you need to modify the previous solution to save and validate the tokens from the database. Here's a step-by-step guide to implementing this: Step 1: Create a Model for Storing JWT Tokens Create a new model in your Django app to store JWT tokens. This model will include fields for the token, the user it’s associated with, and its expiration status. # models.py in your Django app from django.db import models from django.contrib.auth.models import User from django.utils import timezone class PasswordResetToken(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='reset_tokens') token = models.CharField(max_length=255, unique=True) is_expired = models.BooleanField(default=False) created_at = models.DateTimeField(auto_now_add=True) def __str__(self): return f'Token for {self.user.username}' def has_expired(self): # Check if the token has expired (assuming 30 minutes expiry time) expiry_time = self.created_at + timezone.timedelta(minutes=30) return timezone.now() > expiry_time Step 2: Update the request_password_change View Modify the request_password_change view to generate a JWT token, save it in the database, and send it via email. # views.py in your Django … -
Why we need sync_to_async in Django?
The document said: The reason this is needed in Django is that many libraries, specifically database adapters, require that they are accessed in the same thread that they were created in. Also a lot of existing Django code assumes it all runs in the same thread, e.g. middleware adding things to a request for later use in views. But another question Is it safe that when Two asyncio tasks access the same awaitable object? said python's asyncio is thread safe. And as I know since the GIL still exist accessing one object from multiple thread should be thread safe. Can any one give a minimal example for why we have to use await sync_to_async(foo)() instead of directly foo() in django or other async apps? -
Django GeneratedField as ForeignKey with referential integrity
I'm trying to create a generated field that is also a foreign key to another table, while maintaining referential integrity at the database level. Basically, I'm trying to have the same effect as the following SQL, but in Django CREATE TABLE parent( id TEXT PRIMARY KEY ); CREATE TABLE child( id TEXT PRIMARY KEY, data JSONB, parent_id TEXT GENERATED ALWAYS AS (data->>'parent') STORED REFERENCES parent(id) ); I have successfully managed to create the generated field using Django 5.0 GeneratedField class Parent(models.Model): id = models.TextField(primary_key=True) class Child(models.Model): id = models.TextField(primary_key=True) data = models.JSONField() parnet_id = models.GeneratedField(expression=models.fields.json.KT('data__parent'), output_field=models.TextField(), db_persist=True) Now the problem is: how can I make this field also a foreign key? Because using ForeignKey would create a new column in the database that is not generated. I tried using ForeignObject, since it supports using an existing field as a foreign key, but the foreign key constraint was not created at the database level. class Parent(models.Model): id = models.TextField(primary_key=True) class Child(models.Model): id = models.TextField(primary_key=True) data = models.JSONField() parnet_id = models.GeneratedField(expression=models.fields.json.KT('data__parent'), output_field=models.TextField(), db_persist=True) parent = models.ForeignObject(Parent, from_fields=['parnet_id'], to_fields=['id'], on_delete=models.CASCADE) This generates the following SQL, which does not have a foreign key constraint CREATE TABLE "myapp_parent" ("id" text NOT NULL PRIMARY KEY); CREATE … -
Django ModuleNotFoundError: No module named 'anthropic'
I am trying to use the anthropic api within my django application. I am able to access and make requests to the API within a normal python file, but when I try doing the same within my django app (specifically my views.py file), it does not recognize anthropic as a module I have installed. Was wondering if anyone can help with this. I have tried including anthropic within my settings.py file but this didn't resolve the error. The anthropic docs for python sdk: https://github.com/anthropics/anthropic-sdk-python -
Django Prefetch Related Still Queries
I have the function def selected_location_equipment(self): qs = (Equipment.objects .prefetch_related('service_logs') .all()) return qs That returns a queryset with a few related fields grabbed. The problem is that when i access the prefetched data later in my code, it executes a query again. Ive stepped through the Django code and can see where it is checking the cache for the .all() in one spot and doesnt query, but then when its called here, it's almost like the cache is cleared. Debug Toolbar shows a query for each iteration of the loop as well. for e in equipments: last_service = list(e.service_logs.all())[-1] ... Here's the basic model definition for Equipment class ServiceLog(models.Model): equipment = models.ForeignKey(Equipment, on_delete=models.CASCADE, related_name='service_logs') -
Error: Failed to Clone MapStore2 Submodule in GeoNode Docker Build
Question: I'm trying to build a GeoNode instance using Docker, but I'm encountering an error related to cloning the MapStore2 submodule from the geonode-mapstore-client repository. Below is the error output I'm receiving: bash => [django 12/16] RUN yes w | pip install --src /usr/src -r requirements.txt 225.0s => => # fatal: fetch-pack: invalid index-pack output => => # fatal: clone of 'https://github.com/geosolutions-it/MapStore2.git' into submodule path '/usr/src/django-geonode-mapstore-client/geonode_mapstore_client/client/MapStore2' failed => => # Failed to clone 'geonode_mapstore_client/client/MapStore2'. Retry scheduled Full Error Output: => ERROR [django 12/16] RUN yes w | pip install --src /usr/src -r requirements.txt && yes w | pip install -e . ... 202.5 error: RPC failed; curl 92 HTTP/2 stream 0 was not closed cleanly: CANCEL (err 8) ... 319.8 note: This error originates from a subprocess, and is likely not a problem with pip. Dockerfile Snippet: dockerfile Copier le code FROM geonode/geonode-base:4.1.0-ubuntu-22.04 LABEL GeoNode development team # Copy local GeoNode src inside container COPY /src/. /usr/src/geonode/ WORKDIR /usr/src/geonode # Configurer Git pour augmenter la mémoire tampon et gérer les faibles vitesses de téléchargement RUN git config --global http.postBuffer 524288000 && \ git config --global http.lowSpeedLimit 0 && \ git config --global http.lowSpeedTime 999999 RUN yes w | pip install … -
Wagtail: How to validate page model relations before saving
I have a page type where I want to define the title (and slug) automatically, based on some other fields and higher level model relations. To achieve that, I override the full_clean() method of my page class. The only thing that can go wrong is that the new page gets a slug that is already in use among the sibling pages. That is intended, only pages with unique field combinations that will influence the slug should exist. So, if a user tries to save a page with a duplicate combination of data fields, I want to display a nice and readable ValidationError. I understand that the full_clean() method is called multiple during editing/saving pages, following a kind of hierarchical approach, where the cleaning procedure starts with basic stuff and goes up to model relations. It seems that ValidationErrors are only caught in the UI and displayed nicely when they are not raised in the presumably last call of full_clean(), right after hitting the save button. When I raise a ValidationError when I have all the information at hand, it's not caught and the traceback is shown. Is there any way to handle a ValidationError gracefully if I only can raise … -
Django - Multiple images upload and delete
I'm looking at improving my code that allows the user to upload multiple images (files) linked to a record, and also delete them as needed. I am able to handle the first part (multiple images) with the following. models.py class APIvisit(ModelIsDeletable, SafeDeleteModel): _safedelete_policy = SOFT_DELETE created_date = models.DateTimeField(auto_now_add=True,editable=False, verbose_name=_("Créé le")) modified_date = models.DateTimeField(auto_now=True,editable=False, verbose_name=u"Modifié le") visitdate = models.DateField(_('Date de la visite'),default=datetime.date.today) [...] a lot of other fields class Meta: ordering = ('visitdate',) class APIvisitimage(ModelIsDeletable, SafeDeleteModel): _safedelete_policy = SOFT_DELETE created_date = models.DateTimeField(auto_now_add=True,editable=False, verbose_name=_("Créé le")) modified_date = models.DateTimeField(auto_now=True,editable=False, verbose_name=u"Modifié le") fk_visit = models.ForeignKey(APIvisit, on_delete=models.PROTECT, related_name=_('ImageVisite'), verbose_name=_('ImageVisite'), blank=False, null=False) image = models.FileField(upload_to="uploads/%Y/%m/%d/") forms.py from django.forms.widgets import ClearableFileInput class MultipleFileInput(forms.ClearableFileInput): allow_multiple_selected = True class MultipleFileField(forms.FileField): def __init__(self, *args, **kwargs): kwargs.setdefault("widget", MultipleFileInput()) super().__init__(*args, **kwargs) def clean(self, data, initial=None): single_file_clean = super().clean if isinstance(data, (list, tuple)): result = [single_file_clean(d, initial) for d in data] else: result = single_file_clean(data, initial) return result [...] class VisitImageForm(ModelForm): class Meta: model = APIvisitimage fields = ["image"] image = MultipleFileField(label='Choisir les photos', required=False) views.py class VisitEditView(PermissionRequiredMixin, UpdateView): permission_required = 'gestion.change_apivisit' model = APIvisit form_class = VisitForm template_name = 'visite/edition.html' success_url = '/visite/' def form_invalid(self, form): self.object_list = self.get_queryset() context = self.get_context_data(task_form=form) return self.render_to_response(context) def post(self, request, *args, **kwargs): self.object = self.get_object() …