Merge pull request #30 from Hedde/master

Added support for generic inlines
master
Brandon Taylor 2013-03-15 04:23:06 -07:00
commit 893759f7d0
5 changed files with 38 additions and 3 deletions

View File

@ -1,6 +1,8 @@
import json import json
from django import VERSION as DJANGO_VERSION from django import VERSION as DJANGO_VERSION
from django.contrib.contenttypes.generic import GenericStackedInline, GenericTabularInline
DJANGO_MINOR_VERSION = DJANGO_VERSION[1] DJANGO_MINOR_VERSION = DJANGO_VERSION[1]
from django.conf import settings from django.conf import settings
@ -238,3 +240,13 @@ class SortableTabularInline(SortableInlineBase, TabularInline):
class SortableStackedInline(SortableInlineBase, StackedInline): class SortableStackedInline(SortableInlineBase, StackedInline):
"""Custom template that enables sorting for stacked inlines""" """Custom template that enables sorting for stacked inlines"""
template = 'adminsortable/edit_inline/stacked.html' template = 'adminsortable/edit_inline/stacked.html'
class SortableGenericTabularInline(SortableInlineBase, GenericTabularInline):
"""Custom template that enables sorting for tabular inlines"""
template = 'adminsortable/edit_inline/tabular.html'
class SortableGenericStackedInline(SortableInlineBase, GenericStackedInline):
"""Custom template that enables sorting for stacked inlines"""
template = 'adminsortable/edit_inline/stacked.html'

View File

@ -1,8 +1,8 @@
from django.contrib import admin from django.contrib import admin
from adminsortable.admin import (SortableAdmin, SortableTabularInline, from adminsortable.admin import (SortableAdmin, SortableTabularInline,
SortableStackedInline) SortableStackedInline, SortableGenericStackedInline)
from app.models import Category, Project, Credit, Note from app.models import Category, Project, Credit, Note, GenericNote
admin.site.register(Category, SortableAdmin) admin.site.register(Category, SortableAdmin)
@ -17,8 +17,13 @@ class NoteInline(SortableStackedInline):
extra = 0 extra = 0
class GenericNoteInline(SortableGenericStackedInline):
model = GenericNote
extra = 0
class ProjectAdmin(SortableAdmin): class ProjectAdmin(SortableAdmin):
inlines = [CreditInline, NoteInline] inlines = [CreditInline, NoteInline, GenericNoteInline]
list_display = ['__unicode__', 'category'] list_display = ['__unicode__', 'category']
admin.site.register(Project, ProjectAdmin) admin.site.register(Project, ProjectAdmin)

View File

@ -1,3 +1,5 @@
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType
from django.db import models from django.db import models
from adminsortable.fields import SortableForeignKey from adminsortable.fields import SortableForeignKey
@ -58,3 +60,16 @@ class Note(Sortable):
def __unicode__(self): def __unicode__(self):
return self.text return self.text
#a generic bound model
class GenericNote(SimpleModel, Sortable):
content_type = models.ForeignKey(ContentType, verbose_name=u"Content type", related_name="generic_notes")
object_id = models.PositiveIntegerField(u"Content id")
content_object = generic.GenericForeignKey(ct_field='content_type', fk_field='object_id')
class Meta(Sortable.Meta):
pass
def __unicode__(self):
return u"%s : %s" % (self.title, self.content_object)

View File

@ -2,6 +2,9 @@
import os import os
import sys import sys
# Adds the adminsortable package from the cloned repository instead of site_packages
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
if __name__ == "__main__": if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sample_project.settings") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sample_project.settings")