Roman's Blog

Random notes of a Python developer

Featured Image

Django And MySQL: Model Saving Order Is Important

 Nov. 19, 2021    0 comments 

Often in one Django view you need to save multiple models that have "parent-child" relationship. For example, your view may receive data from a traditional HTML form that includes an inline formset or a JSON with a nested array of child objects from a JavaScript frontend app. In this case you need to save a parent model and create/update/delete some child models. Of course, to make the whole operation atomic all model saves should be wrapped in a database transaction to ensure the consistency of saved data. Let's assume we are using MySQL or MariaDB as a database backend for our Django project. And the question arises: "in which order we should save our models?". "What should we save first: parent or children?" (...)

Read post

 DjangoMariaDBMySQLPython

Featured Image

Running Multiple Celery Beat Instances in One Python Project

 Feb. 1, 2021    0 comments 

In Python world Celery is a popular tool for running background tasks. It includes Celery beat that allows to run periodic tasks on some schedule. At my current work I participated in developing a system for running multiple periodic tasks. Since our technology stack is based on Python, Celery beat was a natural choice. One of the requirements to the system was granular control over individual tasks, that is, the ability to start/stop/restart each task individually. The straightforward solution was to run multiple Celery beat/worker pairs for each task, but after some googling it turned out that running multiple Celery beat instances seemed to be impossible. At least everybody said so, e.g. check this Stack Overflow discussion. If you try to do so you'll get duplicate tasks in your workers because each beat sends tasks to each worker. (...)

Read post

 CeleryPython

Featured Image

Interactive Dental Chart in HTML/JavaScript

 Jan. 17, 2021    0 comments 

One of my side-projects that I do on my free time is is a patient management system for my wife's dental clinic. It's written in Python/Django and it's a closed source project for now, but one thing I'd like to share is an interactive dental chart that allows to record the state of patient's teeth. Of course, the actual implementation is a bit more complex than this an it interacts with the Django back-end via Ajax requests but you can use this code as a basis for your own projects. Here's a link to JSFiddle: https://jsfiddle.net/romanvm/bpx8zc76/3/ with the code. Feel free to use this code as you see fit.

Read post

 CSSHTMLJavaScript

Featured Image

Formsets In Django Class-based Views

 Dec. 14, 2020    0 comments 

Django offers several out-of-the-box class-based views, including CreateView and UpdateView, that allow you to quckly implement basic CRUD functionality in your web-application without much of boilerplate code. Under the hood CreateView and UpdateView classes create, validate and save a Django ModelForm instance tied to your model class. But sometimes you need to create/edit related model instances along with the main model on the same page. (...)

Read post

 DjangoPython

Featured Image

Extracting Text from a PDF Using Python

 Jan. 6, 2019    0 comments 

Recently I needed to extract text from a PDF file using Python. Quick googling led me to PyPDF2 package, however I wasn't able to extract any text from my test PDF with it. The test PDF was created with Google Docs (a very common scenario) and did not have any fancy formatting, so PyPDF2 was disqualified for my purposes. After further googling I found pdfminer package and its Python 3 compatible version — pdfminer.six. (...)

Read post

 Python