1、新建organization app,Tools 工具栏运行 run manage.py Task,输入:
startapp organization
2、分析organization app,需要建立3个表如下。
3、编写organization /models.py
from datetime import datetime from django.db import models # Create your models here. class CityDict(models.Model): name = models.CharField(max_length=20, verbose_name=u'城市') add_time = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间') desc = models.CharField(max_length=20, verbose_name=u'城市描述') class Meta: verbose_name = u'城市' verbose_name_plural = verbose_name class CourseOrg(models.Model): city = models.ForeignKey(CityDict, verbose_name=u'所在城市') # 引入外建,完成1对多的关系 name = models.CharField(max_length=50, verbose_name=u'机构名称') desc = models.TextField(verbose_name=u'机构描述') click_nums = models.IntegerField(default=0, verbose_name=u'点击数') fav_nums = models.IntegerField(default=0, verbose_name=u'收藏人数') image = models.ImageField(upload_to='courses/%Y%m', max_length=100, verbose_name=u'封面图') address = models.CharField(max_length=150, default=u'', verbose_name=u'机构地址') add_time = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间') class Meta: verbose_name = u'课程机构' verbose_name_plural = verbose_name class Teacher(models.Model): org = models.ForeignKey(CourseOrg, verbose_name=u'所属机构') # 引入外建,完成1对多的关系 name = models.CharField(max_length=50, verbose_name=u'教师名') work_years = models.IntegerField(default=0, verbose_name=u'工作年限') work_company = models.CharField(max_length=50, verbose_name=u'就职公司') work_position = models.CharField(max_length=50, verbose_name=u'工作职位') points = models.CharField(max_length=50, verbose_name=u'教学特点') click_nums = models.IntegerField(default=0, verbose_name=u'点击数') fav_nums = models.IntegerField(default=0, verbose_name=u'收藏人数') add_time = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间') class Meta: verbose_name = u'教师' verbose_name_plural = verbose_name
4、在MxOnline/settings.py中注册app
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'users', # 注册App 'operation', 'courses ', 'organization ', ]
5、在数据库生成新定义的表,Tools 工具栏运行 run manage.py Task输入:
makemigrations migrate