aboutsummaryrefslogtreecommitdiffstats
path: root/examples/simpleexample/models.py
blob: 13867f611cb36b109939bc828d76c9230b0bd659 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from django.db import models

MAX_INSTANCES = 20

class MyModel(models.Model):
    foo = models.BooleanField()
    bar = models.IntegerField(help_text='Must be an integer.')
    baz = models.CharField(max_length=32, help_text='Free text.  Max length 32 chars.')
    created = models.DateTimeField(auto_now_add=True)
    
    class Meta:
        ordering = ('created',)

    def save(self, *args, **kwargs):
        """For the purposes of the sandbox, limit the maximum number of stored models."""
        while MyModel.objects.all().count() > MAX_INSTANCES:
            MyModel.objects.all()[0].delete()
        super(MyModel, self).save(*args, **kwargs)
    
    @models.permalink
    def get_absolute_url(self):
        return ('simpleexample.views.MyModelResource', (self.pk,))