Advertisement
black_eagle

models.py

Aug 4th, 2011
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | None | 0 0
  1. from django.db import models
  2.  
  3. # Create your models here.
  4.  
  5. class Category(models.Model):
  6.     parent = models.ForeignKey('self', null = True, blank = True)
  7.     slug = models.SlugField()
  8.     level = models.SmallIntegerField(editable = False)
  9.     order = models.SmallIntegerField(editable = False)
  10.  
  11.     def __unicode__(self):
  12.         return u"%s" % '-'*self.level+' '+self.slug
  13.  
  14.     def save(self):
  15.         if self.parent == None:
  16.             self.level = 0
  17.         else:
  18.             self.level = self.parent.level + 1
  19.  
  20.         self.order =  0
  21.  
  22.         super(Category, self).save()
  23.  
  24.     class Meta:
  25.         ordering = ('order', 'slug')
  26.    
  27.  
  28. class Tag(models.Model):
  29.     parent = models.ForeignKey(Category, null = True, blank = True)
  30.     slug = models.SlugField()
  31.  
  32.     def __unicode__(self):
  33.         return u"%s" % self.slug
  34.  
  35. class TestModel(models.Model):
  36.     slug = models.SlugField()
  37.     tags = models.ManyToManyField('Tag')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement