django-admin.py startproject prj
cd prj
./manage.py migrate
./manage.py startapp shop
Добавим приложение в проект.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'shop'
]
Создадим модель (shop/models.py).
from django.db import models
class Category(models.Model):
name = models.CharField(max_length=250)
name_slug = models.CharField(max_length=250)
class Subcategory(models.Model):
name = models.CharField(max_length=250)
name_slug = models.CharField(max_length=250)
category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True)
class Good(models.Model):
name = models.CharField(max_length=250)
name_slug = models.CharField(max_length=250)
desc = models.TextField()
subcategory = models.ForeignKey(Subcategory, on_delete=models.SET_NULL, null=True)
class Image(models.Model):
good = models.ForeignKey(Good, on_delete=models.SET_NULL, null=True)
image = models.ImageField()
Опишем классы админ интерфейса и привяжем их к модели (shop/admin.py).
from django.contrib import admin
from .models import *
class CategoryAdmin(admin.ModelAdmin):
pass
class SubcategoryAdmin(admin.ModelAdmin):
pass
class GoodAdmin(admin.ModelAdmin):
pass
class ImageAdmin(admin.ModelAdmin):
pass
admin.site.register(Category, CategoryAdmin)
admin.site.register(Subcategory,SubcategoryAdmin)
admin.site.register(Good, GoodAdmin)
admin.site.register(Image, ImageAdmin)
Создадим новую команду импорта данных (shop/management/commands/import.py).
from django.core.management.base import BaseCommand, CommandError
from shop.models import *
class Command(BaseCommand):
def handle(self, *args, **options):
print('Importing data')
** Не забываем создавать файлы ini.py в новых каталогах management и commands **