Python and Django

29 Apr, 15

Saving file field uploads not in MEDIA_ROOT directory

I had a requirement recently to upload files to some kind of a private directory, not accessible via www. Of course, I used Django model's FileField to solve the task, but the problem was in a fact that by default, FileField saves uploads under MEDIA_ROOT which is not what I ...

Read now
13 Mar, 15

Snippet: Auto select current user in Django admin form

1
2
3
4
5
6
  1. # file: models.py
  2. class Post(models.Model):
  3. title = models.CharField(max_length=255)
  4. content = models.TextField()
  5. date = models.DateField()
  6. author = models.ForeignKey(settings.AUTH_USER_MODEL)
Read now
06 Mar, 15

Snippet: Override field widget in Django admin form

1
2
3
4
5
6
  1. # file: admin.py
  2. class BlogAdmin(admin.ModelAdmin):
  3. formfield_overrides = {
  4. models.TextField: {'widget': CKEditorWidget},
  5. }
Read now
18 Feb, 15

Snippet: Django post_save signal on parent class with multi-table inheritance

1
2
3
  1. # Propagate signal across all children of a polymorphic parent.
  2. for subclass in Entity.__subclasses__():
  3. post_save.connect(delete_comments, sender=subclass)
Read now
21 Oct, 14

Django Uwsgi Nginx 504 Gateway Timeout

I had a long running process (data import) and encountered 504 Gateway Timeout error. I use Nginx / uwsgi pair to serve my Django app. After doing some research it turned out that the problem is in uwsgi_read_timeout directive. Directive sets the amount of time for upstream to wait for a ...

Read now
09 Oct, 14

AngularJS plus Django: A match made in heaven

I've just finished watching "AngularJS plus Django: A match made in heaven" DjangoCon US 2014 talk by Nina Zakharenko (Video | Slides) and it was quite helpful. Since I'm working with API's and mobile / angular apps a lot, I just made some helpful Django + AngularJS related notes that came out of ...

Read now
19 Sep, 14

Improving Number of Queries Generated by Django Comment's render_comment_list Template Tag

One of my Django apps uses Django Comments. To show a list of comments on a page I use this simple built-in template tag:

1
  1. {% render_comment_list for object %}

All is fine, but I noticed that it loads user object per every comment line. So if you have 50 comments on ...

Read now
03 Sep, 14

Make Django and AngularJS play nicely together

Been working a lot with AngularJS and Django recently. To make them play nicely together one of the first things you need to do in your Angular App is to enable CSRF support and send X-Requested-With header, do it like this:

1
2
3
4
5
6
  1. var app = angular ...
Read now
07 Jun, 14

Stop PIP from re-downloading same packages over and over again

PIP has a habit of re-downloading same packages every time you hit pip install package_name. That's not so cool, especially, when you deploy packages from requirements.txt and one of the packages failed to build, restarting pip install -r requirements.txt would lead pip to redownload all the packages again ...

Read now
01 Jun, 14

Cleanup Files (and Images) On Model Delete in Django

In Django, if your model has Filefield / Imagefield, when it's get deleted it doesn't delete attached files by default. To fix that you need to do two steps: 1) Add filecleanup function, that'll take care of deleting files attached to a model:

 1
 2
 3
 4
 5
 6
 7
 8 ...
Read now