Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Trying to filter "managers" field to only show managers in the currently logged in user's department
I have 3 models The base User model that Django provides UserProfile, a model that has 2 fields: a foreignkey field with the user table a department field thats a choicefield with a dropdown, every user has 1 department And finally and Event model that contains a Manager field, a manytomany with the user model. I am trying to filter the manager field on the backend to only show managers that share the same department, since we don't want people to be able to add other departments as managers in their events This is the function that I'm trying to overwrite in my admin.py (using Django 5.0) def formfield_for_manytomany(self, db_field, request, **kwargs): if db_field.name == "managers": kwargs["queryset"] = Data.objects.filter(managers.userprofile.department == request.user.userprofile.department) return super().formfield_for_manytomany(db_field, request, **kwargs) I keep getting errors about the managers field not having a attribute of userprofile -
How serializer field can be set without showing it to enduser in DRF?
I have url like doc/<int:doc_id>/price/ for get and post and doc/<int:doc_id>/price/<int:id> for delete and patch or retrieve. My problem is, when creating a new price I want to set price.doc_id=self.kwargs['doc_id'] but at the same time I want that serializer field to be hidden in swagger documentation(I use drf-spectacular). The end-user should not be aware of that field. I know I can overwrite the create function of serializer and explicitly set the price or use serializers.save(doc=doc). But I ask this question to get a better solution. I tried hiddenfield as well but apparently it only takes default value and I do not know how to pass doc_id to it. class DocPriceOutHistorySerializer(ModelSerializerWithValidator): currency_name = serializers.CharField(read_only=True) type_price_name = serializers.CharField(read_only=True) class Meta: model = models.DictPriceList fields = ( 'id', 'dat', 'price', 'currency', 'currency_name', 'type_price', 'type_price_name', 'obj', 'doc', ) and in views.py data = request.data data['doc'] = self.kwargs['doc_id'] serializer = self.get_serializer(data=data) serializer.is_valid(raise_exception=True) self.perform_create(serializer) this will work but I do not want to manually write a request schema for swagger. -
how to use AsyncIOScheduler in django?
I am developing an API using Djaongo Rest Framework that periodically calls an asynchronous method through AsyncIOScheduler(a component of APScheduler), making it run every few seconds. However, it is not working as expected, and nothing is printed to the console. Below is my code. Scheduler definition: scheduler = AsyncIOScheduler() log = logging.getLogger(__name__) def start_scheduler(): if not scheduler.running: scheduler.start() logging.info('scheduler started') Code to start the scheduler: class ApiConfig(AppConfig): name = 'api' def ready(self): from .scheduler import start_scheduler start_scheduler() API endpoint definition: class Boo(): async def test(): print("testFoo") await asyncio.sleep(1) print("testFoo done") class Foo(): def __init__(self): self.insideObjs = [Boo() for _ in range(10)] async def run(self): tasks = [boo.test() for boo in self.insideObjs] logging.info(f'waiting for jobs done') results = await asyncio.gather(*tasks) logging.info(f'all jobs done') class FooViewSet(viewsets.ViewSet): @action(detail=False, methods=['post'], url_path='start') def start(self, request): foo = Foo() scheduler.add_job(foo.run, 'interval', seconds=3, id="123", replace_existing=True) In my expection,'testFoo' and 'testFoo done' should be print in the console every 3 seconds,but they actually not,I tried to change my ready() function of ApiConfig class from: class ApiConfig(AppConfig): name = 'api' def ready(self): from .scheduler import start_scheduler start_scheduler() to class ApiConfig(AppConfig): name = 'api' def ready(self): from .scheduler import start_scheduler start_scheduler() try: asyncio.get_event_loop().run_forever() except (KeyboardInterrupt, SystemExit): pass then django server … -
Setup API response Logs to EC2 Django development Server with Gunicorn
I am new to EC2 and web development. Currently I have a Amazone Linux EC2 instance running, and have installed Django. I am creating a project. I already setup ngnix gunicorn configruation and run the project. But i don't want response like local API response logs. I run this command tail -f /var/log/gunicorn/mdm_access.log and this is response 127.0.0.1 - - [28/Jun/2024:12:55:35 +0000] "GET /auth.html HTTP/1.0" 404 2312 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36" 127.0.0.1 - - [28/Jun/2024:12:55:35 +0000] "GET /api/sonicos/auth HTTP/1.0" 404 2333 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36" 127.0.0.1 - - [28/Jun/2024:12:55:35 +0000] "GET / HTTP/1.0" 404 179 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36" 127.0.0.1 - - [28/Jun/2024:12:55:35 +0000] "GET /auth1.html HTTP/1.0" 404 2315 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36" 127.0.0.1 - - [28/Jun/2024:12:55:35 +0000] "GET /sslvpnLogin.html HTTP/1.0" 404 2333 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36" 127.0.0.1 - - [28/Jun/2024:12:55:35 +0000] "GET /api/sonicos/tfa HTTP/1.0" 404 2330 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 … -
Creating a new thread in an API in django and calling another API in that thread
I was told that creating a thread for running background tasks will cause problems because in production gunicorn will kill a thread once the response is recieved. So, if I create a thread and inside that I call another api which is defined in the same app in django, will gunicorn create another thread for this api and thus wait for it to finish processing ? -
Huey process stuck, but why?
I'm trying to debug why our huey queue seems to freeze. We have a huey queu with 4 threads, however since a few days it seems to freeze. My hypotheses is that there is a method/task that keeps on running, and is stopping other tasks from processing. However it's a rather large project with a lot of stuff going into the queue. Huey settings: 'huey_class': 'huey.PriorityRedisHuey', 'name': 'queue', 'result_store': True, 'events': True, 'store_none': False, 'always_eager': False, 'store_errors': True, 'blocking': False, 'connection': { 'host': 'localhost', 'port': 6379, 'db': 0, 'connection_pool': None, 'read_timeout': 1, 'url': None, }, 'consumer': { 'workers': 4, 'worker_type': 'thread', 'initial_delay': 0.1, 'backoff': 1.15, 'max_delay': 10.0, 'utc': True, 'scheduler_interval': 1, 'periodic': True, 'check_worker_health': True, 'health_check_interval': 1, }, How can I debug this? Perhaps there is a setting that I can use to kill any task after x-time and log this somehow? Or perhaps something else I'm missing? Or perhaps there is a command to see what is currently being processed? -
How to Resolve Null Field Errors in Django When Creating a Task with Subtasks and Users
I'm encountering issues when trying to create a task with subtasks and users in my Django application. The task creation fails due to validation errors, specifically with the subtasks and users fields. Here is the payload I'm sending to the backend: { "id": null, "title": "aa", "description": "aaaa", "due_to": "2024-06-27T22:00:00.000Z", "created": null, "updated": null, "priority": "LOW", "category": "TECHNICAL_TASK", "status": "TO_DO", "subtasks": [ { "task_id": null, "description": "s1", "is_done": false }, { "task_id": null, "description": "s2", "is_done": false } ], "users": [ { "id": 6 }, { "id": 7 } ] } When I attempt to create the task, I receive the following error message from the backend: { "subtasks": [ { "task_id": [ "This field may not be null." ] }, { "task_id": [ "This field may not be null." ] } ], "users": [ { "email": [ "This field is required." ], "password": [ "This field is required." ], "name": [ "This field is required." ] }, { "email": [ "This field is required." ], "password": [ "This field is required." ], "name": [ "This field is required." ] } ] } I suspect the issue is related to how I'm handling the nested serializers and the model relationships … -
Is it possible to use the same Django application name in multiple locations within a project?
I have a similar project structure to one below. project_name/ │ ├── apps/ │ ├── items/ │ │ ├── purchases/ │ │ │ ├── migrations/ │ │ │ ├── templates/ │ │ │ ├── __init__.py │ │ │ ├── admin.py │ │ │ ├── apps.py │ │ │ ├── models.py │ │ │ ├── tests.py │ │ │ └── views.py │ │ │ │ │ └── sales/ │ │ ├── migrations/ │ │ ├── templates/ │ │ ├── __init__.py │ │ ├── admin.py │ │ ├── apps.py │ │ ├── models.py │ │ ├── tests.py │ │ └── views.py │ │ │ └── tools/ │ ├── purchases/ │ │ ├── migrations/ │ │ ├── templates/ │ │ ├── __init__.py │ │ ├── admin.py │ │ ├── apps.py │ │ ├── models.py │ │ ├── tests.py │ │ └── views.py │ │ │ └── sales/ │ ├── migrations/ │ ├── templates/ │ ├── __init__.py │ ├── admin.py │ ├── apps.py │ ├── models.py │ ├── tests.py │ └── views.py │ ├── project_name/ │ ├── __init__.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py │ └── manage.py settings INSTALLED_APPS=[ ... "items.purchases", "items.sales", ... "tools.purchases", "tools.sales", ... ] I am getting the … -
operator classes are only allowed for the last column of an inverted index
Since I migrated my Django application from MariaDb to CockraochDB which is basically Postgres I get the following error, I really do not understand, I also do not find any referenced on the web or where I can start to debug this. At my Django Application, I first do manage.py make migrations, to generate the migrations files, after that I do manage.py migrate to apply these against my CockroachDB and this happens: Running migrations: Applying App_CDN.0001_initial...Traceback (most recent call last): File "/venv/lib/python3.12/site-packages/django/db/backends/utils.py", line 103, in _execute return self.cursor.execute(sql) > ^^^^^^^^^^^^^^^^^^^^^^^^ File "/venv/lib/python3.12/site-packages/django_prometheus/db/common.py", line 69, in execute return super().execute(*args, **kwargs) > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ psycopg2.errors.DatatypeMismatch: operator classes are only allowed for the last column of an inverted index The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/app/manage.py", line 15, in <module> execute_from_command_line(sys.argv) File "/venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line utility.execute() File "/venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 436, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/venv/lib/python3.12/site-packages/django/core/management/base.py", line 413, in run_from_argv self.execute(*args, **cmd_options) File "/venv/lib/python3.12/site-packages/django/core/management/base.py", line 459, in execute output = self.handle(*args, **options) > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/venv/lib/python3.12/site-packages/django/core/management/base.py", line 107, in wrapper res = handle_func(*args, **kwargs) > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/venv/lib/python3.12/site-packages/django/core/management/commands/migrate.py", line 356, in handle post_migrate_state = executor.migrate( > ^^^^^^^^^^^^^^^^^ File "/venv/lib/python3.12/site-packages/django/db/migrations/executor.py", line 135, in … -
Django OperationalError: MySQL Connection not available
I'm encountering an issue with connecting Django to MySQL using mysql.connector.django. Here are the details: Error Message: OperationalError: (-1, 'MySQL Connection not available.', None) I'm setting up a Django project (WanderSight) with MySQL as the database backend. I've configured DATABASES in settings.py as follows: DATABASES = { 'default': { 'ENGINE': 'mysql.connector.django', 'NAME': 'seyedbe6_WanderSight', 'USER': 'seyedbe6_WanderSight', 'PASSWORD': '^uqQBK0*Fv59', 'HOST': 'localhost', 'PORT': '3306', } } Steps Taken: Verified MySQL server is running and accessible via command line (mysql -u seyedbe6_WanderSight -p). Installed mysql-connector-python in my virtual environment. Restarted Django development server (python manage.py runserver). Despite these steps, I'm still encountering the "MySQL Connection not available" error when running Django management commands like python manage.py createsuperuser. Django version: 4.1.10, MySQL version: Ver 14.14 Distrib 5.7.43, Python version: 3.9.16, I would appreciate any insights into what might be causing this issue or additional steps I could take to diagnose and resolve it -
How to convert openai api markdown response to html
I'm new to Django and looking for a way to render OpenAI Markdown streaming to HTML on the fly using a Django backend. I want the rendered content to resemble ChatGPT's interface. I prefer not to use any React or JavaScript frameworks—just HTML and some JavaScript. Is there a shortcut to achieve this? -
Can Celery Be Used for Background Tasks Without Redis in Production?
I have an API that needs to upload large files to S3, with file sizes close to 10GB. I want to handle this as a background task and am considering using Celery. However, the client won't accept the additional cost of using a Redis service. Is it possible to implement this background task without using Redis? Any resources for this would be appreciated. -
What if I forgot to activate virtual environment working on Django project?
I have been working on a Django project in Python Crash Course 2nd edition. In the book it was said that I need to create virtual environment and that I need to activate it every time. I did create it but at some point I forgot about it and stopped activating venv. I made a LOT of changes since the last time I activated it. Now I know that virtual environment has to be activated, I read about it on several websites including Stack Overflow. And I do know how to do it. But I couldn't find what would happen if I didn't activate venv? Will there be some kind of consequences? If yes, how to fix it? -
How do I add a javascript file in the default model form in admin.py?
I want to add a javascript function in the default model form that renders on the django admin site. I do not have a separate html template that loads the form. I have used the Media class inside the model form but where do I load it from? Any clues? I have created an 'Example' model in models.py. I have added additional fields to render on the admin panel. admin.py class ExampleForm(forms.ModelForm): class Meta: model = Example fields = "__all__" username = forms.CharField(required=False, max_length=100) password = forms.CharField(widget=forms.PasswordInput, required=False, max_length=100) class Media: js = ('static/tenants/js/custom.js',) I looked for solutions online which asks to include the script tag in the html template. But I am not using a template. Assuming I have to add a template to add javascript on that form, which template do I use? I don't want to change the default design that django provides. -
Django Jenkins pipeline using wsl
I have a silly question, can we make a CI/CD pipeline for a django project using Jenkins and I don't have any access to cloud portals, can I do it using wsl Till now I have not seen any article or video doing this, is it possible.? I don't have any idea as I want to learn this thing -
DJANGO_MIGRATION_NOT_WORKING_gETTING_CONSTANT_ERROR_IN_SHOP_URL
I have been trying to make a project for my internship and i am constantly getting error while migrating in django and its the same error in shop urls. Any type of help will be appreciated as this internship is important to me and i dont wanna waste this opportunity url.py in myproject url.py in `shop image of the error shown I tried renaming and rechecking the names assigned and tried renaming them yet no improvement was found I even tried using chatgpt and restarted my server yet no sign of improvement I was expecting that my project will start by doing these changes -
No url reverse match of pk with a correct setup (most likely)
I'm trying to implement inline editing feature using HTMX in Django form. I've checked the url routing several times and am pretty sure, that everything should be correct. I'm gettings django.urls.exceptions.NoReverseMatch: Reverse for 'update_work_hours' with arguments '('',)' not found. 1 pattern(s) tried: ['nuomotojo\\-platforma/darbo\\-laikas/atnaujinti/(?P<pk>[0-9]+)/\\Z'] error while executing get request. Template (delete works perfectly fine, so I assume, that hours.pk is passed correctly): <div id="work_hour_day" class="flex flex-col md:flex-row items-center my-2"> <span>{{ hours.get_day_of_week_display }}: {{ hours.opening_time }}-{{ hours.closing_time }}</span> <div class="flex gap-2 mx-2"> <button hx-get="{% url 'rentals:update_work_hours' hours.pk %}" hx-target="closest #work_hour_day" hx-swap="outerHTML" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 dark:bg-blue-600 dark:hover:bg-blue-700 focus:outline-none dark:focus:ring-blue-800">Keisti</button> <button hx-delete="{% url 'rentals:delete_work_hours' hours.pk %}" hx-target="closest #work_hour_day" hx-swap="outerHTML swap:1s" hx-trigger='confirmed' onClick="Swal.fire({title: 'Confirm', text: 'Do you want to continue?', icon: 'question'}).then((result)=>{ if(result.isConfirmed){ htmx.trigger(this, 'confirmed'); } })" class="focus:outline-none text-white bg-red-700 hover:bg-red-800 focus:ring-4 focus:ring-red-300 font-medium rounded-lg text-sm px-5 py-2.5 dark:bg-red-600 dark:hover:bg-red-700 dark:focus:ring-red-900" type="button">Ištrinti</button> </div> </div> Views.py: class UpdateRentalWorkingHours(View): def get(self, request, pk): working_hours = get_object_or_404(RentalWorkingHours, pk=pk) form = UpdateWorkingHoursForm(instance=working_hours) day_of_week = working_hours.get_day_of_week_display() # If request from cancel button in the form, return the initial data trigger = request.headers.get("HX-Trigger") if trigger == "get_working_hours_row": return render( request, "rentals/partials/work_hour_row.html", {"hours": working_hours}, ) else: context = {"form": form, "day_of_week": day_of_week} return render( request, … -
Django and WebSocket in production
I'm working with real-time updates in my Django project for the first time, and it works well locally, but I have an error in production (Microsoft Azure). Note: I read a couple of articles about using Redis instead of InMemoryChannelLayer. The Error WebSocket connection to 'wss://mydomain.com/ws/orders/' failed: (anonymous) @ orders/:288 The Code index.html <script type="text/javascript"> const orderId = '{{ order.id }}'; let url = `wss://${window.location.host}/ws/orders/`; const orderSocket = new WebSocket(url) orderSocket.onmessage = function(e){ let data = JSON.parse(e.data); document.querySelector('.backdrop').classList.remove('d-none'); setTimeout(() => { location.reload(); }, 3000); } </script> settings.py ALLOWED_HOSTS = ['mydomain.com', '127.0.0.1', 'localhost'] # my domain refers to my website domain CHANNEL_LAYERS = { "default": { "BACKEND": "channels.layers.InMemoryChannelLayer", }, } ASGI_APPLICATION = 'core.asgi.application' INSTALLED_APPS = [ 'channels', # other apps... ] routing.py from django.urls import path from . import consumers websocket_urlpatterns = [ path('ws/orders/', consumers.OrdersCustomers.as_asgi()), ] Things I've tried: CHANNEL_LAYERS = { "default": { "BACKEND": "channels_redis.core.RedisChannelLayer", "CONFIG": { "host": "redis://localhost:6379", }, }, } But didn't know how to configure the host. -
Modelform using Django
i am using a form from modelform class.but its showing an error of "ValueError at /list/ ModelForm has no model class specified". enter image description here my model form- from django.forms import ModelForm from . models import Student class StudentForm(ModelForm): class meta: model=Student fields='all' i tried everything and checked again and again...but not getting the desire output -
i have a issue with inserting image filed in django database
this is my model: from django.db import models class Topic(models.Model): name = models.CharField(max_length=200) def __str__(self): return self.name class Article(models.Model): topic = models.ForeignKey(Topic, on_delete=models.CASCADE) title = models.CharField(max_length=6000) featured_image = models.ImageField(null=True,blank=True, default="default.jpg") # i want to add this filed content = models.TextField() created = models.DateTimeField(auto_now_add=True) updated = models.DateTimeField(auto_now=True) def __str__(self): return self.title i installed python -m pip install pillow and this is my setting configuration: import os STATIC_URL = '/static/' STATICFILES_DIRS = [ BASE_DIR / 'static' ] in the admin panel i am expecting the apparition of featured image filed that sets the image with the default.jpg if the on the article creation the admin doesn't insert a image. After i run python manage.py makemigrations it is always giving me this: It is impossible to change a nullable field 'topic' on article to non-nullable without providing a default. This is because the database needs something to populate existing rows. Please select a fix: Provide a one-off default now (will be set on all existing rows with a null value for this column) Ignore for now. Existing rows that contain NULL values will have to be handled manually, for example with a RunPython or RunSQL operation. Quit and manually define a default value … -
how to connect multiple clients with Django channels and client should be authenticated by server before opening the channel
So I'm working on a desktop based app that requires a socket to handle multiple authenticated clients with a Django channel, and clients should be authenticated by the server before opening the channels. The server is running on one different system, and clients are connecting to multiple systems. I have tried to connect a server with a different platform with much effort, but I have failed to connect it. Is it possible to connect clients on different platforms to the server? If yes, please help me to resolve this issue because I am new to this technology. I need your valuable suggestions or feedback would be highly appreciated. thank your ! -
Value Error when registering user in Django
In my mobile app, whenever I try to register a user in the Django backend, I get "ValueError: Cannot query "user@example.com": Must be "User" instance." Here's the serializer(django): class UserSerializer(serializers.ModelSerializer): friends = serializers.SerializerMethodField() class Meta: model = User fields = ['id', 'name', 'email', 'date', 'password', 'profile_picture', 'friends'] extra_kwargs = {'password': {'write_only': True, 'required': True}} def get_friends(self, obj): return [friend.email for friend in obj.get_friends()] def create(self, validated_data): user = User.objects.create_user(**validated_data) user.save() print(user) print(type(user)) Token.objects.get_or_create(user=user) print("user created") return user Here's the react native where I send the post request to the server. const [name, setName] = useState(''); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const onChange = (field, value) => { if (field === 'name') setName(value); else if (field === 'email') setEmail(value); else if (field === 'password') setPassword(value); }; const onSubmit = async () => { try { let response = await fetch('http://localhost:8000/api/users', { method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify({ name: name, email: email, password: password, }), }); let json = await response.json(); console.log("JSON response:", json); if (response.status === 201) { // Registration successful // You can dispatch a success action here console.log("Registration Successful"); } else { // Registration failed // You can dispatch … -
unique user based caching in django
In django 3.2 what's the cleanest way to access a user id in functions called from views or outside views? I want to implement caching in my project but it needs to be specific to each user. I thought I could simply access the user id and integrate it in the cache key. The problem is doing so requires access to the request object and I often need access within functions called by a view. I don't want to change all my functions to add a request object, unless that's the best solution. Can something like a global be set for the duration of the request to access it anywhere once it hits the view? It looks like this gets into threads and seems hacky. FWIW Currently I'm using django-cache-memoize which does everything I need https://github.com/peterbe/django-cache-memoize and memcached which I also like. -
This field may not be null when creating a task
When I create a task that can have multiple subtasks, I get the error from Django {"subtasks":[{"task_id":["This field may not be null."]}]}. The problem is that I don't know the task_id when I create it. That's why I can't assign a task_id to the subtask yet. How can I solve this problem? { "id": null, "title": "Task", "subtasks": [ { "task_id": null, "description": "aa", "is_done": false } ] } -
Python Project not able to post on schedule time on Facebook or Instagram using Django
not able to post on schedule time on Facebook and Instagram with schedule time i am able to post but i need to sort the schedule time issue not any error appears. Here is the code (VIEWS.PY) def index(request): post_form = BasicForm() if request.method == 'POST': post_form = BasicForm(request.POST, request.FILES) try: if post_form.is_valid(): fss = FileSystemStorage() file = fss.save(request.FILES['post_file'].name, request.FILES['post_file']) file_url = fss.url(file) post_description = post_form.cleaned_data['post_description'] is_schedule = post_form.cleaned_data['schedule_check'] if is_schedule: schedule_time = post_form.cleaned_data['schedule_time'] if schedule_time: # Ensure schedule_time is timezone-aware if timezone.is_naive(schedule_time): aware_schedule_time = timezone.make_aware(schedule_time, timezone.get_default_timezone()) else: aware_schedule_time = schedule_time SchedulePost.objects.create( post_description=post_description, post_file=file_url, fb_check=post_form.cleaned_data['fb_check'], insta_check=post_form.cleaned_data['insta_check'], whatsapp_check=post_form.cleaned_data['whatsapp_check'], schedule_check=True, my_datetime=aware_schedule_time, ) logger.info(f"Scheduled time (selected): {aware_schedule_time.strftime('%Y-%m-%d %H:%M:%S %Z')}") logger.info("Record scheduled...!") return redirect('index') # Redirect to index after scheduling (SETTING.PY) LANGUAGE_CODE = 'en-us' TIME_ZONE = 'Asia/Karachi' USE_I18N = True USE_TZ = True Kindly let me know if any more details are required [28/Jun/2024 02:17:11] "GET /index/ HTTP/1.1" 200 13741 [28/Jun/2024 02:17:54] "POST /index/ HTTP/1.1" 302 0 Tried : The first step is to add USE_TZ = True to your settings file. Also tried the naive_datetime aware as well