Laur IVAN

DRF: What is ‘source’ in Fields?

Posted by in Software, Tutorial

For a couple of days I’ve been trying to de-serialise a JSON to a model. My problem is the model and the JSON have different fields, mainly to keep the model pythonic and independent of a specific JSON source. So… I’ve been trying to use the source parameter for each Field in my serialiser to map to the JSON. E.g. if my Django Model field would be named timestamp and my JSON would be named time, I would have a serialiser Field like this:

THIS IS WRONG! It’s actually…read more

Timestamp to DateTime Serializer Field for DRF

Posted by in Software, Tutorial

I’m currently trying to deserialize JSON data to a Django model. This is quite straight forward for most numeric fields, but I have a DateTime field which is stored as a timestamp in the original JSON. At first, I thought I should read the timestamp as an integer and convert it through post-processing. Then, after reading more of the DRF docs, it became evident I needed a custom field. The code is below:

The idea is that to_representation converts a DateTime field into a timestamp value for serialisation and…read more

Save Pandas DataFrame as Django Model

Posted by in Software, Tutorial

TL;DR: use bulk_upload coupled with a comprehension to speed up loading. Initially, I started to convert the data frame to a Model object row by row and save it. While easy, it’s also arguably the slowest method to load records, because the save() call performs a commit. Then I looked up how to save pandas data frames and I found out quite a few ways… Manual commit The first step would be to tweak the transaction to manually commit like described here:

SQL Other solution would be to use…read more

Load Templates with Pebble

Posted by in Software, Tutorial

Pebble is a templating engine. It looks particularly familiar to me because I have been using Django. You have access to blocks, include/import features, loops etc. This post is a simple exercise in loading some templates. Prerequisites I’ve been using Eclipse IDE, but you can use whatever you prefer. I’ve created a maven project and added pebble dependency:

The templates I’ve created 3 templates in the resources folder of the project: index.html

cell.html

Note how the {{item}} parameter is taken from the loop in *index.html. inner.html

read more

Change a django password manually

Posted by in Software

The other day I’ve been confronted with a strange situation: I forgot the admin password for a django application. I was playing with installing my own pypi repository, I’ve configured and deployed a test application and, after a couple of weeks, I forgot the password. I tried several times bu I knew that I used something silly and… unfortunate. Even worse, the user name I thought I used was not the one I had set up for the test. Oops :) After looking online for options I’ve found the following:…read more

Python classes to generate CSS

Posted by in Idea, Software

A while ago I got fed up with CSS and its cumbersomeness. As my poison of choice is python, I was thinking it would be quite useful to have a bunch of classes capable of generating CSS automatically in python. One could have a base class, a set of properties, a name-value dictionary and a way to nest them nicely so we could emulate LESS/SCSS. The idea is to use the power of python (or whatever language you’d want), with nesting etc. so we allow non-web designers mess around with…read more

Formless Step in FormWizard

Posted by in Software

Sometimes you’d need to show a purely informational page as part of a FormWizard. As far as I could see, there are two solutions to the problem: If the info is something like a summary, you have it available in the “success” page (the one after the wizard is finished) Alternatively, you can make it a page in the wizard. The more challenging and versatile variant is the second one (versatile because it gives you the possibility to go back to a previous step to correct stuff). My Solution My…read more

Create Data Once in a FormWizard

Posted by in Software

At one of the sites I’m building I need to walk users through a registration process: Select a supported tool Generate an ID Create a configuration based on the previous two steps To do this, I chose to use a FormWizard with three steps, one for each of the above. The problem then becomes to generate the ID once per wizard iteration. Initial Approach(es) Various online solutions (and even in the FormWizard documentation) suggest using get_form_kwargs() or get_form_initial() to build step-relevant data coupled with the get_cleaned_data_from_step(). The get_form_initial() method is…read more

Easy django applications with cookiecutter

Posted by in Software

Now I’m trying to create a bunch of django applications and I’ve got tired of creating everything from scratch. So, here are the steps I’m using to easy create a new django app: Create a new virtual environment. This is vital. It allows you to set up your own versions, not pollute the default installation (removing packages is tricky IMO). If you don’t know what I’m talking about, spend a few minutes to find out. Install cookiecutter (pip install cookiecutter). This is according to some the best thing that happened…read more

Make pyflakes/pylint ignore unused imports

Posted by in Software

Sometimes, you need to add an import without explicitly using it, just to have stuff defined. In such cases, both pyflakes and pylint generate messages, which can be annoying. For example django-appconf has a class-based configuration like so:

If I’m using the file like this, then I’ll end up with both pylint and pyflakes complaining that settings is unused, although it actually is (when the file gets imported and parsed). You can silence pylint by placing a comment on the import line (or in a pylintrc file to have…read more