Created
February 22, 2013 23:56
-
-
Save jacobian/5017514 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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