public
Created

  • Download Gist
admin.py
Python
1 2 3 4 5 6 7 8 9 10 11
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)
models.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.