我正在寻找实现以下用例的Django models.py文件的示例:
我主要试图了解表关系(我需要在Items而不是ForeignKey中使用多对多),以及如何自动增加版本号。
以下是一些代码:
class Collection(models.Model):
"""A collection of items"""
label = models.CharField(blank=True, max_length=50)
slug = models.SlugField()
class Meta:
verbose_name_plural = "Collections"
def __unicode__(self):
return self.label
class Item(models.Model):
"""An item"""
STATUS_CHOICES = (
(1, "Neutral"),
(2, "Flagged Up"),
(3, "Flagged Down"),
)
title = models.CharField(max_length=100)
slug = models.SlugField()
collection = models.ForeignKey(Collection)
owner = models.ForeignKey(User)
status = models.IntegerField(choices=STATUS_CHOICES, default=1)
created = models.DateTimeField(default=datetime.datetime.now)
modified = models.DateTimeField(default=datetime.datetime.now)
class Meta:
ordering = ['modified']
verbose_name_plural = "Items"
class Version(models.Model):
"""The version of a collection"""
collection = models.ForeignKey(Collection)
version_number = "??? how to auto increment, or do you I just use the primary key/auto field ???"
class Meta:
verbose_name_plural = "Versions"
def __unicode__(self):
return self.label
答案 0 :(得分:1)
结帐:http://stdbrouw.github.com/django-revisions/。它是为数不多的模型版本应用程序之一,甚至有些积极开发。它也有一个相当简单的API。