from django.db import models
class Publisher(models.Model):
name = models.CharField(max_length=32)
city = models.CharField(max_length=32)
def __str__(self):
return self.name
class Book(models.Model):
title = models.CharField(max_length=32)
publish_date = models.DateField(auto_now_add=True)
price = models.DecimalField(max_digits=5, decimal_places=2)
publisher = models.ForeignKey(to="Publisher")
def __str__(self):
return self.title
class Author(models.Model):
name = models.CharField(max_length=32)
age = models.IntegerField()
phone = models.IntegerField()
books = models.ManyToManyField(to="Book", related_name="authors")
detail = models.OneToOneField(to="AuthorDetail")
def __str__(self):
return self.name
class AuthorDetail(models.Model):
bobby = models.CharField(max_length=32)
addr = models.CharField(max_length=128)
import os
import sys
if __name__ == '__main__':
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ormday70.settings")
import django
django.setup()
from app01 import models
ret = models.Book.objects.filter(title__contains="沙河")
print(ret)
ret = models.Book.objects.filter(publish_date__year=2018)
print(ret)
ret = models.Book.objects.filter(price__gt=10)
print(ret)
ret = models.Publisher.objects.filter(city="沙河")
print(ret)
ret = models.Book.objects.all().values_list("publisher__name")
print(ret)
print(ret.distinct())
ret = models.Book.objects.all().order_by("price")
print(ret)
print(ret.reverse())
ret = models.Book.objects.all().order_by("-price")
print(ret)
ret = models.Book.objects.filter(title="沙河异闻录").values_list("publisher__city")
print(ret)
ret = models.Book.objects.filter(title="沙河异闻录").values("authors__detail__bobby")
print(ret)