Create a gist now

Instantly share code, notes, and snippets.

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], 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):
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