Author: Anonymous Language: python
Description: No description Timestamp: 2010-03-09 07:27:05 -0500
View raw paste Reply
  1. from mxutils.cms_services import generate_secid
  2. from django.db import models
  3. from django.contrib import admin
  4. from django import forms
  5.  
  6. class World(models.Model):
  7.     title = models.CharField(max_length=150)
  8.     secid = models.SlugField(max_length=1000, editable=False)
  9.     elements = models.ManyToManyField("Element", related_name='elements', blank=True, null=True)
  10.     metadata = models.OneToOneField("Category_metadata", blank=True, null=True)
  11.     def save(self):
  12.         if not self.pk:
  13.             super(World, self).save()
  14.             self.secid = generate_secid(self.title, self.pk, World.objects.all())
  15.         return super(World, self).save()
  16.     def __unicode__(self):
  17.         return "%s" % self.title
  18.  
  19. class Element(models.Model):
  20.     parent = models.ForeignKey(World, related_name='element_parent')
  21.     world = models.ForeignKey(World, related_name='world', blank=True, null=True)
  22.     item = models.ForeignKey("Item", blank=True, null=True)
  23.     value = models.DecimalField(default=0, max_digits=5, decimal_places=3)
  24.     def save(self):
  25.         if self.world and self.item:
  26.             return None
  27.         elif not self.world and not self.item:
  28.             return None
  29.         else:
  30.             return super(Element, self).save()
  31.     def __unicode__(self):
  32.         if self.world:
  33.             return "%s" % self.world.title
  34.         else:
  35.             return "%s" % self.item.title
  36.  
  37. class ElementInline(admin.TabularInline):
  38.     model = Element
  39.     extra=1
  40.  
  41. class WorldAdmin(admin.ModelAdmin):
  42.     inlines = [ElementInline,]
  43.     list_display = ('title',)
  44.     ordering = ['title']
  45.     search_fields = ('title',)
View raw paste Reply