我有一个“Entry”模型,包含一个 GenericForeignKey:
class Entry(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
以及“Note”和“File”等不同的内容模型:
class Note(models.Model):
title = models.CharField(max_length=255)
text = models.TextField()
class File(models.Model):
title = models.CharField(max_length=255)
file_field = models.FileField('File', upload_to='files')
description = models.TextField(blank=True)
如果它们被存储,则会自动创建一个新的“条目”,因此我可以通过以下方式列出所有内容:
Entry.objects.all()
现在我正在寻找一种编写视图和模板的方法,它应该以任意顺序(例如按日期)列出所有条目及其详细信息。如何才能做到这一点?
是否可以将部分模板与特定模型相关联?模板如何知道如何在列表中显示特定实例?
整个而不是简化的 models.py 看起来像这样:
from django.db.models.signals import post_save, pre_delete
import datetime
class Entry(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
def title(self):
return self.content_object.title
def __unicode__(self):
return self.title()
class DateModel(models.Model):
""" A model for basic date awareness """
modified_date = models.DateTimeField(blank=True, null=True)
created_date = models.DateTimeField(blank=True)
class Meta:
abstract = True
ordering = ['-created_date']
def save(self, *args, **kwargs):
if not self.pk:
self.created_date = datetime.datetime.now()
else:
self.modified_date = datetime.datetime.now()
return super(DateModel, self).save(*args, **kwargs)
class Tag(DateModel):
name = models.CharField(max_length=51, unique=True)
def __unicode__(self):
return self.name
class Notebook(models.Model):
name = models.CharField(max_length=50, unique=True)
description = models.TextField(blank=True)
def __unicode__(self):
return self.name
class EntryModel(DateModel):
"""All Entry models are inherited from this abstract class"""
title = models.CharField(max_length=255)
notebook = models.ForeignKey(Notebook, blank=True, null=True)
tags = models.ManyToManyField(Tag, blank=True, null=True)
class Meta:
abstract = True
def __unicode__(self):
return self.title
######## Begin: Entry Classes ########
class Memo(EntryModel):
text = models.TextField()
def save(self, *args, **kwargs):
if self.title == '-' or self.title == '':
if len(self.text) > 20:
self.title = self.text[:17]+'...'
else:
self.title = self.text
return super(EntryModel, self).save(*args, **kwargs)
class File(EntryModel):
file_field = models.FileField('File', upload_to='files')
description = models.TextField(blank=True)
######## End: Entry Classes ########
def create_entry(sender, **kwargs):
"""Handler for post_save signal to create new Entry"""
if 'created' in kwargs:
if kwargs['created']:
instance = kwargs['instance']
ctype = ContentType.objects.get_for_model(instance)
entry = Entry.objects.get_or_create(
content_type=ctype,
object_id=instance.id)
def delete_entry(sender, **kwargs):
"""Handler for pre_delete signal to delete related Entry"""
instance = kwargs['instance']
ctype = ContentType.objects.get_for_model(instance)
entry = Entry.objects.get(
content_type=ctype,
object_id=instance.id)
entry.delete()
# Connect handler to post_save signal
entry_classes = EntryModel.__subclasses__()
for cls in entry_classes:
post_save.connect(create_entry, sender=cls)
pre_delete.connect(delete_entry, sender=cls)