Posts

Migrating from pytz to ZoneInfo (python 3.9+)

December 9, 2023
Development, Django
Django, python, pytz, ZoneInfo

Django 5.0 removed pytz in favor of python 3.9+ supported ZoneInfo. Adding tzinfo to naked datetime using pytz: pytz.timezone("Europe/Helsinki").localize(non_tz_datetime) becomes non_tz_datetime.replace(tzinfo=ZoneInfo("Europe/Helsinki")) Converting to timezone using pytz: timestamp.astimezone(pytz.timezone("Europe/Helsinki")) becomes timestamp.astimezone(ZoneInfo("Europe/Helsinki")) Note that ZoneInfo is not available before python 3.9 so to support 3.8 (we have couple of old servers) need to import ZoneInfo with try: import zoneinfo # noqa except ImportError: from backports import zoneinfo # type: ignore # noqa from zoneinfo import ZoneInfo and in requirements. ...

Customizing Django Admin search

October 11, 2023
Development, Django
Django, backend

Steps to override search form in Django admin: Add custom search form Override ChangeList and implement get_filters_params() so that it excludes the new search form fields (otherwise filters don’t work) Override search_form.html because it has hardcoded single search form field Override change_list.html to customize styling (e.g. with flexbox) If search form has many fields, split fields to multiple divs by using cl.search_form.visible_fields|slice:“0:5” and rendering fields manually

Using settings.XXX as defaults in model fields

April 10, 2023
Development, Django
Django, backend

When you have settings.XXXX defined in settings.py, and if you use it in a model e.g. country = CharField(max_length=3, default=settings.COUNTRY) .. makemigrations will generate alter-field migration always if you change COUNTRY. This can be problem if you have multiple instances of the app deployed with different COUNTRY setting. Better way is to use function to abstract away actual value: def get_system_country() -> str: return settings.COUNTRY ... country = CharField(max_length=3, default=get_system_country) This will generate only one migration even if you change the COUNTRY in settings. ...

Export list view in Django admin to Excel file

December 21, 2021
Development, Django
Django, backend, Export

The goal is whaterver data is changed in list view by (search, filter, sort ) and you can export result list in same view as you see and you don’t need to manually code every column and data in Excel file. Firstly, create share mixin to export to Excel file from jutil.openpyxl_helpers import rows_to_workbook from uuid import uuid1 from django.utils.text import capfirst class ExportExcelMixin: """ Export current result list to Excel file with data is same as list view. ...

Set redirect URL after updated Django model in admin

December 15, 2021
Development, Django
Django, backend

Currently if you do a filter or search in list view in admin in Django, after update a row in given list (Django will open edit page). After that model is updated, Django will redirect to list url without keep search or filter condition that you made before. Here is the solution to make Django redirect to previous list view url. Firstly, you need define own ChangeList. from jutil.admin import admin_obj_url from django. ...

Debugging Django emails

November 2, 2021
Development, Django
Django, backend

Run in terminal: python -m smtpd -n -c DebuggingServer localhost:1025 After that in Django’s settings.py: EMAIL_HOST = 'localhost' EMAIL_HOST_PASSWORD = '' EMAIL_HOST_USER = '' EMAIL_PORT = 1025 EMAIL_USE_SSL = False Then you get all sent email contents printed to terminal: