Skip to content

Instantly share code, notes, and snippets.

@jacobian
Created February 22, 2013 23:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacobian/5017514 to your computer and use it in GitHub Desktop.
Save jacobian/5017514 to your computer and use it in GitHub Desktop.
from django.contrib import admin
from .models import Author, Book
class BookInline(admin.TabularInline):
model = Book
readonly_fields = ['slug']
class AuthorAdmin(admin.ModelAdmin):
inlines = [BookInline]
admin.site.register(Author, AuthorAdmin)
from django.db import models
from django.template.defaultfilters import slugify
class Author(models.Model):
name = models.CharField(max_length=200)
def __unicode__(self):
return self.name
class Book(models.Model):
author = models.ForeignKey(Author)
slug = models.SlugField(primary_key=True)
title = models.CharField(max_length=200)
def __unicode__(self):
return self.title
def save(self, **kwargs):
if not self.slug:
self.slug = slugify(self.title)
super(Book, self).save(**kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment