本文共 1346 字,大约阅读时间需要 4 分钟。
from django.db import models
class Book(models.Model):
nid = models.AutoField(primary_key=True)name = models.CharField(max_length=32)price = models.DecimalField(max_digits=5, decimal_places=2)publish_date = models.DateField()publish = models.ForeignKey(to='Publish',to_field='nid',on_delete=models.CASCADE)authors=models.ManyToManyField(to='Author')def __str__(self): return self.name
class Author(models.Model):
nid = models.AutoField(primary_key=True)name = models.CharField(max_length=32)age = models.IntegerField()author_detail = models.OneToOneField(to='AuthorDatail',to_field='nid',unique=True,on_delete=models.CASCADE)class AuthorDatail(models.Model):
nid = models.AutoField(primary_key=True)telephone = models.BigIntegerField()birthday = models.DateField()addr = models.CharField(max_length=64)class Publish(models.Model):
nid = models.AutoField(primary_key=True)name = models.CharField(max_length=32)city = models.CharField(max_length=32)email = models.EmailField()def str(self):return self.namedef test(self):return self.emailclass UserInfo(models.Model):
name = models.CharField(max_length=32)pwd = models.CharField(max_length=32)class UserToken(models.Model):
token = models.CharField(max_length=64)user = models.OneToOneField(to=UserInfo)转载于:https://blog.51cto.com/14127188/2337640