Skip to content

Instantly share code, notes, and snippets.

@chrisjones-brack3t
Forked from kennethlove/gist:329475
Created March 11, 2010 18:27
Show Gist options
  • Save chrisjones-brack3t/329480 to your computer and use it in GitHub Desktop.
Save chrisjones-brack3t/329480 to your computer and use it in GitHub Desktop.
## common/models.py
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
class CommonFile(models.Model):
""" Model for common files. """
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
order = PositionField(unique_for_field='object_id')
title = models.CharField(max_length=255)
title.alphabetic_filter = True
description = models.TextField(null=True, blank=True)
upload = models.FileField(upload_to="uploads/files/")
def __unicode__(self):
return self.title
class Meta:
verbose_name = "File"
verbose_name_plural = "Files"
ordering = ['order',]
## common/admin.py
from django.contrib.contenttypes import generic
class FileInline(generic.GenericStackedInline):
model = CommonFile
allow_add = True
## other_app/admin.py
from common.admin import FileInline
class OtherModelAdmin(admin.ModelAdmin):
inlines = [FileInline,]
## other_app/models.py
from django.contrib.contenttypes import generic
from common.models import CommonFile
class OtherModel(models.Model):
files = generic.GenericRelation(CommonFile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment