1

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_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

1 Answer 1

1

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.

1
  • django-revisions looks good, though the documentation is quite sparse. I'll try and get some code working and if successful return to accept this answer, along with a code sample. Cheers!
    – sgriffee
    Oct 4, 2011 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.