November 2011

archive

remove domain dns key entry in known_hosts for ssh Nov
22
1
1

Sometimes I re-key or move the domain of a server and need to reset the key so RSA doesn't complain. Technically you can just open ~/.ssh/known_hosts file and remove the entry for the dns, but the names are hashed which makes the entry difficult to find.

I used to just reset the entire known_hosts file, but that's not necessary. You can remove the domain entry with the simple command:

ssh-keygen -R example.com

Next time you try to ssh you'll be prompted to add the key as if you're visiting the site the first time. I hope this helps.

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