Category Archives: DJango

thread locals and user object in django

There is an interesting scenario regarding automation of routines related to model.

I want make my model in such a way that, everytime it is modified by some user, it should automatically set “modified by” field with current logged in user.
first step is to override “save” method in model, do update fields as you require and call super class save method.
but the question is, how model can access logged in user object, there is no request object wright?
there is a way with thread locals to deal with this kind of situation, click on following link
thread local and user objct in django models


DJango fixtures

One of the important feature from DJango is fixture generation and consumption.

Fixture is nothing but searialized data in file with proper file naming. Well, it by default recognizes some serialization formats. ex: xml, json etc. Usally file names goes like this: MyFixture.json.

DJango tries to learn data format from file extension.
Generating fixtures from DJango is pretty easy dumpdata

Above command results serialized data of all models of your all apps under project. Ofcourse, there are couple of options to fine tune your command to exclude some apps, to mention format of serialization etc. help dumpdata

issue above command to get full details of available options from your DJango project path.