2. Pizza Model - zdimon/pizza_store GitHub Wiki

Pizza model

from decimal import Decimal

class Pizza(models.Model):

	size = 50
	dough = 'thick'

	types = (
	    ('cheese', 'Cheese'),
	    ('chicken', 'Chicken'),
	    ('mushroom', 'Mushroom')
	)
	
	name = models.CharField(max_length=30)
	type = models.CharField(verbose_name='Пицца', max_length=15, choices=types, default='cheese')
	cost = models.DecimalField(max_digits=20, decimal_places=2, default=Decimal(0.00))

Command load pizza

from django.core.management.base import BaseCommand
from django.utils import timezone

from shop.models import Pizza

class Command(BaseCommand):

	def handle(self, *args, **kwargs):
	    print('Поехали')
	    for i in Pizza.types:
	        p = Pizza()
	        p.type = i[0]
	        p.name = '%s pizza!' % i[1]
	        p.cost = 10
	        p.save()
	        print('Creating %s' % i[0])
	    print('Done')

Admin

from django.contrib import admin

from .models import *
# Register your models here.

class PizzaAdmin(admin.ModelAdmin):
	pass

admin.site.register(Pizza, PizzaAdmin)

Shop class and context processor

class Shop(object):
	name = 'My Pizza shop!'


def shop_processor(request):
 shop = Shop()           
 return {'shop': shop}

TEMPLATES = [
	{
	    ...
	        'context_processors': [
	            ...
	            'shop.lib.shop_class.shop_processor'
	        ],
	    },
	},
]

Split models.py

mkdir models
touch models/__init__.py

models/order.py

from django.db import models

# Create your models here.
#from decimal import Decimal
from .pizza import Pizza

class Order(models.Model):

	pizza = models.ForeignKey(Pizza,on_delete=models.CASCADE)
	def __str__(self):
	    return '%s' % (pizza.name)	

models/pizza.py

from django.db import models

# Create your models here.
from decimal import Decimal

class Pizza(models.Model):

	types = (
	    ('cheese', 'Cheese'),
	    ('chicken', 'Chicken'),
	    ('mushroom', 'Mushroom')
	)
	
	name = models.CharField(max_length=30)
	type = models.CharField(verbose_name='Пицца', max_length=15, choices=types, default='cheese')
	cost = models.DecimalField(max_digits=20, decimal_places=2, default=Decimal(0.00))

	def __str__(self):
	    return '%s (%s)' % (self.name,self.type)

init.py

from .order import Order
from .pizza import Pizza

Visualization

pip install pyparsing pydot
./manage.py graph_models -a > my_project.dot
./manage.py graph_models -a -g -o my_project_visualized.png