models.py
from django.db import models
class AuthorDetail(models.Model):
nid = models.AutoField(primary_key=True)
birthday = models.DateField()
telephone = models.BigIntegerField()
address = models.CharField(max_length=64)
class Meta(object):
db_table = 'authordetail'
class Author(models.Model):
nid = models.AutoField(primary_key=True)
name = models.CharField(max_length=32)
age = models.IntegerField()
authordetail = models.OneToOneField(to='AuthorDetail',to_field='nid',on_delete=models.CASCADE)
class Meta(object):
db_table = 'author'
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()
class Meta(object):
db_table = 'publish'
class Book(models.Model):
nid = models.AutoField(primary_key=True)
title = models.CharField(max_length=32)
publishDate = models.DateField()
price = models.DecimalField(max_digits=5,decimal_places=2)
publish = models.ForeignKey(to='Publish', to_field='nid',on_delete=models.CASCADE,null=True)
"""
create table book (
id INT primary key auto_increment,
title varchar(32),
price DECIMAL(8,2),
publishDate Date,
publish_id INT,
foregin key(publish_id) references publish(nid)
)
"""
authors = models.ManyToManyField(to='Author')
"""
mysql> desc book_authors;
+-----------+------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| book_id | int | NO | MUL | NULL | |
| author_id | int | NO | MUL | NULL | |
+-----------+------+------+-----+---------+----------------+
"""
class Meta(object):
db_table = 'book'
views.py
from django.shortcuts import render,HttpResponse
from .models import *
def index(request):
pass
"""
MariaDB [orm]> select * from book;
+-----+------------+-------------+--------+------------+
| nid | title | publishDate | price | publish_id |
+-----+------------+-------------+--------+------------+
| 1 | 红楼梦 | 2001-04-01 | 200.00 | 1 |
| 2 | 西游记 | 2001-04-01 | 200.00 | 1 |
| 3 | 水浒传 | 2001-04-01 | 200.00 | 1 |
| 4 | 红楼梦 | 2001-04-01 | 200.00 | 1 |
| 5 | 西游记 | 2001-04-01 | 200.00 | 1 |
| 6 | 水浒传 | 2001-04-01 | 200.00 | 1 |
| 7 | 金瓶梅 | 2001-04-01 | 200.00 | 1 |
| 8 | 金瓶梅1 | 2001-04-01 | 200.00 | 1 |
| 9 | 金瓶梅2 | 2001-04-01 | 200.00 | 1 |
+-----+------------+-------------+--------+------------+
"""
pass
"""
MariaDB [orm]> select * from author;
+-----+------+-----+-----------------+
| nid | name | age | authordetail_id |
+-----+------+-----+-----------------+
| 1 | alex | 33 | 1 |
| 2 | egon | 43 | 2 |
+-----+------+-----+-----------------+
"""
"""
MariaDB [orm]> select * from book_authors;
+----+---------+-----------+
| id | book_id | author_id |
+----+---------+-----------+
| 3 | 9 | 1 |
| 4 | 9 | 2 |
+----+---------+-----------+
"""
book = Book.objects.filter(nid=10).first()
print(book)
pass
"""
MariaDB [orm]> select * from book_authors;
Empty set (0.000 sec)
"""
pass
print(book.authors.all())
ret = book.authors.all().values('name')
print(ret)
return HttpResponse('OK')
|