from django.http import HttpResponse from .models import Pulisher def index9(request): pulisher = Publisher.objects.get_or_create(name='深圳大學出版社') print(pulisher) # 查看publisher的類型 # print(type(pulisher)) <class 'tuple'> return HttpResponse("success")
再次刷新瀏覽器,查看輸出的結果爲:
python
def Publisher_Default(): return Publisher.objects.get_or_create(name='默認出版社') # 定義圖書模型 class Book(models.Model): name = models.CharField(max_length=100, unique=True) pages = models.IntegerField() price = models.FloatField() rating = models.FloatField() author = models.ForeignKey('Author', on_delete=models.CASCADE) publisher = models.ForeignKey('Publisher', on_delete=models.SET_DEFAULT, default=Publisher_Default)
def index(request): publisher =Publisher.objects.bulk_create([ Publisher(name='hello出版社'), Publisher(name='你好出版社') ]) return HttpResponse("success")