标签:def __name__ demo objects val mode art settings imp
import os
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "auth_demo.settings")
import django
django.setup()
from app01 import models
a1 = models.Article.objects.first() #为对象
print(type(a1))
a2 = models.Article.objects.all() #为querySet
print(type(a2))
for a3 in a2:
print(type(a3)) #单个对象
print(a3.user.avatar)
a4 = models.Article.objects.filter(pk=1) #querySet
print(type(a4))
a5 = a4.values("user__avatar") #querySet
print(a5, type(a5))
for i in a5:
print(i)
a6 = models.Article.objects.values("user__avatar") #querySet
#<QuerySet [{‘user__avatar‘: ‘avatars/95449993_9UqVsuI.jpg‘}, {‘user__avatar‘: ‘avatars/95449993_9UqVsuI.jpg‘}, {‘user__avatar‘: ‘avatars/95449993.jpg‘}]>
print("a6: %s" %(a6))
for i in a6:
print(i)
标签:def __name__ demo objects val mode art settings imp
原文地址:https://www.cnblogs.com/Liang-jc/p/9500701.html