Skip to content

Instantly share code, notes, and snippets.

@Brachamul
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Brachamul/2b4fb95438605d23b5a7 to your computer and use it in GitHub Desktop.
Save Brachamul/2b4fb95438605d23b5a7 to your computer and use it in GitHub Desktop.
Django test-app snippet
from django.contrib import admin
# Register your models here.
from .models import Item, Feature, RequiredMaterial, DevelopmentProject
class ItemAdmin(admin.ModelAdmin):
model = Item
admin.site.register(Item, ItemAdmin)
class FeatureAdmin(admin.ModelAdmin):
model = Feature
admin.site.register(Feature, FeatureAdmin)
class RequiredMaterialInline(admin.TabularInline):
model = RequiredMaterial
extra = 1
class DevelopmentProjectAdmin(admin.ModelAdmin):
inlines = (RequiredMaterialInline,)
list_display = ("development_project", "was", "becomes", "types_of_materials_needed")
admin.site.register(DevelopmentProject, DevelopmentProjectAdmin)
from django.db import models
from django.core.exceptions import ValidationError
# Create your models here.
class Item(models.Model):
name = models.CharField(max_length=255)
# illustration = models.ImageField(upload_to=)
def __str__(self):
return self.name
class Feature(models.Model):
# A feature is what occupies a slot, it can be a man-made constuct or natural terrain.
name = models.CharField(max_length=255)
description = models.TextField(max_length=255)
# illustration = models.ImageField(upload_to=)
def __str__(self):
return self.name
class DevelopmentProject(models.Model):
# Development projects can be applied to features in order to improve them.
was = models.ForeignKey(Feature, related_name='Project Source')
becomes = models.ForeignKey(Feature, related_name='Project Result')
materials_needed = models.ManyToManyField(Item, through='RequiredMaterial')
# illustration = models.ImageField(upload_to=)
def development_project(self):
return ("[%s] ⇒ [%s]" % (str(self.was), str(self.becomes)))
def types_of_materials_needed(self):
return ",\n".join([r.name for r in self.materials_needed.all()])
def validate_material_quantity(talue):
if value % 10 != 0:
raise ValidationError('Please input a multiple of 10.')
class RequiredMaterial(models.Model):
project = models.ForeignKey(DevelopmentProject)
item = models.ForeignKey(Item)
quantity = models.PositiveSmallIntegerField(default=0, validators=[validate_material_quantity])
def __str__(self):
quantity_of_items = str(self.item) + ', ' + str(self.quantity)
return quantity_of_items
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment