# models.py


from django.db import models
from django.contrib.contenttypes.models import ContentType

""" Managers """
class LastIdentifierManager(models.Manager):     
    
    def get_new_id(self, obj):
        """
        Getting a new ID according to the content type.
        It retrieves the last saved value and adds 1.
        then, this new value is saved and returned.
        """
        if isinstance(obj, models.base.ModelBase):
            content_type = ContentType.objects.get_for_model(obj)
        else:
            content_type = obj

        lastidentifier, created = self.get_or_create(content_type=content_type)
        lastidentifier.value += 1
        lastidentifier.save()
        return lastidentifier.value

"""Model"""        
class LastIdentifier(models.Model):
    value = models.IntegerField('last ID', default=0)
    content_type = models.ForeignKey(ContentType, verbose_name='content type', unique=True)
   
    objects = LastIdentifierManager()

    class Meta:
        verbose_name = 'last ID'
        verbose_name_plural = 'last IDs'

    def __unicode__(self):
        return "%s - %s" % (self.value, self.content_type)




# views.py - sample view

from apps.foo.models import Foo
from apps.foo.models import LastIdentifier
from django.http import HttpResponse

    def identifier(request):
        ref = LastIdentifier.objects.get_new_id(Foo)
        return HttpResponse(ref)


