1\u3001\u5b50\u67e5\u8be2\uff1a\u57fa\u4e8e\u5bf9\u8c61\u7684\u8de8\u8868\u67e5\u8be2 <\/h3>\n\n
\n <\/p>\n\ndef<\/span> query(request): <\/span>\"\"\"<\/span> \u8de8\u8868\u67e5\u8be2\uff1a 1.\u57fa\u4e8e\u5bf9\u8c61\u67e5\u8be2 2.\u57fa\u4e8e\u53cc\u4e0b\u5212\u7ebf\u67e5\u8be2 3.\u805a\u5408\u3001\u5206\u7ec4\u67e5\u8be2 4. F Q \u67e5\u8be2 <\/span>\"\"\"<\/span> #<\/span> ----\u57fa\u4e8e\u5bf9\u8c61\u7684\u8de8\u8868\u67e5\u8be2\uff08\u5b50\u67e5\u8be2\uff09<\/span> #<\/span> 1.\u4e00\u5bf9\u591a\u67e5\u8be2<\/span> \"\"\"<\/span> # \u4e00\u5bf9\u591a\u67e5\u8be2 \u6b63\u5411\u67e5\u8be2:\u6309\u5b57\u6bb5 \u53cd\u5411\u67e5\u8be2:\u8868\u540d\u5c0f\u5199_set.all() book_obj.publish Book(\u5173\u8054\u5c5e\u6027:publish)\u5bf9\u8c61 --------------> Publish\u5bf9\u8c61 <-------------- publish_obj.book_set.all() # queryset <\/span>\"\"\"<\/span> #<\/span> \u6b63\u5411\u67e5\u8be2\uff1a\u67e5\u8be2\u74f6\u6885\u8fd9\u672c\u4e66\u7684\u51fa\u7248\u793e\u7684\u540d\u5b57<\/span> book_obj = Book.objects.filter(title='<\/span>\u74f6\u6885<\/span>'<\/span>).first() publish_obj <\/span>= book_obj.publish #<\/span> \u4e0e\u8fd9\u672c\u4e66\u5173\u8054\u7684\u51fa\u7248\u793e\u5bf9\u8c61<\/span> publish_name = publish_obj.name <\/span>print<\/span>(publish_name) #<\/span> \u5357\u4eac\u51fa\u7248\u793e<\/span> #<\/span> \u53cd\u5411\u67e5\u8be2\uff1a\u67e5\u8be2\u4eba\u6c11\u51fa\u7248\u793e\u51fa\u7248\u7684\u4e66\u7c4d\u540d\u79f0<\/span> publish_obj = Publish.objects.filter(name='<\/span>\u4eba\u6c11\u51fa\u7248\u793e<\/span>'<\/span>).first() books_obj <\/span>= publish_obj.book_set.all() <\/span>print<\/span>(books_obj) #<\/span> <QuerySet [<Book: \u6c34\u6d52\u4f20>, <Book: \u897f\u6e38\u8bb0>, <Book: \u5927\u8bdd\u8bbe\u8ba1\u6a21\u5f0f>]><\/span> #<\/span> print(books_obj.title) #error 'QuerySet' object has no attribute 'title'<\/span> #<\/span> 2.\u591a\u5bf9\u591a<\/span> \"\"\"<\/span> # \u591a\u5bf9\u591a\u67e5\u8be2 \u6b63\u5411\u67e5\u8be2:\u6309\u5b57\u6bb5 \u53cd\u5411\u67e5\u8be2:\u8868\u540d\u5c0f\u5199_set.all() book_obj.authors.all() Book(\u5173\u8054\u5c5e\u6027:authors)\u5bf9\u8c61 ------------------------> Author\u5bf9\u8c61 <------------------------ author_obj.book_set.all() # queryset <\/span>\"\"\"<\/span> #<\/span> \u6b63\u5411\u67e5\u8be2\uff1a\u67e5\u8be2\u91d12\u74f6\u6885\u8fd9\u672c\u4e66\u7684\u6240\u6709\u4f5c\u8005<\/span> book_obj1 = Book.objects.filter(title='<\/span>\u91d12\u74f6\u6885<\/span>'<\/span>).first() book_obj2 <\/span>= Book.objects.filter(title='<\/span>\u91d12\u74f6\u6885<\/span>'<\/span>) <\/span>print<\/span>('<\/span>.first()<\/span>'<\/span>,book_obj1, type(book_obj1)) <\/span>print<\/span>('<\/span>\u65e0first()<\/span>'<\/span>,book_obj2,type(book_obj2)) authors_list <\/span>= book_obj.authors.all() <\/span>print<\/span>(authors_list) #<\/span> QuerySet\u5bf9\u8c61 <QuerySet [<Author: alex>, <Author: jack>]><\/span> for<\/span> author in<\/span> authors_list: <\/span>print<\/span>(author.name) <\/span>#<\/span> \u53cd\u5411\u67e5\u8be2\uff1a\u67e5\u8be2alex\u51fa\u7248\u8fc7\u7684\u6240\u6709\u4e66\u7c4d\u540d\u79f0<\/span> author_obj = Author.objects.filter(name='<\/span>alex<\/span>'<\/span>).first() books_list <\/span>= author_obj.book_set.all() <\/span>print<\/span>(books_list) <\/span>for<\/span> book in<\/span> books_list: <\/span>print<\/span>(book.title) <\/span>#<\/span> 3. \u4e00\u5bf9\u4e00<\/span> \"\"\"<\/span> # \u4e00\u5bf9\u4e00\u67e5\u8be2 \u6b63\u5411\u67e5\u8be2:\u6309\u5b57\u6bb5 \u53cd\u5411\u67e5\u8be2:\u8868\u540d\u5c0f\u5199 author.authordetail Author(\u5173\u8054\u5c5e\u6027:authordetail)\u5bf9\u8c61 ------------------------>AuthorDetail\u5bf9\u8c61 <------------------------ authordetail.author <\/span>\"\"\"<\/span> #<\/span> \u6b63\u5411\u67e5\u8be2\uff1a\u67e5\u8be2alex\u7684\u624b\u673a\u53f7<\/span> author_obj = Author.objects.filter(name='<\/span>alex<\/span>'<\/span>).first() phone <\/span>= author_obj.authordetail.telephone #<\/span> \u4e0eauthor\u5173\u8054\u7684authordetail\u5bf9\u8c61<\/span> print<\/span>(phone) <\/span>#<\/span> \u53cd\u5411\u67e5\u8be2\uff1a\u67e5\u8be2\u624b\u673a\u53f7\u4e3a110\u7684\u4f5c\u8005\u7684\u540d\u5b57\u548c\u5e74\u9f84<\/span> ad_obj = AuthorDetail.objects.filter(telephone=\"<\/span>110<\/span>\"<\/span>).first() name <\/span>= ad_obj.author.name #<\/span> \u5173\u8054\u7684\u5bf9\u8c61author<\/span> age = ad_obj.author.age <\/span>print<\/span>(name, age) <\/span>return<\/span> HttpResponse(\"<\/span>\u67e5\u8be2\u6210\u529f<\/span>\"<\/span>)<\/pre>\n<\/p><\/div>\n Views\u4ee3\u7801<\/span>\n <\/div>\n <\/p>\n
\u3000\u30001.\u7b80\u4ecb<\/h4>\n
\u3000\u3000\u6838\u5fc3\uff1abook.authors.all()<\/p>\n
\n\u5173\u952e\u70b9 \u4e00\u3001book_obj.publish <\/span>= Publish.objects.filter(id=book_obj.publish_id).first() \u4e8c\u3001book_obj.authors.all() \u5173\u952e\u70b9\uff1a book.authors.all() <\/span>#<\/span> \u4e0e\u8fd9\u672c\u4e66\u5173\u8054\u7684\u4f5c\u8005\u96c6\u5408<\/span> 1 book.id=3 2 book_authors id book_id author_id <\/span>3 3 1 4 3 2 3. author id name <\/span>1 alex <\/span>2 jack book_obj.authors.all() <\/span>-----> [alex,jack]<\/pre>\n<\/p><\/div>\n <\/p>\n
\u3000\u3000\u6b63\u5411\u67e5\u8be2 \u53cd\u5411\u67e5\u8be2<\/p>\n
\nA-B \u5173\u8054\u5c5e\u6027\u5728A\u8868\u4e2d \u6b63\u5411\u67e5\u8be2: A<\/span>------>B \u53cd\u5411\u67e5\u8be2: B<\/span>------>A<\/pre>\n<\/p><\/div>\n<\/p>\n
<\/p>\n
2\u3001\u4e00\u5bf9\u591a<\/span><\/h4>\n \u8bed\u6cd5<\/p>\n
\n #<\/span> \u4e00\u5bf9\u591a\u67e5\u8be2<\/span> \u6b63\u5411\u67e5\u8be2:\u6309\u5b57\u6bb5 \u53cd\u5411\u67e5\u8be2:\u8868\u540d\u5c0f\u5199_set.all() book_obj.publish Book(\u5173\u8054\u5c5e\u6027:publish)\u5bf9\u8c61 <\/span>--------------> Publish\u5bf9\u8c61 <\/span><-------------- publish_obj.book_set.all() <\/span>#<\/span> queryset<\/span><\/pre>\n<\/p><\/div>\n <\/p>\n
\u3000\u3000\u67e5\u8be2<\/p>\n
<\/p>\n
\n #<\/span> \u6b63\u5411\u67e5\u8be2\uff1a\u67e5\u8be2\u6885\u74f6jin\u8fd9\u672c\u4e66\u7684\u51fa\u7248\u793e\u7684\u540d\u5b57<\/span> book_obj = Book.objects.filter(title='<\/span>\u6885\u74f6jin<\/span>'<\/span>).first() publish_obj <\/span>= book_obj.publish #<\/span> \u4e0e\u8fd9\u672c\u4e66\u5173\u8054\u7684\u51fa\u7248\u793e\u5bf9\u8c61<\/span> publish_name = publish_obj.name <\/span>print<\/span>(publish_name) #<\/span> \u5357\u4eac\u51fa\u7248\u793e<\/span>
#<\/span> \u53cd\u5411\u67e5\u8be2\uff1a\u67e5\u8be2\u4eba\u6c11\u51fa\u7248\u793e\u51fa\u7248\u7684\u4e66\u7c4d\u540d\u79f0<\/span> publish_obj = Publish.objects.filter(name='<\/span>\u4eba\u6c11\u51fa\u7248\u793e<\/span>'<\/span>).first() books_obj <\/span>= publish_obj.book_set.all() <\/span>print<\/span>(books_obj) #<\/span> <QuerySet [<Book: \u6c34\u6d52\u4f20>, <Book: \u897f\u6e38\u8bb0>, <Book: \u5927\u8bdd\u8bbe\u8ba1\u6a21\u5f0f>]><\/span> #<\/span> print(books_obj.title) #error 'QuerySet' object has no attribute 'title'<\/span><\/pre>\n<\/p><\/div>\n <\/p>\n
<\/p>\n
<\/p>\n
3\u3001\u591a\u5bf9\u591a<\/h4>\n
\u89c4\u5219<\/p>\n
\n #<\/span> \u591a\u5bf9\u591a\u67e5\u8be2<\/span> \u6b63\u5411\u67e5\u8be2:\u6309\u5b57\u6bb5 \u53cd\u5411\u67e5\u8be2:\u8868\u540d\u5c0f\u5199_set.all() book_obj.authors.all() Book(\u5173\u8054\u5c5e\u6027:authors)\u5bf9\u8c61 <\/span>------------------------> Author\u5bf9\u8c61 <\/span><------------------------ author_obj.book_set.all() <\/span>#<\/span> queryset<\/span><\/pre>\n<\/p><\/div>\n <\/p>\n
<\/p>\n
<\/p>\n
<\/p>\n
<\/p>\n
\n #<\/span> \u6b63\u5411\u67e5\u8be2\uff1a\u67e5\u8be2\u6885\u74f6jin\u8fd9\u672c\u4e66\u7684\u6240\u6709\u4f5c\u8005<\/span> book_obj = Book.objects.filter(title='<\/span>\u6885\u74f6jin<\/span>'<\/span>).first() authors_list <\/span>= book_obj.authors.all() <\/span>print<\/span>(authors_list) #<\/span> QuerySet\u5bf9\u8c61 <QuerySet [<Author: alex>, <Author: jack>]><\/span> for<\/span> author in<\/span> authors_list: <\/span>print<\/span>(author.name)<\/pre>\n<\/p><\/div>\n <\/p>\n
<\/p>\n
<\/p>\n
\n #<\/span> \u53cd\u5411\u67e5\u8be2\uff1a\u67e5\u8be2alex\u51fa\u7248\u8fc7\u7684\u6240\u6709\u4e66\u7c4d\u540d\u79f0<\/span> author_obj = Author.objects.filter(name='<\/span>alex<\/span>'<\/span>).first() books_list <\/span>= author_obj.book_set.all() <\/span>print<\/span>(books_list) <\/span>for<\/span> book in<\/span> books_list: <\/span>print<\/span>(book.title)<\/pre>\n<\/p><\/div>\n <\/p>\n
<\/p>\n
<\/p>\n
4\u3001\u4e00\u5bf9\u4e00<\/h4>\n\n #<\/span> \u4e00\u5bf9\u4e00\u67e5\u8be2<\/span> \u6b63\u5411\u67e5\u8be2:\u6309\u5b57\u6bb5 \u53cd\u5411\u67e5\u8be2:\u8868\u540d\u5c0f\u5199 author.authordetail Author(\u5173\u8054\u5c5e\u6027:authordetail)\u5bf9\u8c61 <\/span>------------------------>AuthorDetail\u5bf9\u8c61 <\/span><------------------------ authordetail.author<\/span><\/pre>\n<\/p><\/div>\n <\/p>\n
<\/p>\n
\u67e5\u8be2<\/p>\n
\n #<\/span> \u6b63\u5411\u67e5\u8be2\uff1a\u67e5\u8be2alex\u7684\u624b\u673a\u53f7<\/span> author_obj = Author.objects.filter(name='<\/span>alex<\/span>'<\/span>).first() phone <\/span>= author_obj.authordetail.telephone #<\/span> \u4e0eauthor\u5173\u8054\u7684authordetail\u5bf9\u8c61<\/span> print<\/span>(phone)<\/pre>\n<\/p><\/div>\n <\/p>\n
<\/p>\n
\n #<\/span> \u53cd\u5411\u67e5\u8be2\uff1a\u67e5\u8be2\u624b\u673a\u53f7\u4e3a110\u7684\u4f5c\u8005\u7684\u540d\u5b57\u548c\u5e74\u9f84<\/span> ad_obj = AuthorDetail.objects.filter(telephone=\"<\/span>110<\/span>\"<\/span>).first() name <\/span>= ad_obj.author.name #<\/span> \u5173\u8054\u7684\u5bf9\u8c61author<\/span> age = ad_obj.author.age <\/span>print<\/span>(name, age)<\/pre>\n<\/p><\/div>\n<\/p>\n
<\/p>\n
<\/p>\n
5\u3001\u57fa\u4e8e\u5bf9\u8c61\u8de8\u8868\u67e5\u8be2\u7684sql\u8bed\u53e5\uff1asetting\u4e2dlog\u65e5\u5fd7<\/h4>\n
setting\u4e2d\u6dfb\u52a0logging\u65e5\u5fd7<\/p>\n
\nLOGGING = { <\/span>'<\/span>version<\/span>'<\/span>: 1, <\/span>'<\/span>disable_existing_loggers<\/span>'<\/span>: False, <\/span>'<\/span>
\n <\/p>\n
def<\/span> query(request): <\/span>\"\"\"<\/span> \u8de8\u8868\u67e5\u8be2\uff1a 1.\u57fa\u4e8e\u5bf9\u8c61\u67e5\u8be2 2.\u57fa\u4e8e\u53cc\u4e0b\u5212\u7ebf\u67e5\u8be2 3.\u805a\u5408\u3001\u5206\u7ec4\u67e5\u8be2 4. F Q \u67e5\u8be2 <\/span>\"\"\"<\/span> #<\/span> ----\u57fa\u4e8e\u5bf9\u8c61\u7684\u8de8\u8868\u67e5\u8be2\uff08\u5b50\u67e5\u8be2\uff09<\/span> #<\/span> 1.\u4e00\u5bf9\u591a\u67e5\u8be2<\/span> \"\"\"<\/span> # \u4e00\u5bf9\u591a\u67e5\u8be2 \u6b63\u5411\u67e5\u8be2:\u6309\u5b57\u6bb5 \u53cd\u5411\u67e5\u8be2:\u8868\u540d\u5c0f\u5199_set.all() book_obj.publish Book(\u5173\u8054\u5c5e\u6027:publish)\u5bf9\u8c61 --------------> Publish\u5bf9\u8c61 <-------------- publish_obj.book_set.all() # queryset <\/span>\"\"\"<\/span> #<\/span> \u6b63\u5411\u67e5\u8be2\uff1a\u67e5\u8be2\u74f6\u6885\u8fd9\u672c\u4e66\u7684\u51fa\u7248\u793e\u7684\u540d\u5b57<\/span> book_obj = Book.objects.filter(title='<\/span>\u74f6\u6885<\/span>'<\/span>).first() publish_obj <\/span>= book_obj.publish #<\/span> \u4e0e\u8fd9\u672c\u4e66\u5173\u8054\u7684\u51fa\u7248\u793e\u5bf9\u8c61<\/span> publish_name = publish_obj.name <\/span>print<\/span>(publish_name) #<\/span> \u5357\u4eac\u51fa\u7248\u793e<\/span> #<\/span> \u53cd\u5411\u67e5\u8be2\uff1a\u67e5\u8be2\u4eba\u6c11\u51fa\u7248\u793e\u51fa\u7248\u7684\u4e66\u7c4d\u540d\u79f0<\/span> publish_obj = Publish.objects.filter(name='<\/span>\u4eba\u6c11\u51fa\u7248\u793e<\/span>'<\/span>).first() books_obj <\/span>= publish_obj.book_set.all() <\/span>print<\/span>(books_obj) #<\/span> <QuerySet [<Book: \u6c34\u6d52\u4f20>, <Book: \u897f\u6e38\u8bb0>, <Book: \u5927\u8bdd\u8bbe\u8ba1\u6a21\u5f0f>]><\/span> #<\/span> print(books_obj.title) #error 'QuerySet' object has no attribute 'title'<\/span> #<\/span> 2.\u591a\u5bf9\u591a<\/span> \"\"\"<\/span> # \u591a\u5bf9\u591a\u67e5\u8be2 \u6b63\u5411\u67e5\u8be2:\u6309\u5b57\u6bb5 \u53cd\u5411\u67e5\u8be2:\u8868\u540d\u5c0f\u5199_set.all() book_obj.authors.all() Book(\u5173\u8054\u5c5e\u6027:authors)\u5bf9\u8c61 ------------------------> Author\u5bf9\u8c61 <------------------------ author_obj.book_set.all() # queryset <\/span>\"\"\"<\/span> #<\/span> \u6b63\u5411\u67e5\u8be2\uff1a\u67e5\u8be2\u91d12\u74f6\u6885\u8fd9\u672c\u4e66\u7684\u6240\u6709\u4f5c\u8005<\/span> book_obj1 = Book.objects.filter(title='<\/span>\u91d12\u74f6\u6885<\/span>'<\/span>).first() book_obj2 <\/span>= Book.objects.filter(title='<\/span>\u91d12\u74f6\u6885<\/span>'<\/span>) <\/span>print<\/span>('<\/span>.first()<\/span>'<\/span>,book_obj1, type(book_obj1)) <\/span>print<\/span>('<\/span>\u65e0first()<\/span>'<\/span>,book_obj2,type(book_obj2)) authors_list <\/span>= book_obj.authors.all() <\/span>print<\/span>(authors_list) #<\/span> QuerySet\u5bf9\u8c61 <QuerySet [<Author: alex>, <Author: jack>]><\/span> for<\/span> author in<\/span> authors_list: <\/span>print<\/span>(author.name) <\/span>#<\/span> \u53cd\u5411\u67e5\u8be2\uff1a\u67e5\u8be2alex\u51fa\u7248\u8fc7\u7684\u6240\u6709\u4e66\u7c4d\u540d\u79f0<\/span> author_obj = Author.objects.filter(name='<\/span>alex<\/span>'<\/span>).first() books_list <\/span>= author_obj.book_set.all() <\/span>print<\/span>(books_list) <\/span>for<\/span> book in<\/span> books_list: <\/span>print<\/span>(book.title) <\/span>#<\/span> 3. \u4e00\u5bf9\u4e00<\/span> \"\"\"<\/span> # \u4e00\u5bf9\u4e00\u67e5\u8be2 \u6b63\u5411\u67e5\u8be2:\u6309\u5b57\u6bb5 \u53cd\u5411\u67e5\u8be2:\u8868\u540d\u5c0f\u5199 author.authordetail Author(\u5173\u8054\u5c5e\u6027:authordetail)\u5bf9\u8c61 ------------------------>AuthorDetail\u5bf9\u8c61 <------------------------ authordetail.author <\/span>\"\"\"<\/span> #<\/span> \u6b63\u5411\u67e5\u8be2\uff1a\u67e5\u8be2alex\u7684\u624b\u673a\u53f7<\/span> author_obj = Author.objects.filter(name='<\/span>alex<\/span>'<\/span>).first() phone <\/span>= author_obj.authordetail.telephone #<\/span> \u4e0eauthor\u5173\u8054\u7684authordetail\u5bf9\u8c61<\/span> print<\/span>(phone) <\/span>#<\/span> \u53cd\u5411\u67e5\u8be2\uff1a\u67e5\u8be2\u624b\u673a\u53f7\u4e3a110\u7684\u4f5c\u8005\u7684\u540d\u5b57\u548c\u5e74\u9f84<\/span> ad_obj = AuthorDetail.objects.filter(telephone=\"<\/span>110<\/span>\"<\/span>).first() name <\/span>= ad_obj.author.name #<\/span> \u5173\u8054\u7684\u5bf9\u8c61author<\/span> age = ad_obj.author.age <\/span>print<\/span>(name, age) <\/span>return<\/span> HttpResponse(\"<\/span>\u67e5\u8be2\u6210\u529f<\/span>\"<\/span>)<\/pre>\n<\/p><\/div>\n Views\u4ee3\u7801<\/span>\n <\/div>\n <\/p>\n
\u3000\u30001.\u7b80\u4ecb<\/h4>\n
\u3000\u3000\u6838\u5fc3\uff1abook.authors.all()<\/p>\n
\n\u5173\u952e\u70b9 \u4e00\u3001book_obj.publish <\/span>= Publish.objects.filter(id=book_obj.publish_id).first() \u4e8c\u3001book_obj.authors.all() \u5173\u952e\u70b9\uff1a book.authors.all() <\/span>#<\/span> \u4e0e\u8fd9\u672c\u4e66\u5173\u8054\u7684\u4f5c\u8005\u96c6\u5408<\/span> 1 book.id=3 2 book_authors id book_id author_id <\/span>3 3 1 4 3 2 3. author id name <\/span>1 alex <\/span>2 jack book_obj.authors.all() <\/span>-----> [alex,jack]<\/pre>\n<\/p><\/div>\n <\/p>\n
\u3000\u3000\u6b63\u5411\u67e5\u8be2 \u53cd\u5411\u67e5\u8be2<\/p>\n
\nA-B \u5173\u8054\u5c5e\u6027\u5728A\u8868\u4e2d \u6b63\u5411\u67e5\u8be2: A<\/span>------>B \u53cd\u5411\u67e5\u8be2: B<\/span>------>A<\/pre>\n<\/p><\/div>\n<\/p>\n
<\/p>\n
2\u3001\u4e00\u5bf9\u591a<\/span><\/h4>\n \u8bed\u6cd5<\/p>\n
\n #<\/span> \u4e00\u5bf9\u591a\u67e5\u8be2<\/span> \u6b63\u5411\u67e5\u8be2:\u6309\u5b57\u6bb5 \u53cd\u5411\u67e5\u8be2:\u8868\u540d\u5c0f\u5199_set.all() book_obj.publish Book(\u5173\u8054\u5c5e\u6027:publish)\u5bf9\u8c61 <\/span>--------------> Publish\u5bf9\u8c61 <\/span><-------------- publish_obj.book_set.all() <\/span>#<\/span> queryset<\/span><\/pre>\n<\/p><\/div>\n <\/p>\n
\u3000\u3000\u67e5\u8be2<\/p>\n
<\/p>\n
\n #<\/span> \u6b63\u5411\u67e5\u8be2\uff1a\u67e5\u8be2\u6885\u74f6jin\u8fd9\u672c\u4e66\u7684\u51fa\u7248\u793e\u7684\u540d\u5b57<\/span> book_obj = Book.objects.filter(title='<\/span>\u6885\u74f6jin<\/span>'<\/span>).first() publish_obj <\/span>= book_obj.publish #<\/span> \u4e0e\u8fd9\u672c\u4e66\u5173\u8054\u7684\u51fa\u7248\u793e\u5bf9\u8c61<\/span> publish_name = publish_obj.name <\/span>print<\/span>(publish_name) #<\/span> \u5357\u4eac\u51fa\u7248\u793e<\/span>
#<\/span> \u53cd\u5411\u67e5\u8be2\uff1a\u67e5\u8be2\u4eba\u6c11\u51fa\u7248\u793e\u51fa\u7248\u7684\u4e66\u7c4d\u540d\u79f0<\/span> publish_obj = Publish.objects.filter(name='<\/span>\u4eba\u6c11\u51fa\u7248\u793e<\/span>'<\/span>).first() books_obj <\/span>= publish_obj.book_set.all() <\/span>print<\/span>(books_obj) #<\/span> <QuerySet [<Book: \u6c34\u6d52\u4f20>, <Book: \u897f\u6e38\u8bb0>, <Book: \u5927\u8bdd\u8bbe\u8ba1\u6a21\u5f0f>]><\/span> #<\/span> print(books_obj.title) #error 'QuerySet' object has no attribute 'title'<\/span><\/pre>\n<\/p><\/div>\n <\/p>\n
<\/p>\n
<\/p>\n
3\u3001\u591a\u5bf9\u591a<\/h4>\n
\u89c4\u5219<\/p>\n
\n #<\/span> \u591a\u5bf9\u591a\u67e5\u8be2<\/span> \u6b63\u5411\u67e5\u8be2:\u6309\u5b57\u6bb5 \u53cd\u5411\u67e5\u8be2:\u8868\u540d\u5c0f\u5199_set.all() book_obj.authors.all() Book(\u5173\u8054\u5c5e\u6027:authors)\u5bf9\u8c61 <\/span>------------------------> Author\u5bf9\u8c61 <\/span><------------------------ author_obj.book_set.all() <\/span>#<\/span> queryset<\/span><\/pre>\n<\/p><\/div>\n <\/p>\n
<\/p>\n
<\/p>\n
<\/p>\n
<\/p>\n
\n #<\/span> \u6b63\u5411\u67e5\u8be2\uff1a\u67e5\u8be2\u6885\u74f6jin\u8fd9\u672c\u4e66\u7684\u6240\u6709\u4f5c\u8005<\/span> book_obj = Book.objects.filter(title='<\/span>\u6885\u74f6jin<\/span>'<\/span>).first() authors_list <\/span>= book_obj.authors.all() <\/span>print<\/span>(authors_list) #<\/span> QuerySet\u5bf9\u8c61 <QuerySet [<Author: alex>, <Author: jack>]><\/span> for<\/span> author in<\/span> authors_list: <\/span>print<\/span>(author.name)<\/pre>\n<\/p><\/div>\n <\/p>\n
<\/p>\n
<\/p>\n
\n #<\/span> \u53cd\u5411\u67e5\u8be2\uff1a\u67e5\u8be2alex\u51fa\u7248\u8fc7\u7684\u6240\u6709\u4e66\u7c4d\u540d\u79f0<\/span> author_obj = Author.objects.filter(name='<\/span>alex<\/span>'<\/span>).first() books_list <\/span>= author_obj.book_set.all() <\/span>print<\/span>(books_list) <\/span>for<\/span> book in<\/span> books_list: <\/span>print<\/span>(book.title)<\/pre>\n<\/p><\/div>\n <\/p>\n
<\/p>\n
<\/p>\n
4\u3001\u4e00\u5bf9\u4e00<\/h4>\n\n #<\/span> \u4e00\u5bf9\u4e00\u67e5\u8be2<\/span> \u6b63\u5411\u67e5\u8be2:\u6309\u5b57\u6bb5 \u53cd\u5411\u67e5\u8be2:\u8868\u540d\u5c0f\u5199 author.authordetail Author(\u5173\u8054\u5c5e\u6027:authordetail)\u5bf9\u8c61 <\/span>------------------------>AuthorDetail\u5bf9\u8c61 <\/span><------------------------ authordetail.author<\/span><\/pre>\n<\/p><\/div>\n <\/p>\n
<\/p>\n
\u67e5\u8be2<\/p>\n
\n #<\/span> \u6b63\u5411\u67e5\u8be2\uff1a\u67e5\u8be2alex\u7684\u624b\u673a\u53f7<\/span> author_obj = Author.objects.filter(name='<\/span>alex<\/span>'<\/span>).first() phone <\/span>= author_obj.authordetail.telephone #<\/span> \u4e0eauthor\u5173\u8054\u7684authordetail\u5bf9\u8c61<\/span> print<\/span>(phone)<\/pre>\n<\/p><\/div>\n <\/p>\n
<\/p>\n
\n #<\/span> \u53cd\u5411\u67e5\u8be2\uff1a\u67e5\u8be2\u624b\u673a\u53f7\u4e3a110\u7684\u4f5c\u8005\u7684\u540d\u5b57\u548c\u5e74\u9f84<\/span> ad_obj = AuthorDetail.objects.filter(telephone=\"<\/span>110<\/span>\"<\/span>).first() name <\/span>= ad_obj.author.name #<\/span> \u5173\u8054\u7684\u5bf9\u8c61author<\/span> age = ad_obj.author.age <\/span>print<\/span>(name, age)<\/pre>\n<\/p><\/div>\n<\/p>\n
<\/p>\n
<\/p>\n
5\u3001\u57fa\u4e8e\u5bf9\u8c61\u8de8\u8868\u67e5\u8be2\u7684sql\u8bed\u53e5\uff1asetting\u4e2dlog\u65e5\u5fd7<\/h4>\n
setting\u4e2d\u6dfb\u52a0logging\u65e5\u5fd7<\/p>\n
\nLOGGING = { <\/span>'<\/span>version<\/span>'<\/span>: 1, <\/span>'<\/span>disable_existing_loggers<\/span>'<\/span>: False, <\/span>'<\/span>