django(7)数据库外键及mantyomany

[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'

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'

[root@localhost project0904]# python manage.py makemigrations
You are trying to add a non-nullable field 'teacher' to student without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
 2) Quit, and let me add a default in models.py
Select an option: 1\
Please select a valid option: 1
Please enter the default value now, as valid Python
The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now
Type 'exit' to exit this prompt
>>> 1
Migrations for 'app0904':
  app0904/migrations/0002_auto_20160905_0220.py:
    - Create model Teacher
    - Add field teacher to student
[root@localhost project0904]# python manage.py migrate
Operations to perform:
  Apply all migrations: admin, app0904, auth, contenttypes, sessions
Running migrations:
  Applying app0904.0002_auto_20160905_0220... OK



[root@localhost project0904]# python manage.py shell  
Python 2.7.5 (default, Jun 17 2014, 18:11:42) 
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from app0904.models import Student ,Teacher
>>> 
>>> Teacher.objects.create(id=1,name='teacherWang')
<Teacher: Teacher object>
>>> Teacher.objects.create(id=2,name='teacherLiu') 
<Teacher: Teacher object>
>>> student=Student.objects.get(id=1)
>>> student.teacher.name
u'teacherWang'
>>> teacher=Teacher.objects.get(id=1)
>>> student=teacher.student_set.get(id=1)
>>> student.name
u'wang1'

ManyToMany

[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'

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'


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'



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


[root@localhost project0904]# python manage.py shell
Python 2.7.5 (default, Jun 17 2014, 18:11:42) 
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> 
>>> from app0904.models import *
>>> studentList=Student.objects.filter(teacher_id=1)
>>> len(studentList)
2
>>> teacher=Teacher.objects.get(id=1)
>>> group=Group.objects.create(id=2,name='teacherWangs')
>>> MemberShip(group=group,student=studentList[0])
<MemberShip: MemberShip object>
>>> MemberShip(group=group,student=studentList[1])
<MemberShip: MemberShip object>

>>> group=Group.objects.get(id=2)
>>> group.name
u'teacherWangs'

>>> teacher.name
u'teacherWang'
>>> teacher.student_set.all()
<QuerySet [<Student: Student object>, <Student: Student object>]>
>>> len(teacher.student_set.all())
2
>>> group=Group.objects.get(id=2)
>>> studentList=group.members.all()
>>> groupList=student.group_set.all()
>>>

相关推荐