Category: Programming

Django `CheckboxSelectMultiple` with `ModelMultipleChoiceField` generates too many queries (solution)

If you use  django.forms.ModelMultipleChoiceField with the default widget ( django.forms.widgets.SelectMultiple ), all’s good.

However, if you need to generate checkboxes for your choices, and use  django.forms.widgets.CheckboxSelectMultiple you’ll notice that the field will generate alot of queries (one for each element in the queryset), which is really really bad. I personally ended up with hundred of queries.

Fix: add  cache_choices=True to your  django.forms.ModelMultipleChoiceField

Example:

Good practice:

Bad practice:

django-logo-negative

Django get current user globally in the project

Sometimes, you will need to get the current authenticated user in models, or other parts of your app.

To achieve this, you must create this middleware and add it to MIDDLEWARE_CLASSES in project settings:

Usage example:

Invalidate template fragment cache in Django 1.5

Django 1.5 does not have a utility method to delete template fragment cache, with arguments.

In order to achieve this, you must create a custom method:

Example usage:

… and delete the cache:

 

In django 1.6+ you can do this more easy:

Manually generate Django password reset token

Ever needed to manually generate a password reset token?

For example, imagine this situation:
You create a user from a custom form, and you want the user to set his own password.
Solution: generate a password reset token,  email him the link, and let him choose his own password.

Here’s how you implement this:

To display the link in the email, you must set the password reset url, and display it:

 

Deploy with fabric on multiple servers

Ever needed to deploy with fabric on multiple machines?
Here’s a simple solution to aproach this issue.

In your fabfile.py , add these two methods:

Now, when deploying, for example:

you must use the following commands, in the correct order:

Fabric runs the “staging” or “deploy” commands first, setting the envrinoment variables, and then it runs the “deploy” command doing whatever you asked it to do.

jqPlot resize bar charts

To make a jqPlot bar chart resizeable, you need to do this:

where “chart” is the object returned by $.plot when initializing the plot.

Pip PIL error: Could not find any downloads that satisfy the requirement PIL

When installing “PIL” with pip, I got this error:

 Fix: use “Pillow” instead of “PIL”. Make sure you uninstall PIL first.

 

Compare integer variable in django templates

Ever needed to compare some variable (that may come from db, forms, etc) in django templates with an integer?
Example:

The above example will not work. Django’s template engine interprets the variable as a string.

Workaround for integer comparison:

By using the “add” filter, the variable is transformed into an integer.

Sublime text 3 sidebar collapse expand bug or folders missing (fix)

In the latest builds of Sublime Text 3 beta, the sidebar sometimes bugged itself. Either the folders are missing, or not expanding, or sometimes all the files are overlaped.

Workaround: In the menu, Click:
View -> Sidebar -> Show Open Files.
Restart Sublime.

Should work, but let’s home the team fies the bug in the following revisions.

Happy coding!