tag (admin)

archive

accessing the object inside a django admin change_form.html template Jul
06
1
2

When overriding the change_form.html template sometimes I need access to the object that is being edited. The change_view method provides the original object in the 'original' context variable.

You can access the object in the template using the original variable:

{{ original }}

comments

showing a foreign key value in a django admin list display Nov
07
1
1

say you have two models like so:

# models.py

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.ForeignKey("Author")

class Author(models.Model):
    name = models.CharField(max_length=100)

In this case it would be handy if we could list the author in the list_display of the book admin, but Django gives an error if we list foreign keys like so:

# admin.py

class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'author__name')

admin.register(Book, BookAdmin)

Luckily we can easily fix this by adjusting how we link into the author foreign key. We just need to add a method to the Book ModelAdmin and return the desired string.

# admin.py

class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'author_name')

    def author_name(self, instance):
        return instance.author.name

Be careful though as gratuitous additions of foreign key field can greatly increase the load time of the admin page for the list view of the model: use this judiciously.

comments

automatically dump and load data into django Mar
18
1
0

Django provides and easy way to backup and load data from your project regardless of which storage engine you're using through [dumpdata] and [loaddata]. If you don't provide the application name after the dumpdata command it'll output the data from all installed applications.

If you want the file to be formated for reading, use the [--indent] option.

manage.py dumpdata flatpages --indent=4 > flatpages.json

manage.py loaddata flatpages.json

You can name the dumpdata file [initial_data.json] if you want to automatically load the data file when you [syncdb] and [flush]. It's handy to use with the [--noinput] option to suppress user input if you have an initial_data.json set up.

Be very careful with initial_data.json though as you can unwillingly overwrite data, and you'll be a sad panda. I personally will only locally manage initial_data.json, and will keep it out of the repository at all times.

manage.py syncdb --noinput

manage.py flush --noinput

comments

delete old files in linux Mar
16
1
0

Edited: 4.15.2010 - fixed typo

You can use the [find] command in linux to remove old files through the command line. Pass in [-mmin +/-num] to specify minutes, and [-mtime +/-num] for days. Use [+] to get items older than the specified times, and [-] for younger.  Filter file/directory names through [-name], and run a command through [-exec].

# remove all files older than 7 days
find /path/to/directory* -mtime +7 -exec rm {} \;

# remove all *.py files younger than 30 minutes
find /path/to/directory* -name '*.py' -mmin -30 -exec rm {} \;

This is tested on Ubuntu.

comments