Login

Snippets by realmac

Snippet List

SelectRelatedManager

Because select_related() only works on ForeignKeys that are not null or blank, when you are customizing the admin, even if you set "list_select_related=True" you can still end up with way too many querys in the Admin changelist. By adding this code to your model you can decrease the queries dramatically. I found I needed this when I was working with Django apps with a lot of legacy data and I couldn't set the ForeignKey null=False.

  • admin
  • manager
  • select
  • queryset
Read More

Make anything into a template

This is a quick and dirty way to reuse the Django templating system for your own ends. Just pop in the familiar Django template syntax into whatever content you like and any chunk of content can be a template. This is great if you need to wrap data in HTML (as in for a mass email). The best part about this is that your "template" can now be stored in a database, instead of just in the file system.

  • template
  • templating
Read More

Age - custom filter

Based on the discussion at Empty Thoughts (http://blog.michaeltrier.com/2007/8/6/age-in-years-calculation) I built a quick and dirty custom filter. Save this as a file in your "templatetags" folder. I called mine "calculate_age.py" and then in your template "{% load calculate_age %}" then use it, "{{ object.birthdate|age }}"

  • filter
  • age
  • birthday
  • custom-tag
Read More

realmac has posted 3 snippets.