django(7) 데이터베이스 키 및 mantyomany
4741 단어 django
[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.objects.create(id=2,name='teacherLiu')
>>> 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(group=group,student=studentList[1])
>>> group=Group.objects.get(id=2)
>>> group.name
u'teacherWangs'
>>> teacher.name
u'teacherWang'
>>> teacher.student_set.all()
, ]>
>>> len(teacher.student_set.all())
2
>>> group=Group.objects.get(id=2)
>>> studentList=group.members.all()
>>> groupList=student.group_set.all()
>>>
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Django의 질문 및 답변 웹사이트환영 친구, 이것은 우리의 새로운 블로그입니다. 이 블로그에서는 , 과 같은 Question-n-Answer 웹사이트를 만들고 있습니다. 이 웹사이트는 회원가입 및 로그인이 가능합니다. 로그인 후 사용자는 사용자의 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.