开发者

Django data model example for versioned collection of items

I'm looking for an example of a Django models.py file implementing the following use case:

  1. A page with a list of items can be created (a Collection)
  2. When the Collection gets reordered | is added to | is removed from and saved, the version number of the Collection is incremented by 1 (a Version)
  3. A user can go back to a previous version of this Collection, compare any two versions, and also create new Collections, which also need to be versioned

I'm mainly trying to get my head around the table relationships (do I need many-to-many in Items rather than ForeignKey), and how to automatically increment the version number.

Here's some code to start with:

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_numb开发者_开发问答er = "??? 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


Check out: http://stdbrouw.github.com/django-revisions/. It's one of the few apps for model versioning that's even somewhat actively developed. It also has a fairly simple API.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜