django(8):简单配置admin页

[root@localhost project0904]# vim app0904/admin.py

from django.contrib import admin
from app0904.models import Student,Teacher,Group
# Register your models here.

admin.site.register(Student)
admin.site.register(Teacher)
admin.site.register(Group)


[root@localhost project0904]# python manage.py makemigrations
No changes detected
[root@localhost project0904]# python manage.py migrate       
Operations to perform:
  Apply all migrations: admin, app0904, auth, contenttypes, sessions
Running migrations:
  No migrations to apply.
[root@localhost project0904]# vim app0904/models.py

from __future__ import unicode_literals

from django.db import models

# Create your models here.

class Teacher(models.Model):
  id=models.IntegerField(primary_key=True)
  name=models.CharField(max_length=50)
  class Meta:
    db_table='teacher'
  def __unicode__(self):
    return self.name

class Student(models.Model):
  id = models.IntegerField(primary_key=True)
  name = models.CharField(max_length=50)
  age=models.IntegerField()
  sex=models.IntegerField()
  teacher=models.ForeignKey(Teacher)
  class Meta:
    db_table='student'
  def __unicode__(self):
    return self.name


class Group(models.Model):
  id=models.IntegerField(primary_key=True)
  name=models.CharField(max_length=50)
  members=models.ManyToManyField(Student,through='MemberShip')
  class Meta:
    db_table='group'
  def __unicode__(self):
    return self.name



class MemberShip(models.Model):
  id=models.IntegerField(primary_key=True)
  group=models.ForeignKey(Group)
  student=models.ForeignKey(Student)
  class Meta:
    db_table='MemberShip'

打开后台可以看到加入的内容!

相关推荐