{"id":116,"date":"2020-11-30T20:41:14","date_gmt":"2020-11-30T14:41:14","guid":{"rendered":"http:\/\/www.dreamflier.net\/wordpress\/?p=116"},"modified":"2021-01-14T19:51:21","modified_gmt":"2021-01-14T13:51:21","slug":"django%e7%9a%84%e6%95%b0%e6%8d%ae%e5%ba%93%e8%af%a6%e7%bb%86%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"http:\/\/www.jichangqing.net\/wordpress\/?p=116","title":{"rendered":"Django\u7684\u6570\u636e\u5e93\u8be6\u7ec6\u64cd\u4f5c"},"content":{"rendered":"<div class=\"article-header-box\" style=\"color: rgba(0, 0, 0, 0.75);\">\n<div class=\"article-header\">\n<div class=\"article-title-box\">\n<h1 class=\"title-article\" style=\"font-weight: 600; color: #222226;\">from: https:\/\/blog.csdn.net\/yanpenggong\/article\/details\/82316514<\/h1>\n<h1 id=\"articleContentId\" class=\"title-article\" style=\"font-weight: 600; color: #222226;\">ORM\u6846\u67b6<\/h1>\n<\/div>\n<\/div>\n<\/div>\n<article class=\"baidu_pl\" style=\"color: rgba(0, 0, 0, 0.75);\">\n<div id=\"article_content\" class=\"article_content clearfix\">\n<div id=\"content_views\" class=\"markdown_views\">\n<p style=\"color: #4d4d4d;\">O\u662fobject\uff0c\u4e5f\u5c31\u7c7b\u5bf9\u8c61\u7684\u610f\u601d\uff0cR\u662frelation\uff0c\u7ffb\u8bd1\u6210\u4e2d\u6587\u662f\u5173\u7cfb\uff0c\u4e5f\u5c31\u662f\u5173\u7cfb\u6570\u636e\u5e93\u4e2d\u6570\u636e\u8868\u7684\u610f\u601d\uff0cM\u662fmapping\uff0c\u662f\u6620\u5c04\u7684\u610f\u601d\u3002\u5728ORM\u6846\u67b6\u4e2d\uff0c\u5b83\u5e2e\u6211\u4eec\u628a\u7c7b\u548c\u6570\u636e\u8868\u8fdb\u884c\u4e86\u4e00\u4e2a\u6620\u5c04\uff0c\u53ef\u4ee5\u8ba9\u6211\u4eec\u901a\u8fc7\u7c7b\u548c\u7c7b\u5bf9\u8c61\u5c31\u80fd\u64cd\u4f5c\u5b83\u6240\u5bf9\u5e94\u7684\u8868\u683c\u4e2d\u7684\u6570\u636e\u3002ORM\u6846\u67b6\u8fd8\u6709\u4e00\u4e2a\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u6839\u636e\u6211\u4eec\u8bbe\u8ba1\u7684\u7c7b\u81ea\u52a8\u5e2e\u6211\u4eec\u751f\u6210\u6570\u636e\u5e93\u4e2d\u7684\u8868\u683c\uff0c\u7701\u53bb\u4e86\u6211\u4eec\u81ea\u5df1\u5efa\u8868\u7684\u8fc7\u7a0b\u3002<\/p>\n<p style=\"color: #4d4d4d;\">django\u4e2d\u5185\u5d4c\u4e86ORM\u6846\u67b6\uff0c\u4e0d\u9700\u8981\u76f4\u63a5\u9762\u5411\u6570\u636e\u5e93\u7f16\u7a0b\uff0c\u800c\u662f\u5b9a\u4e49\u6a21\u578b\u7c7b\uff0c\u901a\u8fc7\u6a21\u578b\u7c7b\u548c\u5bf9\u8c61\u5b8c\u6210\u6570\u636e\u8868\u7684\u589e\u5220\u6539\u67e5\u64cd\u4f5c\u3002<\/p>\n<p style=\"color: #4d4d4d;\">\u4f7f\u7528django\u8fdb\u884c\u6570\u636e\u5e93\u5f00\u53d1\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<br \/>\n1. \u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f<br \/>\n2. \u5728models.py\u4e2d\u5b9a\u4e49\u6a21\u578b\u7c7b<br \/>\n3. \u8fc1\u79fb<br \/>\n4. \u901a\u8fc7\u7c7b\u548c\u5bf9\u8c61\u5b8c\u6210\u6570\u636e\u589e\u5220\u6539\u67e5\u64cd\u4f5c<\/p>\n<h2 id=\"\u4e00\u914d\u7f6e\" style=\"font-weight: bold; color: #4f4f4f;\"><a style=\"font-weight: normal; color: #4ea1db;\" name=\"t1\"><\/a><a style=\"font-weight: normal; color: #4ea1db;\" name=\"t1\"><\/a>\u4e00\u3001\u914d\u7f6e<\/h2>\n<p style=\"color: #4d4d4d;\">\u5728settings.py\u4e2d\u4fdd\u5b58\u4e86\u6570\u636e\u5e93\u7684\u8fde\u63a5\u914d\u7f6e\u4fe1\u606f\uff0cDjango\u9ed8\u8ba4\u521d\u59cb\u914d\u7f6e\u4f7f\u7528sqlite\u6570\u636e\u5e93\u3002<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs cs has-numbering\" style=\"color: #4f4f4f !important;\">DATABASES = {\r\n    <span class=\"hljs-string\" style=\"color: #009900 !important;\">'default'<\/span>: {\r\n        <span class=\"hljs-string\" style=\"color: #009900 !important;\">'ENGINE'<\/span>: <span class=\"hljs-string\" style=\"color: #009900 !important;\">'django.db.backends.sqlite3'<\/span>,\r\n        <span class=\"hljs-string\" style=\"color: #009900 !important;\">'NAME'<\/span>: os.path.<span class=\"hljs-keyword\" style=\"color: #000088 !important;\">join<\/span>(BASE_DIR, <span class=\"hljs-string\" style=\"color: #009900 !important;\">'db.sqlite3'<\/span>),\r\n    }\r\n}<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<li style=\"color: #999999;\">3<\/li>\n<li style=\"color: #999999;\">4<\/li>\n<li style=\"color: #999999;\">5<\/li>\n<li style=\"color: #999999;\">6<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\n1. \u4f7f\u7528MySQL\u6570\u636e\u5e93\u9996\u5148\u9700\u8981\u5b89\u88c5\u9a71\u52a8\u7a0b\u5e8f<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs cmake has-numbering\" style=\"color: #4f4f4f !important;\">pip <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">install<\/span> PyMySQL<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\n2. \u5728Django\u7684\u5de5\u7a0b\u540c\u540d\u5b50\u76ee\u5f55\u7684<span style=\"font-weight: bold;\">init<\/span>.py\u6587\u4ef6\u4e2d\u6dfb\u52a0\u5982\u4e0b\u8bed\u53e5<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs python has-numbering\" style=\"color: #4f4f4f !important;\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">from<\/span> pymysql <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">import<\/span> install_as_MySQLdb\r\ninstall_as_MySQLdb()<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\u4f5c\u7528\u662f\u8ba9Django\u7684ORM\u80fd\u4ee5mysqldb\u7684\u65b9\u5f0f\u6765\u8c03\u7528PyMySQL\u3002<br \/>\n3. \u4fee\u6539DATABASES\u914d\u7f6e\u4fe1\u606f<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs bash has-numbering\" style=\"color: #4f4f4f !important;\">DATABASES = {\r\n    <span class=\"hljs-string\" style=\"color: #009900 !important;\">'default'<\/span>: {\r\n        <span class=\"hljs-string\" style=\"color: #009900 !important;\">'ENGINE'<\/span>: <span class=\"hljs-string\" style=\"color: #009900 !important;\">'django.db.backends.mysql'<\/span>,\r\n        <span class=\"hljs-string\" style=\"color: #009900 !important;\">'HOST'<\/span>: <span class=\"hljs-string\" style=\"color: #009900 !important;\">'127.0.0.1'<\/span>,  <span class=\"hljs-comment\" style=\"color: #880000 !important;\"># \u6570\u636e\u5e93\u4e3b\u673a<\/span>\r\n        <span class=\"hljs-string\" style=\"color: #009900 !important;\">'PORT'<\/span>: <span class=\"hljs-number\" style=\"color: #006666 !important;\">3306<\/span>,  <span class=\"hljs-comment\" style=\"color: #880000 !important;\"># \u6570\u636e\u5e93\u7aef\u53e3<\/span>\r\n        <span class=\"hljs-string\" style=\"color: #009900 !important;\">'USER'<\/span>: <span class=\"hljs-string\" style=\"color: #009900 !important;\">'root'<\/span>,  <span class=\"hljs-comment\" style=\"color: #880000 !important;\"># \u6570\u636e\u5e93\u7528\u6237\u540d<\/span>\r\n        <span class=\"hljs-string\" style=\"color: #009900 !important;\">'PASSWORD'<\/span>: <span class=\"hljs-string\" style=\"color: #009900 !important;\">'mysql'<\/span>,  <span class=\"hljs-comment\" style=\"color: #880000 !important;\"># \u6570\u636e\u5e93\u7528\u6237\u5bc6\u7801<\/span>\r\n        <span class=\"hljs-string\" style=\"color: #009900 !important;\">'NAME'<\/span>: <span class=\"hljs-string\" style=\"color: #009900 !important;\">'django_demo'<\/span>  <span class=\"hljs-comment\" style=\"color: #880000 !important;\"># \u6570\u636e\u5e93\u540d\u5b57<\/span>\r\n    }\r\n}<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<li style=\"color: #999999;\">3<\/li>\n<li style=\"color: #999999;\">4<\/li>\n<li style=\"color: #999999;\">5<\/li>\n<li style=\"color: #999999;\">6<\/li>\n<li style=\"color: #999999;\">7<\/li>\n<li style=\"color: #999999;\">8<\/li>\n<li style=\"color: #999999;\">9<\/li>\n<li style=\"color: #999999;\">10<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\n4. \u5728MySQL\u4e2d\u521b\u5efa\u6570\u636e\u5e93<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs sql has-numbering\" style=\"color: #4f4f4f !important;\"><span class=\"hljs-operator\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">create<\/span> <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">database<\/span> django_demo <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">default<\/span> charset=utf8;<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\n<h2 id=\"\u4e8c\u5b9a\u4e49\u6a21\u578b\u7c7b\" style=\"font-weight: bold; color: #4f4f4f;\"><a style=\"font-weight: normal; color: #4ea1db;\" name=\"t2\"><\/a><a style=\"font-weight: normal; color: #4ea1db;\" name=\"t2\"><\/a>\u4e8c\u3001\u5b9a\u4e49\u6a21\u578b\u7c7b<\/h2>\n<ul>\n<li>\u6a21\u578b\u7c7b\u88ab\u5b9a\u4e49\u5728\u201d\u5e94\u7528\/models.py\u201d\u6587\u4ef6\u4e2d\u3002<\/li>\n<li>\u6a21\u578b\u7c7b\u5fc5\u987b\u7ee7\u627f\u81eaModel\u7c7b\uff0c\u4f4d\u4e8e\u5305django.db.models\u4e2d\u3002<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\u63a5\u4e0b\u6765\u9996\u5148\u4ee5\u201d\u56fe\u4e66-\u82f1\u96c4\u201d\u7ba1\u7406\u4e3a\u4f8b\u8fdb\u884c\u6f14\u793a\u3002<\/p>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">1. \u5b9a\u4e49<\/span><\/p>\n<p style=\"color: #4d4d4d;\">\u521b\u5efa\u5e94\u7528booktest\uff0c\u5728models.py \u6587\u4ef6\u4e2d\u5b9a\u4e49\u6a21\u578b\u7c7b\u3002<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs python has-numbering\" style=\"color: #4f4f4f !important;\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">from<\/span> django.db <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">import<\/span> models\r\n\r\n<span class=\"hljs-comment\" style=\"color: #880000 !important;\">#\u5b9a\u4e49\u56fe\u4e66\u6a21\u578b\u7c7bBookInfo<\/span>\r\n<span class=\"hljs-class\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">class<\/span> <span class=\"hljs-title\">BookInfo<\/span><span class=\"hljs-params\">(models.Model)<\/span>:<\/span>\r\n    btitle = models.CharField(max_length=<span class=\"hljs-number\" style=\"color: #006666 !important;\">20<\/span>, verbose_name=<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u540d\u79f0'<\/span>)\r\n    bpub_date = models.DateField(verbose_name=<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u53d1\u5e03\u65e5\u671f'<\/span>)\r\n    bread = models.IntegerField(default=<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>, verbose_name=<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u9605\u8bfb\u91cf'<\/span>)\r\n    bcomment = models.IntegerField(default=<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>, verbose_name=<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u8bc4\u8bba\u91cf'<\/span>)\r\n    is_delete = models.BooleanField(default=<span class=\"hljs-keyword\" style=\"color: #000088 !important;\">False<\/span>, verbose_name=<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u903b\u8f91\u5220\u9664'<\/span>)\r\n\r\n    <span class=\"hljs-class\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">class<\/span> <span class=\"hljs-title\">Meta<\/span>:<\/span>\r\n        db_table = <span class=\"hljs-string\" style=\"color: #009900 !important;\">'tb_books'<\/span>  <span class=\"hljs-comment\" style=\"color: #880000 !important;\"># \u6307\u660e\u6570\u636e\u5e93\u8868\u540d<\/span>\r\n        verbose_name = <span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u56fe\u4e66'<\/span>  <span class=\"hljs-comment\" style=\"color: #880000 !important;\"># \u5728admin\u7ad9\u70b9\u4e2d\u663e\u793a\u7684\u540d\u79f0<\/span>\r\n        verbose_name_plural = verbose_name  <span class=\"hljs-comment\" style=\"color: #880000 !important;\"># \u663e\u793a\u7684\u590d\u6570\u540d\u79f0<\/span>\r\n\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">def<\/span> <span class=\"hljs-title\" style=\"color: #009900 !important;\">__str__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\r\n        <span class=\"hljs-string\" style=\"color: #009900 !important;\">\"\"\"\u5b9a\u4e49\u6bcf\u4e2a\u6570\u636e\u5bf9\u8c61\u7684\u663e\u793a\u4fe1\u606f\"\"\"<\/span>\r\n        <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">return<\/span> self.btitle\r\n\r\n<span class=\"hljs-comment\" style=\"color: #880000 !important;\">#\u5b9a\u4e49\u82f1\u96c4\u6a21\u578b\u7c7bHeroInfo<\/span>\r\n<span class=\"hljs-class\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">class<\/span> <span class=\"hljs-title\">HeroInfo<\/span><span class=\"hljs-params\">(models.Model)<\/span>:<\/span>\r\n    GENDER_CHOICES = (\r\n        (<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>, <span class=\"hljs-string\" style=\"color: #009900 !important;\">'male'<\/span>),\r\n        (<span class=\"hljs-number\" style=\"color: #006666 !important;\">1<\/span>, <span class=\"hljs-string\" style=\"color: #009900 !important;\">'female'<\/span>)\r\n    )\r\n    hname = models.CharField(max_length=<span class=\"hljs-number\" style=\"color: #006666 !important;\">20<\/span>, verbose_name=<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u540d\u79f0'<\/span>) \r\n    hgender = models.SmallIntegerField(choices=GENDER_CHOICES, default=<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>, verbose_name=<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u6027\u522b'<\/span>)  \r\n    hcomment = models.CharField(max_length=<span class=\"hljs-number\" style=\"color: #006666 !important;\">200<\/span>, null=<span class=\"hljs-keyword\" style=\"color: #000088 !important;\">True<\/span>, verbose_name=<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u63cf\u8ff0\u4fe1\u606f'<\/span>) \r\n    hbook = models.ForeignKey(BookInfo, on_delete=models.CASCADE, verbose_name=<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u56fe\u4e66'<\/span>)  <span class=\"hljs-comment\" style=\"color: #880000 !important;\"># \u5916\u952e<\/span>\r\n    is_delete = models.BooleanField(default=<span class=\"hljs-keyword\" style=\"color: #000088 !important;\">False<\/span>, verbose_name=<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u903b\u8f91\u5220\u9664'<\/span>)\r\n\r\n    <span class=\"hljs-class\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">class<\/span> <span class=\"hljs-title\">Meta<\/span>:<\/span>\r\n        db_table = <span class=\"hljs-string\" style=\"color: #009900 !important;\">'tb_heros'<\/span>\r\n        verbose_name = <span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u82f1\u96c4'<\/span>\r\n        verbose_name_plural = verbose_name\r\n\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">def<\/span> <span class=\"hljs-title\" style=\"color: #009900 !important;\">__str__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\r\n        <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">return<\/span> self.hname<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<li style=\"color: #999999;\">3<\/li>\n<li style=\"color: #999999;\">4<\/li>\n<li style=\"color: #999999;\">5<\/li>\n<li style=\"color: #999999;\">6<\/li>\n<li style=\"color: #999999;\">7<\/li>\n<li style=\"color: #999999;\">8<\/li>\n<li style=\"color: #999999;\">9<\/li>\n<li style=\"color: #999999;\">10<\/li>\n<li style=\"color: #999999;\">11<\/li>\n<li style=\"color: #999999;\">12<\/li>\n<li style=\"color: #999999;\">13<\/li>\n<li style=\"color: #999999;\">14<\/li>\n<li style=\"color: #999999;\">15<\/li>\n<li style=\"color: #999999;\">16<\/li>\n<li style=\"color: #999999;\">17<\/li>\n<li style=\"color: #999999;\">18<\/li>\n<li style=\"color: #999999;\">19<\/li>\n<li style=\"color: #999999;\">20<\/li>\n<li style=\"color: #999999;\">21<\/li>\n<li style=\"color: #999999;\">22<\/li>\n<li style=\"color: #999999;\">23<\/li>\n<li style=\"color: #999999;\">24<\/li>\n<li style=\"color: #999999;\">25<\/li>\n<li style=\"color: #999999;\">26<\/li>\n<li style=\"color: #999999;\">27<\/li>\n<li style=\"color: #999999;\">28<\/li>\n<li style=\"color: #999999;\">29<\/li>\n<li style=\"color: #999999;\">30<\/li>\n<li style=\"color: #999999;\">31<\/li>\n<li style=\"color: #999999;\">32<\/li>\n<li style=\"color: #999999;\">33<\/li>\n<li style=\"color: #999999;\">34<\/li>\n<li style=\"color: #999999;\">35<\/li>\n<li style=\"color: #999999;\">36<\/li>\n<li style=\"color: #999999;\">37<\/li>\n<li style=\"color: #999999;\">38<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">1.1 \u6570\u636e\u5e93\u8868\u540d<\/span><\/p>\n<p style=\"color: #4d4d4d;\">\u6a21\u578b\u7c7b\u5982\u679c\u672a\u6307\u660e\u8868\u540d\uff0cDjango\u9ed8\u8ba4\u4ee5 \u5c0f\u5199app\u5e94\u7528\u540d_\u5c0f\u5199\u6a21\u578b\u7c7b\u540d \u4e3a\u6570\u636e\u5e93\u8868\u540d\u3002<\/p>\n<p style=\"color: #4d4d4d;\">\u53ef\u901a\u8fc7db_table \u6307\u660e\u6570\u636e\u5e93\u8868\u540d\u3002<\/p>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">1.2 \u5173\u4e8e\u4e3b\u952e<\/span><\/p>\n<p style=\"color: #4d4d4d;\">django\u4f1a\u4e3a\u8868\u521b\u5efa\u81ea\u52a8\u589e\u957f\u7684\u4e3b\u952e\u5217\uff0c\u6bcf\u4e2a\u6a21\u578b\u53ea\u80fd\u6709\u4e00\u4e2a\u4e3b\u952e\u5217\uff0c\u5982\u679c\u4f7f\u7528\u9009\u9879\u8bbe\u7f6e\u67d0\u5c5e\u6027\u4e3a\u4e3b\u952e\u5217\u540edjango\u4e0d\u4f1a\u518d\u521b\u5efa\u81ea\u52a8\u589e\u957f\u7684\u4e3b\u952e\u5217\u3002<\/p>\n<p style=\"color: #4d4d4d;\">\u9ed8\u8ba4\u521b\u5efa\u7684\u4e3b\u952e\u5217\u5c5e\u6027\u4e3aid\uff0c\u53ef\u4ee5\u4f7f\u7528pk\u4ee3\u66ff\uff0cpk\u5168\u62fc\u4e3aprimary key\u3002<\/p>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">1.3 \u5c5e\u6027\u547d\u540d\u9650\u5236<\/span><\/p>\n<ul>\n<li>\u4e0d\u80fd\u662fpython\u7684\u4fdd\u7559\u5173\u952e\u5b57\u3002<\/li>\n<li>\u4e0d\u5141\u8bb8\u4f7f\u7528\u8fde\u7eed\u7684\u4e0b\u5212\u7ebf\uff0c\u8fd9\u662f\u7531django\u7684\u67e5\u8be2\u65b9\u5f0f\u51b3\u5b9a\u7684\u3002<\/li>\n<li>\u5b9a\u4e49\u5c5e\u6027\u65f6\u9700\u8981\u6307\u5b9a\u5b57\u6bb5\u7c7b\u578b\uff0c\u901a\u8fc7\u5b57\u6bb5\u7c7b\u578b\u7684\u53c2\u6570\u6307\u5b9a\u9009\u9879\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/li>\n<\/ul>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs fix has-numbering\" style=\"color: #4f4f4f !important;\"><span class=\"hljs-attribute\" style=\"color: #0086b3;\">\u5c5e\u6027<\/span>=<span class=\"hljs-string\" style=\"color: #009900 !important;\">models.\u5b57\u6bb5\u7c7b\u578b(\u9009\u9879)<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">1.4 \u5b57\u6bb5\u7c7b\u578b<\/span><br \/>\n<img decoding=\"async\" title=\"\" src=\"https:\/\/img-blog.csdn.net\/20180902153820684?watermark\/2\/text\/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lhbnBlbmdnb25n\/font\/5a6L5L2T\/fontsize\/400\/fill\/I0JBQkFCMA==\/dissolve\/70\" alt=\"\u8fd9\u91cc\u5199\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n<span style=\"font-weight: bold;\">1.5 \u9009\u9879<\/span><br \/>\n<img decoding=\"async\" title=\"\" src=\"https:\/\/img-blog.csdn.net\/20180902153943443?watermark\/2\/text\/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lhbnBlbmdnb25n\/font\/5a6L5L2T\/fontsize\/400\/fill\/I0JBQkFCMA==\/dissolve\/70\" alt=\"\u8fd9\u91cc\u5199\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n<span style=\"font-weight: bold;\">null\u662f\u6570\u636e\u5e93\u8303\u7574\u7684\u6982\u5ff5\uff0cblank\u662f\u8868\u5355\u9a8c\u8bc1\u8303\u7574\u7684<\/span><\/p>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">1.6 \u5916\u952e<\/span><br \/>\n\u5728\u8bbe\u7f6e\u5916\u952e\u65f6\uff0c\u9700\u8981\u901a\u8fc7on_delete\u9009\u9879\u6307\u660e\u4e3b\u8868\u5220\u9664\u6570\u636e\u65f6\uff0c\u5bf9\u4e8e\u5916\u952e\u5f15\u7528\u8868\u6570\u636e\u5982\u4f55\u5904\u7406\uff0c\u5728django.db.models\u4e2d\u5305\u542b\u4e86\u53ef\u9009\u5e38\u91cf\uff1a<\/p>\n<ul>\n<li>CASCADE \u7ea7\u8054\uff0c\u5220\u9664\u4e3b\u8868\u6570\u636e\u65f6\u8fde\u901a\u4e00\u8d77\u5220\u9664\u5916\u952e\u8868\u4e2d\u6570\u636e<\/li>\n<li>PROTECT \u4fdd\u62a4\uff0c\u901a\u8fc7\u629b\u51faProtectedError\u5f02\u5e38\uff0c\u6765\u963b\u6b62\u5220\u9664\u4e3b\u8868\u4e2d\u88ab\u5916\u952e\u5e94\u7528\u7684\u6570\u636e<\/li>\n<li>SET_NULL \u8bbe\u7f6e\u4e3aNULL\uff0c\u4ec5\u5728\u8be5\u5b57\u6bb5null=True\u5141\u8bb8\u4e3anull\u65f6\u53ef\u7528<\/li>\n<li>SET_DEFAULT \u8bbe\u7f6e\u4e3a\u9ed8\u8ba4\u503c\uff0c\u4ec5\u5728\u8be5\u5b57\u6bb5\u8bbe\u7f6e\u4e86\u9ed8\u8ba4\u503c\u65f6\u53ef\u7528<\/li>\n<li>SET() \u8bbe\u7f6e\u4e3a\u7279\u5b9a\u503c\u6216\u8005\u8c03\u7528\u7279\u5b9a\u65b9\u6cd5\uff0c\u5982<\/li>\n<\/ul>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs python has-numbering\" style=\"color: #4f4f4f !important;\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">from<\/span> django.conf <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">import<\/span> settings\r\n<span class=\"hljs-keyword\" style=\"color: #000088 !important;\">from<\/span> django.contrib.auth <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">import<\/span> get_user_model\r\n<span class=\"hljs-keyword\" style=\"color: #000088 !important;\">from<\/span> django.db <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">import<\/span> models\r\n\r\n<span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">def<\/span> <span class=\"hljs-title\" style=\"color: #009900 !important;\">get_sentinel_user<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\r\n    <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">return<\/span> get_user_model().objects.get_or_create(username=<span class=\"hljs-string\" style=\"color: #009900 !important;\">'deleted'<\/span>)[<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>]\r\n\r\n<span class=\"hljs-class\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">class<\/span> <span class=\"hljs-title\">MyModel<\/span><span class=\"hljs-params\">(models.Model)<\/span>:<\/span>\r\n    user = models.ForeignKey(\r\n        settings.AUTH_USER_MODEL,\r\n        on_delete=models.SET(get_sentinel_user),\r\n    )<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<li style=\"color: #999999;\">3<\/li>\n<li style=\"color: #999999;\">4<\/li>\n<li style=\"color: #999999;\">5<\/li>\n<li style=\"color: #999999;\">6<\/li>\n<li style=\"color: #999999;\">7<\/li>\n<li style=\"color: #999999;\">8<\/li>\n<li style=\"color: #999999;\">9<\/li>\n<li style=\"color: #999999;\">10<\/li>\n<li style=\"color: #999999;\">11<\/li>\n<li style=\"color: #999999;\">12<\/li>\n<\/ul>\n<ul>\n<li>DO_NOTHING \u4e0d\u505a\u4efb\u4f55\u64cd\u4f5c\uff0c\u5982\u679c\u6570\u636e\u5e93\u524d\u7f6e\u6307\u660e\u7ea7\u8054\u6027\uff0c\u6b64\u9009\u9879\u4f1a\u629b\u51faIntegrityError\u5f02\u5e38<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">2. \u8fc1\u79fb<\/span><\/p>\n<p style=\"color: #4d4d4d;\">\u5c06\u6a21\u578b\u7c7b\u540c\u6b65\u5230\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">2.1 \u751f\u6210\u8fc1\u79fb\u6587\u4ef6<\/span><\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs avrasm has-numbering\" style=\"color: #4f4f4f !important;\">python manage<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.py<\/span> makemigrations<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">2.2 \u540c\u6b65\u5230\u6570\u636e\u5e93\u4e2d<\/span><\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs avrasm has-numbering\" style=\"color: #4f4f4f !important;\">python manage<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.py<\/span> migrate<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">3. \u6dfb\u52a0\u6d4b\u8bd5\u6570\u636e<\/span><\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs sql has-numbering\" style=\"color: #4f4f4f !important;\"><span class=\"hljs-operator\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">insert<\/span> <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">into<\/span> tb_books(btitle,bread,bcomment,is_delete) <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">values<\/span>\r\n(<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u5c04\u96d5\u82f1\u96c4\u4f20'<\/span>,<span class=\"hljs-string\" style=\"color: #009900 !important;\">'1980-5-1'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">12<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">34<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>),\r\n(<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u5929\u9f99\u516b\u90e8'<\/span>,<span class=\"hljs-string\" style=\"color: #009900 !important;\">'1986-7-24'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">36<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">40<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>),\r\n(<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u7b11\u50b2\u6c5f\u6e56'<\/span>,<span class=\"hljs-string\" style=\"color: #009900 !important;\">'1995-12-24'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">20<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">80<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>),\r\n(<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u96ea\u5c71\u98de\u72d0'<\/span>,<span class=\"hljs-string\" style=\"color: #009900 !important;\">'1987-11-11'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">58<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">24<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>);<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<li style=\"color: #999999;\">3<\/li>\n<li style=\"color: #999999;\">4<\/li>\n<li style=\"color: #999999;\">5<\/li>\n<\/ul>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs sql has-numbering\" style=\"color: #4f4f4f !important;\"><span class=\"hljs-operator\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">insert<\/span> <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">into<\/span> tb_heros(hname,hgender,hbook_id,hcomment,is_delete) <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">values<\/span>\r\n(<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u90ed\u9756'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">1<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">1<\/span>,<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u964d\u9f99\u5341\u516b\u638c'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>),\r\n(<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u9ec4\u84c9'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">1<\/span>,<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u6253\u72d7\u68cd\u6cd5'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>),\r\n(<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u9ec4\u836f\u5e08'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">1<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">1<\/span>,<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u5f39\u6307\u795e\u901a'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>),\r\n(<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u6b27\u9633\u950b'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">1<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">1<\/span>,<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u86e4\u87c6\u529f'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>),\r\n(<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u6885\u8d85\u98ce'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">1<\/span>,<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u4e5d\u9634\u767d\u9aa8\u722a'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>),\r\n(<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u4e54\u5cf0'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">1<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">2<\/span>,<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u964d\u9f99\u5341\u516b\u638c'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>),\r\n(<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u6bb5\u8a89'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">1<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">2<\/span>,<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u516d\u8109\u795e\u5251'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>),\r\n(<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u865a\u7af9'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">1<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">2<\/span>,<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u5929\u5c71\u516d\u9633\u638c'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>),\r\n(<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u738b\u8bed\u5ae3'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">2<\/span>,<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u795e\u4ed9\u59d0\u59d0'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>),\r\n(<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u4ee4\u72d0\u51b2'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">1<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">3<\/span>,<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u72ec\u5b64\u4e5d\u5251'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>),\r\n(<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u4efb\u76c8\u76c8'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">3<\/span>,<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u5f39\u7434'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>),\r\n(<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u5cb3\u4e0d\u7fa4'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">1<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">3<\/span>,<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u534e\u5c71\u5251\u6cd5'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>),\r\n(<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u4e1c\u65b9\u4e0d\u8d25'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">3<\/span>,<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u8475\u82b1\u5b9d\u5178'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>),\r\n(<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u80e1\u6590'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">1<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">4<\/span>,<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u80e1\u5bb6\u5200\u6cd5'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>),\r\n(<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u82d7\u82e5\u5170'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">4<\/span>,<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u9ec4\u8863'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>),\r\n(<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u7a0b\u7075\u7d20'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">4<\/span>,<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u533b\u672f'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>),\r\n(<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u8881\u7d2b\u8863'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">4<\/span>,<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u516d\u5408\u62f3'<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>);<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<li style=\"color: #999999;\">3<\/li>\n<li style=\"color: #999999;\">4<\/li>\n<li style=\"color: #999999;\">5<\/li>\n<li style=\"color: #999999;\">6<\/li>\n<li style=\"color: #999999;\">7<\/li>\n<li style=\"color: #999999;\">8<\/li>\n<li style=\"color: #999999;\">9<\/li>\n<li style=\"color: #999999;\">10<\/li>\n<li style=\"color: #999999;\">11<\/li>\n<li style=\"color: #999999;\">12<\/li>\n<li style=\"color: #999999;\">13<\/li>\n<li style=\"color: #999999;\">14<\/li>\n<li style=\"color: #999999;\">15<\/li>\n<li style=\"color: #999999;\">16<\/li>\n<li style=\"color: #999999;\">17<\/li>\n<li style=\"color: #999999;\">18<\/li>\n<\/ul>\n<hr \/>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">\u4e09\u3001\u6f14\u793a\u5de5\u5177\u4f7f\u7528<\/span><br \/>\n<span style=\"font-weight: bold;\">1. shell\u5de5\u5177<\/span><\/p>\n<p style=\"color: #4d4d4d;\">Django\u7684manage\u5de5\u5177\u63d0\u4f9b\u4e86shell\u547d\u4ee4\uff0c\u5e2e\u52a9\u6211\u4eec\u914d\u7f6e\u597d\u5f53\u524d\u5de5\u7a0b\u7684\u8fd0\u884c\u73af\u5883\uff08\u5982\u8fde\u63a5\u597d\u6570\u636e\u5e93\u7b49\uff09\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u76f4\u63a5\u5728\u7ec8\u7aef\u4e2d\u6267\u884c\u6d4b\u8bd5python\u8bed\u53e5\u3002<\/p>\n<p style=\"color: #4d4d4d;\">\u901a\u8fc7\u5982\u4e0b\u547d\u4ee4\u8fdb\u5165shell<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs livecodeserver has-numbering\" style=\"color: #4f4f4f !important;\">python manage.py <span class=\"hljs-built_in\">shell<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\u5bfc\u5165\u4e24\u4e2a\u6a21\u578b\u7c7b\uff0c\u4ee5\u4fbf\u540e\u7eed\u4f7f\u7528<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs python has-numbering\" style=\"color: #4f4f4f !important;\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">from<\/span> booktest.models <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">import<\/span> BookInfo, HeroInfo<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">2. \u67e5\u770bMySQL\u6570\u636e\u5e93\u65e5\u5fd7<\/span><\/p>\n<p style=\"color: #4d4d4d;\">\u67e5\u770bmysql\u6570\u636e\u5e93\u65e5\u5fd7\u53ef\u4ee5\u67e5\u770b\u5bf9\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u8bb0\u5f55\u3002 mysql\u65e5\u5fd7\u6587\u4ef6\u9ed8\u8ba4\u6ca1\u6709\u4ea7\u751f\uff0c\u9700\u8981\u505a\u5982\u4e0b\u914d\u7f6e\uff1a<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs avrasm has-numbering\" style=\"color: #4f4f4f !important;\">sudo vi \/etc\/mysql\/mysql<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.conf<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.d<\/span>\/mysqld<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.cnf<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\u4fee\u6539\u6216\u589e\u52a0\u5185\u5bb9\uff1a<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs lasso has-numbering\" style=\"color: #4f4f4f !important;\">general_log_file    <span class=\"hljs-subst\">=<\/span>\/<span class=\"hljs-built_in\">var<\/span>\/<span class=\"hljs-keyword\" style=\"color: #000088 !important;\">log<\/span>\/mysql\/mysql<span class=\"hljs-built_in\">.<\/span><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">log<\/span>\r\ngeneral_log         <span class=\"hljs-subst\">=<\/span><span class=\"hljs-number\" style=\"color: #006666 !important;\">1<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\u7136\u540e\u4fdd\u5b58\u5e76\u4f7f\u7528\u5982\u4e0b\u547d\u4ee4\u91cd\u542fmysql\u670d\u52a1\u3002<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs bash has-numbering\" style=\"color: #4f4f4f !important;\"><span class=\"hljs-built_in\">sudo<\/span> service mysql restart<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\u4f7f\u7528\u5982\u4e0b\u547d\u4ee4\u6253\u5f00mysql\u65e5\u5fd7\u6587\u4ef6\u3002<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs glsl has-numbering\" style=\"color: #4f4f4f !important;\">tail -f \/var\/<span class=\"hljs-built_in\">log<\/span>\/mysql\/mysql.<span class=\"hljs-built_in\">log<\/span>  <span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\"># \u53ef\u4ee5\u5b9e\u65f6\u67e5\u770b\u6570\u636e\u5e93\u7684\u65e5\u5fd7\u5185\u5bb9<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">Mac\u4e0a\u53ef\u4ee5\u8fd9\u6837\u64cd\u4f5c\uff1a<\/span><br \/>\n\u5f00\u542f general log \u5c06\u6240\u6709\u5230\u8fbeMySQL Server\u7684SQL\u8bed\u53e5\u8bb0\u5f55\u4e0b\u6765\u3002<\/p>\n<p style=\"color: #4d4d4d;\">\u4e00\u822c\u4e0d\u4f1a\u5f00\u542f\u5f00\u529f\u80fd\uff0c\u56e0\u4e3alog\u7684\u91cf\u4f1a\u975e\u5e38\u5e9e\u5927\u3002\u4f46\u4e2a\u522b\u60c5\u51b5\u4e0b\u53ef\u80fd\u4f1a\u4e34\u65f6\u7684\u5f00\u4e00\u4f1a\u513fgeneral log\u4ee5\u4f9b\u6392\u969c\u4f7f\u7528\u3002<br \/>\n\u76f8\u5173\u53c2\u6570\u4e00\u5171\u67093\uff1ageneral_log\u3001log_output\u3001general_log_file<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs sql has-numbering\" style=\"color: #4f4f4f !important;\"><span class=\"hljs-operator\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">show<\/span> variables <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">like<\/span> <span class=\"hljs-string\" style=\"color: #009900 !important;\">'general_log'<\/span>;<\/span>  <span class=\"hljs-comment\" style=\"color: #880000 !important;\">-- \u67e5\u770b\u65e5\u5fd7\u662f\u5426\u5f00\u542f<\/span>\r\n<span class=\"hljs-operator\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">set<\/span> <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">global<\/span> general_log=<span class=\"hljs-keyword\" style=\"color: #000088 !important;\">on<\/span>;<\/span> <span class=\"hljs-comment\" style=\"color: #880000 !important;\">-- \u5f00\u542f\u65e5\u5fd7\u529f\u80fd<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<\/ul>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs sql has-numbering\" style=\"color: #4f4f4f !important;\"><span class=\"hljs-operator\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">show<\/span> variables <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">like<\/span> <span class=\"hljs-string\" style=\"color: #009900 !important;\">'general_log_file'<\/span>;<\/span>  <span class=\"hljs-comment\" style=\"color: #880000 !important;\">-- \u770b\u770b\u65e5\u5fd7\u6587\u4ef6\u4fdd\u5b58\u4f4d\u7f6e<\/span>\r\n<span class=\"hljs-operator\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">set<\/span> <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">global<\/span> general_log_file=<span class=\"hljs-string\" style=\"color: #009900 !important;\">'tmp\/general.lg'<\/span>;<\/span> <span class=\"hljs-comment\" style=\"color: #880000 !important;\">-- \u8bbe\u7f6e\u65e5\u5fd7\u6587\u4ef6\u4fdd\u5b58\u4f4d\u7f6e<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<\/ul>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs sql has-numbering\" style=\"color: #4f4f4f !important;\"><span class=\"hljs-operator\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">show<\/span> variables <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">like<\/span> <span class=\"hljs-string\" style=\"color: #009900 !important;\">'log_output'<\/span>;<\/span>  <span class=\"hljs-comment\" style=\"color: #880000 !important;\">-- \u770b\u770b\u65e5\u5fd7\u8f93\u51fa\u7c7b\u578b  table\u6216file<\/span>\r\n<span class=\"hljs-operator\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">set<\/span> <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">global<\/span> log_output=<span class=\"hljs-string\" style=\"color: #009900 !important;\">'table'<\/span>;<\/span> <span class=\"hljs-comment\" style=\"color: #880000 !important;\">-- \u8bbe\u7f6e\u8f93\u51fa\u7c7b\u578b\u4e3a table<\/span>\r\n<span class=\"hljs-operator\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">set<\/span> <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">global<\/span> log_output=<span class=\"hljs-string\" style=\"color: #009900 !important;\">'file'<\/span>;<\/span>   <span class=\"hljs-comment\" style=\"color: #880000 !important;\">-- \u8bbe\u7f6e\u8f93\u51fa\u7c7b\u578b\u4e3afile<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<li style=\"color: #999999;\">3<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">\u6ce8\uff1a<\/span>\u5177\u4f53\u8be6\u60c5\u8bf7\u53c2\u7167\uff1a<br \/>\n<a style=\"color: #6795b5;\" href=\"https:\/\/blog.csdn.net\/yanpenggong\/article\/details\/82286793\">https:\/\/blog.csdn.net\/yanpenggong\/article\/details\/82286793<\/a><\/p>\n<h2 id=\"\u56db\u6570\u636e\u5e93\u64cd\u4f5c\u589e\u5220\u6539\u67e5\" style=\"font-weight: bold; color: #4f4f4f;\"><a style=\"font-weight: normal; color: #4ea1db;\" name=\"t3\"><\/a><a style=\"font-weight: normal; color: #4ea1db;\" name=\"t3\"><\/a>\u56db\u3001\u6570\u636e\u5e93\u64cd\u4f5c\u2014\u589e\u3001\u5220\u3001\u6539\u3001\u67e5<\/h2>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">1. \u589e\u52a0<\/span><\/p>\n<p style=\"color: #4d4d4d;\">\u589e\u52a0\u6570\u636e\u6709\u4e24\u79cd\u65b9\u6cd5\u3002<\/p>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">1.1 save<\/span><\/p>\n<p style=\"color: #4d4d4d;\">\u901a\u8fc7\u521b\u5efa\u6a21\u578b\u7c7b\u5bf9\u8c61\uff0c\u6267\u884c\u5bf9\u8c61\u7684save()\u65b9\u6cd5\u4fdd\u5b58\u5230\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs python has-numbering\" style=\"color: #4f4f4f !important;\"><span class=\"hljs-prompt\" style=\"color: #006666 !important;\">&gt;&gt;&gt; <\/span><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">from<\/span> datetime <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">import<\/span> date\r\n<span class=\"hljs-prompt\" style=\"color: #006666 !important;\">&gt;&gt;&gt; <\/span>book = BookInfo(\r\n    btitle=<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u897f\u6e38\u8bb0'<\/span>,\r\n    bput_date=date(<span class=\"hljs-number\" style=\"color: #006666 !important;\">1988<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">1<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">1<\/span>),\r\n    bread=<span class=\"hljs-number\" style=\"color: #006666 !important;\">10<\/span>,\r\n    bcomment=<span class=\"hljs-number\" style=\"color: #006666 !important;\">10<\/span>\r\n)\r\n<span class=\"hljs-prompt\" style=\"color: #006666 !important;\">&gt;&gt;&gt; <\/span>book.save()\r\n<span class=\"hljs-prompt\" style=\"color: #006666 !important;\">&gt;&gt;&gt; <\/span>hero = HeroInfo(\r\n    hname=<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u5b59\u609f\u7a7a'<\/span>,\r\n    hgender=<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>,\r\n    hbook=book\r\n)\r\n<span class=\"hljs-prompt\" style=\"color: #006666 !important;\">&gt;&gt;&gt; <\/span>hero.save()\r\n<span class=\"hljs-prompt\" style=\"color: #006666 !important;\">&gt;&gt;&gt; <\/span>hero2 = HeroInfo(\r\n    hname=<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u732a\u516b\u6212'<\/span>,\r\n    hgender=<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>,\r\n    hbook_id=book.id\r\n)\r\n<span class=\"hljs-prompt\" style=\"color: #006666 !important;\">&gt;&gt;&gt; <\/span>hero2.save()<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<li style=\"color: #999999;\">3<\/li>\n<li style=\"color: #999999;\">4<\/li>\n<li style=\"color: #999999;\">5<\/li>\n<li style=\"color: #999999;\">6<\/li>\n<li style=\"color: #999999;\">7<\/li>\n<li style=\"color: #999999;\">8<\/li>\n<li style=\"color: #999999;\">9<\/li>\n<li style=\"color: #999999;\">10<\/li>\n<li style=\"color: #999999;\">11<\/li>\n<li style=\"color: #999999;\">12<\/li>\n<li style=\"color: #999999;\">13<\/li>\n<li style=\"color: #999999;\">14<\/li>\n<li style=\"color: #999999;\">15<\/li>\n<li style=\"color: #999999;\">16<\/li>\n<li style=\"color: #999999;\">17<\/li>\n<li style=\"color: #999999;\">18<\/li>\n<li style=\"color: #999999;\">19<\/li>\n<li style=\"color: #999999;\">20<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">1.2 create<\/span><\/p>\n<p style=\"color: #4d4d4d;\">\u901a\u8fc7\u6a21\u578b\u7c7b.objects.create()\u4fdd\u5b58\u3002<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs python has-numbering\" style=\"color: #4f4f4f !important;\"><span class=\"hljs-prompt\" style=\"color: #006666 !important;\">&gt;&gt;&gt; <\/span>HeroInfo.objects.create(\r\n    hname=<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u6c99\u609f\u51c0'<\/span>,\r\n    hgender=<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>,\r\n    hbook=book\r\n)\r\n&lt;HeroInfo: \u6c99\u609f\u51c0&gt;<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<li style=\"color: #999999;\">3<\/li>\n<li style=\"color: #999999;\">4<\/li>\n<li style=\"color: #999999;\">5<\/li>\n<li style=\"color: #999999;\">6<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">2. \u67e5\u8be2<\/span><\/p>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">2.1 \u57fa\u672c\u67e5\u8be2<\/span><br \/>\n<span style=\"font-weight: bold;\">get<\/span>\u00a0:\u67e5\u8be2\u5355\u4e00\u7ed3\u679c\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u4f1a\u629b\u51fa<span style=\"font-weight: bold;\">\u6a21\u578b\u7c7b.DoesNotExist<\/span>\u5f02\u5e38\u3002<br \/>\n<span style=\"font-weight: bold;\">all<\/span>\u00a0:\u67e5\u8be2\u591a\u4e2a\u7ed3\u679c\u3002<br \/>\n<span style=\"font-weight: bold;\">count<\/span>\u00a0:\u67e5\u8be2\u7ed3\u679c\u6570\u91cf\u3002<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs livecodeserver has-numbering\" style=\"color: #4f4f4f !important;\">&gt;&gt;&gt; BookInfo.objects.all()\r\n&lt;QuerySet [&lt;BookInfo: \u5c04\u96d5\u82f1\u96c4\u4f20&gt;, &lt;BookInfo: \u5929\u9f99\u516b\u90e8&gt;, &lt;BookInfo: \u7b11\u50b2\u6c5f\u6e56&gt;, &lt;BookInfo: \u96ea\u5c71\u98de\u72d0&gt;, &lt;BookInfo: \u897f\u6e38\u8bb0&gt;]&gt;\r\n&gt;&gt;&gt; book = BookInfo.objects.<span class=\"hljs-built_in\">get<\/span>(btitle=<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u897f\u6e38\u8bb0'<\/span>)\r\n&gt;&gt;&gt; book.id\r\n<span class=\"hljs-number\" style=\"color: #006666 !important;\">5<\/span>\r\n\r\n&gt;&gt;&gt; BookInfo.objects.<span class=\"hljs-built_in\">get<\/span>(id=<span class=\"hljs-number\" style=\"color: #006666 !important;\">3<\/span>)\r\n&lt;BookInfo: \u7b11\u50b2\u6c5f\u6e56&gt;\r\n&gt;&gt;&gt; BookInfo.objects.<span class=\"hljs-built_in\">get<\/span>(pk=<span class=\"hljs-number\" style=\"color: #006666 !important;\">3<\/span>)\r\n&lt;BookInfo: \u96ea\u5c71\u98de\u72d0&gt;\r\n&gt;&gt;&gt; BookInfo.objects.<span class=\"hljs-built_in\">get<\/span>(id=<span class=\"hljs-number\" style=\"color: #006666 !important;\">100<\/span>)\r\nTraceback (most recent call <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">last<\/span>):\r\n  File <span class=\"hljs-string\" style=\"color: #009900 !important;\">\"&lt;console&gt;\"<\/span>, <span class=\"hljs-built_in\">line<\/span> <span class=\"hljs-number\" style=\"color: #006666 !important;\">1<\/span>, <span class=\"hljs-operator\">in<\/span> &lt;module&gt;\r\n  File <span class=\"hljs-string\" style=\"color: #009900 !important;\">\"\/Users\/delron\/.virtualenv\/dj\/lib\/python3.6\/site-packages\/django\/db\/models\/manager.py\"<\/span>, <span class=\"hljs-built_in\">line<\/span> <span class=\"hljs-number\" style=\"color: #006666 !important;\">85<\/span>, <span class=\"hljs-operator\">in<\/span> manager_method\r\n    <span class=\"hljs-constant\" style=\"color: #009900 !important;\">return<\/span> getattr(self.get_queryset(), name)(*args, **kwargs)\r\n  File <span class=\"hljs-string\" style=\"color: #009900 !important;\">\"\/Users\/delron\/.virtualenv\/dj\/lib\/python3.6\/site-packages\/django\/db\/models\/query.py\"<\/span>, <span class=\"hljs-built_in\">line<\/span> <span class=\"hljs-number\" style=\"color: #006666 !important;\">380<\/span>, <span class=\"hljs-operator\">in<\/span> <span class=\"hljs-built_in\">get<\/span>\r\n    self.model.<span class=\"hljs-title\" style=\"color: #009900 !important;\">_meta<\/span>.object_name\r\ndb.models.DoesNotExist: BookInfo matching query does <span class=\"hljs-operator\">not<\/span> exist.\r\n\r\n&gt;&gt;&gt; BookInfo.objects.count()\r\n<span class=\"hljs-number\" style=\"color: #006666 !important;\">6<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<li style=\"color: #999999;\">3<\/li>\n<li style=\"color: #999999;\">4<\/li>\n<li style=\"color: #999999;\">5<\/li>\n<li style=\"color: #999999;\">6<\/li>\n<li style=\"color: #999999;\">7<\/li>\n<li style=\"color: #999999;\">8<\/li>\n<li style=\"color: #999999;\">9<\/li>\n<li style=\"color: #999999;\">10<\/li>\n<li style=\"color: #999999;\">11<\/li>\n<li style=\"color: #999999;\">12<\/li>\n<li style=\"color: #999999;\">13<\/li>\n<li style=\"color: #999999;\">14<\/li>\n<li style=\"color: #999999;\">15<\/li>\n<li style=\"color: #999999;\">16<\/li>\n<li style=\"color: #999999;\">17<\/li>\n<li style=\"color: #999999;\">18<\/li>\n<li style=\"color: #999999;\">19<\/li>\n<li style=\"color: #999999;\">20<\/li>\n<li style=\"color: #999999;\">21<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">2.2 \u8fc7\u6ee4\u67e5\u8be2<\/span><\/p>\n<p style=\"color: #4d4d4d;\">\u5b9e\u73b0SQL\u4e2d\u7684where\u529f\u80fd\uff0c\u5305\u62ec<\/p>\n<ul>\n<li>filter \u8fc7\u6ee4\u51fa\u591a\u4e2a\u7ed3\u679c<\/li>\n<li>exclude \u6392\u9664\u6389\u7b26\u5408\u6761\u4ef6\u5269\u4e0b\u7684\u7ed3\u679c<\/li>\n<li>get \u8fc7\u6ee4\u5355\u4e00\u7ed3\u679c<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\u5bf9\u4e8e\u8fc7\u6ee4\u6761\u4ef6\u7684\u4f7f\u7528\uff0c\u4e0a\u8ff0\u4e09\u4e2a\u65b9\u6cd5\u76f8\u540c\uff0c\u6545\u4ec5\u4ee5<span style=\"font-weight: bold;\">filter<\/span>\u8fdb\u884c\u8bb2\u89e3\u3002<\/p>\n<p style=\"color: #4d4d4d;\">\u8fc7\u6ee4\u6761\u4ef6\u7684\u8868\u8fbe\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs vala has-numbering\" style=\"color: #4f4f4f !important;\">\u5c5e\u6027\u540d\u79f0__\u6bd4\u8f83\u8fd0\u7b97\u7b26=\u503c\r\n<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\"># \u5c5e\u6027\u540d\u79f0\u548c\u6bd4\u8f83\u8fd0\u7b97\u7b26\u95f4\u4f7f\u7528\u4e24\u4e2a\u4e0b\u5212\u7ebf\uff0c\u6240\u4ee5\u5c5e\u6027\u540d\u4e0d\u80fd\u5305\u62ec\u591a\u4e2a\u4e0b\u5212\u7ebf<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">1\uff09\u76f8\u7b49<\/span><\/p>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">exact<\/span>\uff1a\u8868\u793a\u5224\u7b49\u3002<\/p>\n<p style=\"color: #4d4d4d;\">\u4f8b\uff1a\u67e5\u8be2\u7f16\u53f7\u4e3a3\u7684\u56fe\u4e66\u3002<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs avrasm has-numbering\" style=\"color: #4f4f4f !important;\">BookInfo<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.objects<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.filter<\/span>(id__exact=<span class=\"hljs-number\" style=\"color: #006666 !important;\">3<\/span>)\r\n\u53ef\u7b80\u5199\u4e3a\uff1a\r\nBookInfo<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.objects<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.filter<\/span>(id=<span class=\"hljs-number\" style=\"color: #006666 !important;\">3<\/span>)<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<li style=\"color: #999999;\">3<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">2\uff09\u6a21\u7cca\u67e5\u8be2<\/span><\/p>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">contains<\/span>\uff1a\u662f\u5426\u5305\u542b\u3002<\/p>\n<p style=\"color: #4d4d4d;\"><em>\u8bf4\u660e\uff1a\u5982\u679c\u8981\u5305\u542b%\u65e0\u9700\u8f6c\u4e49\uff0c\u76f4\u63a5\u5199\u5373\u53ef\u3002<\/em><\/p>\n<p style=\"color: #4d4d4d;\">\u4f8b\uff1a\u67e5\u8be2\u4e66\u540d\u5305\u542b\u2019\u6e38\u2019\u7684\u56fe\u4e66\u3002<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs avrasm has-numbering\" style=\"color: #4f4f4f !important;\">BookInfo<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.objects<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.filter<\/span>(btitle__contains=<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u6e38'<\/span>)<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">startswith\u3001endswith\uff1a\u4ee5\u6307\u5b9a\u503c\u5f00\u5934\u6216\u7ed3\u5c3e\u3002<\/span><br \/>\n\u4f8b\uff1a\u67e5\u8be2\u4e66\u540d\u4ee5\u2019\u8bb0\u2019\u7ed3\u5c3e\u7684\u56fe\u4e66<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs avrasm has-numbering\" style=\"color: #4f4f4f !important;\">BookInfo<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.objects<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.filter<\/span>(btitle__endswith=<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u8bb0'<\/span>)<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\u4ee5\u4e0a\u8fd0\u7b97\u7b26\u90fd\u533a\u5206\u5927\u5c0f\u5199\uff0c\u5728\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u524d\u52a0\u4e0ai\u8868\u793a\u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff0c\u5982iexact\u3001icontains\u3001istartswith\u3001iendswith.<\/p>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">3\uff09 \u7a7a\u67e5\u8be2<\/span><\/p>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">isnull<\/span>\uff1a\u662f\u5426\u4e3anull\u3002<\/p>\n<p style=\"color: #4d4d4d;\">\u4f8b\uff1a\u67e5\u8be2\u4e66\u540d\u4e0d\u4e3a\u7a7a\u7684\u56fe\u4e66\u3002<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs avrasm has-numbering\" style=\"color: #4f4f4f !important;\">BookInfo<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.objects<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.filter<\/span>(btitle__isnull=False)<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">4\uff09 \u8303\u56f4\u67e5\u8be2<\/span><\/p>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">in<\/span>\uff1a\u662f\u5426\u5305\u542b\u5728\u8303\u56f4\u5185\u3002<\/p>\n<p style=\"color: #4d4d4d;\">\u4f8b\uff1a\u67e5\u8be2\u7f16\u53f7\u4e3a1\u62163\u62165\u7684\u56fe\u4e66<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs avrasm has-numbering\" style=\"color: #4f4f4f !important;\">BookInfo<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.objects<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.filter<\/span>(id__in=[<span class=\"hljs-number\" style=\"color: #006666 !important;\">1<\/span>, <span class=\"hljs-number\" style=\"color: #006666 !important;\">3<\/span>, <span class=\"hljs-number\" style=\"color: #006666 !important;\">5<\/span>])<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">5\uff09\u6bd4\u8f83\u67e5\u8be2<\/span><\/p>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">gt<\/span>\u00a0\uff1a\u5927\u4e8e (greater then)<br \/>\n<span style=\"font-weight: bold;\">gte<\/span>\u00a0\uff1a\u5927\u4e8e\u7b49\u4e8e (greater then equal)<br \/>\n<span style=\"font-weight: bold;\">lt<\/span>\u00a0\uff1a\u5c0f\u4e8e (less then)<br \/>\n<span style=\"font-weight: bold;\">lte<\/span>\u00a0\uff1a\u5c0f\u4e8e\u7b49\u4e8e (less then equal)<br \/>\n\u4f8b\uff1a\u67e5\u8be2\u7f16\u53f7\u5927\u4e8e3\u7684\u56fe\u4e66<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs avrasm has-numbering\" style=\"color: #4f4f4f !important;\">BookInfo<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.objects<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.filter<\/span>(id__gt=<span class=\"hljs-number\" style=\"color: #006666 !important;\">3<\/span>)<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\u4e0d\u7b49\u4e8e\u7684\u8fd0\u7b97\u7b26\uff0c\u4f7f\u7528exclude()\u8fc7\u6ee4\u5668\u3002<\/p>\n<p style=\"color: #4d4d4d;\">\u4f8b\uff1a\u67e5\u8be2\u7f16\u53f7\u4e0d\u7b49\u4e8e3\u7684\u56fe\u4e66<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs avrasm has-numbering\" style=\"color: #4f4f4f !important;\">BookInfo<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.objects<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.exclude<\/span>(id=<span class=\"hljs-number\" style=\"color: #006666 !important;\">3<\/span>)<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">6\uff09\u65e5\u671f\u67e5\u8be2<\/span><\/p>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">year<\/span>\u3001<span style=\"font-weight: bold;\">month<\/span>\u3001<span style=\"font-weight: bold;\">day<\/span>\u3001<span style=\"font-weight: bold;\">week_day<\/span>\u3001<span style=\"font-weight: bold;\">hour<\/span>\u3001<span style=\"font-weight: bold;\">minute<\/span>\u3001<span style=\"font-weight: bold;\">second<\/span>\uff1a\u5bf9\u65e5\u671f\u65f6\u95f4\u7c7b\u578b\u7684\u5c5e\u6027\u8fdb\u884c\u8fd0\u7b97\u3002<\/p>\n<p style=\"color: #4d4d4d;\">\u4f8b\uff1a\u67e5\u8be21980\u5e74\u53d1\u8868\u7684\u56fe\u4e66\u3002<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs avrasm has-numbering\" style=\"color: #4f4f4f !important;\">BookInfo<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.objects<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.filter<\/span>(bpub_date__year=<span class=\"hljs-number\" style=\"color: #006666 !important;\">1980<\/span>)<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\u4f8b\uff1a\u67e5\u8be21980\u5e741\u67081\u65e5\u540e\u53d1\u8868\u7684\u56fe\u4e66\u3002<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs livecodeserver has-numbering\" style=\"color: #4f4f4f !important;\">BookInfo.objects.<span class=\"hljs-built_in\">filter<\/span>(bpub_date__gt=<span class=\"hljs-built_in\">date<\/span>(<span class=\"hljs-number\" style=\"color: #006666 !important;\">1990<\/span>, <span class=\"hljs-number\" style=\"color: #006666 !important;\">1<\/span>, <span class=\"hljs-number\" style=\"color: #006666 !important;\">1<\/span>))<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">F\u5bf9\u8c61<\/span><\/p>\n<p style=\"color: #4d4d4d;\">\u4e4b\u524d\u7684\u67e5\u8be2\u90fd\u662f\u5bf9\u8c61\u7684\u5c5e\u6027\u4e0e\u5e38\u91cf\u503c\u6bd4\u8f83\uff0c\u4e24\u4e2a\u5c5e\u6027\u600e\u4e48\u6bd4\u8f83\u5462\uff1f \u7b54\uff1a\u4f7f\u7528F\u5bf9\u8c61\uff0c\u88ab\u5b9a\u4e49\u5728django.db.models\u4e2d\u3002<\/p>\n<p style=\"color: #4d4d4d;\">\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs r has-numbering\" style=\"color: #4f4f4f !important;\"><span class=\"hljs-literal\" style=\"color: #006666 !important;\">F<\/span>(\u5c5e\u6027\u540d)<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\u4f8b\uff1a\u67e5\u8be2\u9605\u8bfb\u91cf\u5927\u4e8e\u7b49\u4e8e\u8bc4\u8bba\u91cf\u7684\u56fe\u4e66\u3002<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs python has-numbering\" style=\"color: #4f4f4f !important;\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">from<\/span> django.db.models <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">import<\/span> F\r\nBookInfo.objects.filter(bread__gte=F(<span class=\"hljs-string\" style=\"color: #009900 !important;\">'bcomment'<\/span>))<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\u53ef\u4ee5\u5728F\u5bf9\u8c61\u4e0a\u4f7f\u7528\u7b97\u6570\u8fd0\u7b97\u3002<\/p>\n<p style=\"color: #4d4d4d;\">\u4f8b\uff1a\u67e5\u8be2\u9605\u8bfb\u91cf\u5927\u4e8e2\u500d\u8bc4\u8bba\u91cf\u7684\u56fe\u4e66\u3002<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs livecodeserver has-numbering\" style=\"color: #4f4f4f !important;\">BookInfo.objects.<span class=\"hljs-built_in\">filter<\/span>(bread__gt=F(<span class=\"hljs-string\" style=\"color: #009900 !important;\">'bcomment'<\/span>) * <span class=\"hljs-number\" style=\"color: #006666 !important;\">2<\/span>)<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">Q\u5bf9\u8c61<\/span><\/p>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">\u591a\u4e2a\u8fc7\u6ee4\u5668\u9010\u4e2a\u8c03\u7528\u8868\u793a\u903b\u8f91\u4e0e\u5173\u7cfb\uff0c\u540csql\u8bed\u53e5\u4e2dwhere\u90e8\u5206\u7684and\u5173\u952e\u5b57\u3002<\/span><\/p>\n<p style=\"color: #4d4d4d;\">\u4f8b\uff1a\u67e5\u8be2\u9605\u8bfb\u91cf\u5927\u4e8e20\uff0c\u5e76\u4e14\u7f16\u53f7\u5c0f\u4e8e3\u7684\u56fe\u4e66\u3002<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs avrasm has-numbering\" style=\"color: #4f4f4f !important;\">BookInfo<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.objects<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.filter<\/span>(bread__gt=<span class=\"hljs-number\" style=\"color: #006666 !important;\">20<\/span>,id__lt=<span class=\"hljs-number\" style=\"color: #006666 !important;\">3<\/span>)\r\n\u6216\r\nBookInfo<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.objects<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.filter<\/span>(bread__gt=<span class=\"hljs-number\" style=\"color: #006666 !important;\">20<\/span>)<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.filter<\/span>(id__lt=<span class=\"hljs-number\" style=\"color: #006666 !important;\">3<\/span>)<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<li style=\"color: #999999;\">3<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\u5982\u679c\u9700\u8981\u5b9e\u73b0\u903b\u8f91\u6216or\u7684\u67e5\u8be2\uff0c\u9700\u8981\u4f7f\u7528Q()\u5bf9\u8c61\u7ed3\u5408|\u8fd0\u7b97\u7b26\uff0cQ\u5bf9\u8c61\u88ab\u4e49\u5728django.db.models\u4e2d\u3002<\/p>\n<p style=\"color: #4d4d4d;\">\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs fix has-numbering\" style=\"color: #4f4f4f !important;\"><span class=\"hljs-attribute\" style=\"color: #0086b3;\">Q(\u5c5e\u6027\u540d__\u8fd0\u7b97\u7b26<\/span>=<span class=\"hljs-string\" style=\"color: #009900 !important;\">\u503c)<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\u4f8b\uff1a\u67e5\u8be2\u9605\u8bfb\u91cf\u5927\u4e8e20\u7684\u56fe\u4e66\uff0c\u6539\u5199\u4e3aQ\u5bf9\u8c61\u5982\u4e0b\u3002<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs avrasm has-numbering\" style=\"color: #4f4f4f !important;\">from django<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.db<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.models<\/span> import Q\r\n\r\nBookInfo<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.objects<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.filter<\/span>(Q(bread__gt=<span class=\"hljs-number\" style=\"color: #006666 !important;\">20<\/span>))<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<li style=\"color: #999999;\">3<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">Q\u5bf9\u8c61\u53ef\u4ee5\u4f7f\u7528&amp;\u3001|\u8fde\u63a5\uff0c&amp;\u8868\u793a\u903b\u8f91\u4e0e\uff0c|\u8868\u793a\u903b\u8f91\u6216\u3002<\/p>\n<p style=\"color: #4d4d4d;\">\u4f8b\uff1a\u67e5\u8be2\u9605\u8bfb\u91cf\u5927\u4e8e20\uff0c\u6216\u7f16\u53f7\u5c0f\u4e8e3\u7684\u56fe\u4e66\uff0c\u53ea\u80fd\u4f7f\u7528Q\u5bf9\u8c61\u5b9e\u73b0<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs avrasm has-numbering\" style=\"color: #4f4f4f !important;\">BookInfo<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.objects<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.filter<\/span>(Q(bread__gt=<span class=\"hljs-number\" style=\"color: #006666 !important;\">20<\/span>) | Q(pk__lt=<span class=\"hljs-number\" style=\"color: #006666 !important;\">3<\/span>))<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">Q\u5bf9\u8c61\u524d\u53ef\u4ee5\u4f7f\u7528~\u64cd\u4f5c\u7b26\uff0c\u8868\u793a\u975enot\u3002<\/p>\n<p style=\"color: #4d4d4d;\">\u4f8b\uff1a\u67e5\u8be2\u7f16\u53f7\u4e0d\u7b49\u4e8e3\u7684\u56fe\u4e66\u3002<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs avrasm has-numbering\" style=\"color: #4f4f4f !important;\">BookInfo<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.objects<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.filter<\/span>(~Q(pk=<span class=\"hljs-number\" style=\"color: #006666 !important;\">3<\/span>))<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">\u805a\u5408\u51fd\u6570<\/span><\/p>\n<p style=\"color: #4d4d4d;\">\u4f7f\u7528aggregate()\u8fc7\u6ee4\u5668\u8c03\u7528\u805a\u5408\u51fd\u6570\u3002\u805a\u5408\u51fd\u6570\u5305\u62ec\uff1a<span style=\"font-weight: bold;\">Avg<\/span>(\u5e73\u5747)\uff0c<span style=\"font-weight: bold;\">Count<\/span>(\u6570\u91cf)\uff0c<span style=\"font-weight: bold;\">Max<\/span>(\u6700\u5927)\uff0c<span style=\"font-weight: bold;\">Min<\/span>(\u6700\u5c0f)\uff0c<span style=\"font-weight: bold;\">Sum<\/span>(\u6c42\u548c)\uff0c\u88ab\u5b9a\u4e49\u5728django.db.models\u4e2d\u3002<\/p>\n<p style=\"color: #4d4d4d;\">\u4f8b\uff1a\u67e5\u8be2\u56fe\u4e66\u7684\u603b\u9605\u8bfb\u91cf\u3002<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs lasso has-numbering\" style=\"color: #4f4f4f !important;\">from django<span class=\"hljs-built_in\">.<\/span>db<span class=\"hljs-built_in\">.<\/span>models <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">import<\/span> <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">Sum<\/span>\r\nBookInfo<span class=\"hljs-built_in\">.<\/span>objects<span class=\"hljs-built_in\">.<\/span>aggregate(<span class=\"hljs-keyword\" style=\"color: #000088 !important;\">Sum<\/span>(<span class=\"hljs-string\" style=\"color: #009900 !important;\">'bread'<\/span>))<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\u6ce8\u610faggregate\u7684\u8fd4\u56de\u503c\u662f\u4e00\u4e2a\u5b57\u5178\u7c7b\u578b\uff0c\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs bash has-numbering\" style=\"color: #4f4f4f !important;\">  {<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u5c5e\u6027\u540d__\u805a\u5408\u7c7b\u5c0f\u5199'<\/span>:\u503c}\r\n  \u5982:{<span class=\"hljs-string\" style=\"color: #009900 !important;\">'bread__sum'<\/span>:<span class=\"hljs-number\" style=\"color: #006666 !important;\">3<\/span>}<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\u4f7f\u7528count\u65f6\u4e00\u822c\u4e0d\u4f7f\u7528aggregate()\u8fc7\u6ee4\u5668\u3002<\/p>\n<p style=\"color: #4d4d4d;\">\u4f8b\uff1a\u67e5\u8be2\u56fe\u4e66\u603b\u6570\u3002<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs avrasm has-numbering\" style=\"color: #4f4f4f !important;\">BookInfo<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.objects<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.count<\/span>()<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\u6ce8\u610fcount\u51fd\u6570\u7684\u8fd4\u56de\u503c\u662f\u4e00\u4e2a\u6570\u5b57\u3002<\/p>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">2.3 \u6392\u5e8f<\/span><\/p>\n<p style=\"color: #4d4d4d;\">\u4f7f\u7528<span style=\"font-weight: bold;\">order_by<\/span>\u5bf9\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs bash has-numbering\" style=\"color: #4f4f4f !important;\">BookInfo.objects.all().order_by(<span class=\"hljs-string\" style=\"color: #009900 !important;\">'bread'<\/span>)  <span class=\"hljs-comment\" style=\"color: #880000 !important;\"># \u5347\u5e8f<\/span>\r\nBookInfo.objects.all().order_by(<span class=\"hljs-string\" style=\"color: #009900 !important;\">'-bread'<\/span>)  <span class=\"hljs-comment\" style=\"color: #880000 !important;\"># \u964d\u5e8f<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">2.4 \u5173\u8054\u67e5\u8be2<\/span><\/p>\n<p style=\"color: #4d4d4d;\">\u7531\u4e00\u5230\u591a\u7684\u8bbf\u95ee\u8bed\u6cd5\uff1a<\/p>\n<p style=\"color: #4d4d4d;\">\u4e00\u5bf9\u5e94\u7684\u6a21\u578b\u7c7b\u5bf9\u8c61.\u591a\u5bf9\u5e94\u7684\u6a21\u578b\u7c7b\u540d\u5c0f\u5199_set \u4f8b\uff1a<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs avrasm has-numbering\" style=\"color: #4f4f4f !important;\">b = BookInfo<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.objects<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.get<\/span>(id=<span class=\"hljs-number\" style=\"color: #006666 !important;\">1<\/span>)\r\nb<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.heroinfo<\/span>_set<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.all<\/span>()<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\u7531\u591a\u5230\u4e00\u7684\u8bbf\u95ee\u8bed\u6cd5:<\/p>\n<p style=\"color: #4d4d4d;\">\u591a\u5bf9\u5e94\u7684\u6a21\u578b\u7c7b\u5bf9\u8c61.\u591a\u5bf9\u5e94\u7684\u6a21\u578b\u7c7b\u4e2d\u7684\u5173\u7cfb\u7c7b\u5c5e\u6027\u540d \u4f8b\uff1a<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs avrasm has-numbering\" style=\"color: #4f4f4f !important;\">h = HeroInfo<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.objects<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.get<\/span>(id=<span class=\"hljs-number\" style=\"color: #006666 !important;\">1<\/span>)\r\nh<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.hbook<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\u8bbf\u95ee\u4e00\u5bf9\u5e94\u7684\u6a21\u578b\u7c7b\u5173\u8054\u5bf9\u8c61\u7684id\u8bed\u6cd5:<\/p>\n<p style=\"color: #4d4d4d;\">\u591a\u5bf9\u5e94\u7684\u6a21\u578b\u7c7b\u5bf9\u8c61.\u5173\u8054\u7c7b\u5c5e\u6027_id<\/p>\n<p style=\"color: #4d4d4d;\">\u4f8b\uff1a<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs avrasm has-numbering\" style=\"color: #4f4f4f !important;\">h = HeroInfo<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.objects<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.get<\/span>(id=<span class=\"hljs-number\" style=\"color: #006666 !important;\">1<\/span>)\r\nh<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.book<\/span>_id<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">\u5173\u8054\u8fc7\u6ee4\u67e5\u8be2<\/span><\/p>\n<p style=\"color: #4d4d4d;\">\u7531\u591a\u6a21\u578b\u7c7b\u6761\u4ef6\u67e5\u8be2\u4e00\u6a21\u578b\u7c7b\u6570\u636e:<\/p>\n<p style=\"color: #4d4d4d;\">\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p style=\"color: #4d4d4d;\">\u5173\u8054\u6a21\u578b\u7c7b\u540d\u5c0f\u5199<span style=\"font-weight: bold;\">\u5c5e\u6027\u540d<\/span>\u6761\u4ef6\u8fd0\u7b97\u7b26=\u503c<br \/>\n\u6ce8\u610f\uff1a\u5982\u679c\u6ca1\u6709\u201d__\u8fd0\u7b97\u7b26\u201d\u90e8\u5206\uff0c\u8868\u793a\u7b49\u4e8e\u3002<\/p>\n<p style=\"color: #4d4d4d;\">\u4f8b\uff1a\u67e5\u8be2\u56fe\u4e66\uff0c\u8981\u6c42\u56fe\u4e66\u82f1\u96c4\u4e3a\u201d\u5b59\u609f\u7a7a\u201d<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs livecodeserver has-numbering\" style=\"color: #4f4f4f !important;\">BookInfo.objects.<span class=\"hljs-built_in\">filter<\/span>(heroinfo__hname=<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u5b59\u609f\u7a7a'<\/span>)<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\u67e5\u8be2\u56fe\u4e66\uff0c\u8981\u6c42\u56fe\u4e66\u4e2d\u82f1\u96c4\u7684\u63cf\u8ff0\u5305\u542b\u201d\u516b\u201d<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs avrasm has-numbering\" style=\"color: #4f4f4f !important;\">BookInfo<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.objects<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.filter<\/span>(heroinfo__hcomment__contains=<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u516b'<\/span>)<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">\u7531\u4e00\u6a21\u578b\u7c7b\u6761\u4ef6\u67e5\u8be2\u591a\u6a21\u578b\u7c7b\u6570\u636e:<\/span><br \/>\n\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs fix has-numbering\" style=\"color: #4f4f4f !important;\"><span class=\"hljs-attribute\" style=\"color: #0086b3;\">\u4e00\u6a21\u578b\u7c7b\u5173\u8054\u5c5e\u6027\u540d__\u4e00\u6a21\u578b\u7c7b\u5c5e\u6027\u540d__\u6761\u4ef6\u8fd0\u7b97\u7b26<\/span>=<span class=\"hljs-string\" style=\"color: #009900 !important;\">\u503c<\/span><\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">\u6ce8\u610f\uff1a\u5982\u679c\u6ca1\u6709\u201d__\u8fd0\u7b97\u7b26\u201d\u90e8\u5206\uff0c\u8868\u793a\u7b49\u4e8e\u3002<\/span><\/p>\n<p style=\"color: #4d4d4d;\">\u4f8b\uff1a\u67e5\u8be2\u4e66\u540d\u4e3a\u201c\u5929\u9f99\u516b\u90e8\u201d\u7684\u6240\u6709\u82f1\u96c4\u3002<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs livecodeserver has-numbering\" style=\"color: #4f4f4f !important;\">HeroInfo.objects.<span class=\"hljs-built_in\">filter<\/span>(hbook__btitle=<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u5929\u9f99\u516b\u90e8'<\/span>)<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\u67e5\u8be2\u56fe\u4e66\u9605\u8bfb\u91cf\u5927\u4e8e30\u7684\u6240\u6709\u82f1\u96c4<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs avrasm has-numbering\" style=\"color: #4f4f4f !important;\">HeroInfo<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.objects<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.filter<\/span>(hbook__bread__gt=<span class=\"hljs-number\" style=\"color: #006666 !important;\">30<\/span>)<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">3. \u4fee\u6539<\/span><\/p>\n<p style=\"color: #4d4d4d;\">\u4fee\u6539\u66f4\u65b0\u6709\u4e24\u79cd\u65b9\u6cd5<\/p>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">3.1 save<\/span><\/p>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">\u4fee\u6539\u6a21\u578b\u7c7b\u5bf9\u8c61\u7684\u5c5e\u6027\uff0c\u7136\u540e\u6267\u884csave()\u65b9\u6cd5<\/span><\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs cs has-numbering\" style=\"color: #4f4f4f !important;\">hero = HeroInfo.objects.<span class=\"hljs-keyword\" style=\"color: #000088 !important;\">get<\/span>(hname=<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u732a\u516b\u6212'<\/span>)\r\nhero.hname = <span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u732a\u609f\u80fd'<\/span>\r\nhero.save()<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<li style=\"color: #999999;\">3<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">3.2 update<\/span><br \/>\n<span style=\"font-weight: bold;\">\u4f7f\u7528\u6a21\u578b\u7c7b.objects.filter().update()\uff0c\u4f1a\u8fd4\u56de\u53d7\u5f71\u54cd\u7684\u884c\u6570<\/span><\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs livecodeserver has-numbering\" style=\"color: #4f4f4f !important;\">HeroInfo.objects.<span class=\"hljs-built_in\">filter<\/span>(hname=<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u6c99\u609f\u51c0'<\/span>).update(hname=<span class=\"hljs-string\" style=\"color: #009900 !important;\">'\u6c99\u50e7'<\/span>)<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">4. \u5220\u9664<\/span><br \/>\n\u5220\u9664\u6709\u4e24\u79cd\u65b9\u6cd5<\/p>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">4.1 \u6a21\u578b\u7c7b\u5bf9\u8c61delete<\/span><\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs avrasm has-numbering\" style=\"color: #4f4f4f !important;\">hero = HeroInfo<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.objects<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.get<\/span>(id=<span class=\"hljs-number\" style=\"color: #006666 !important;\">13<\/span>)\r\nhero<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.delete<\/span>()<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">4.2 \u6a21\u578b\u7c7b.objects.filter().delete()<\/span><\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs avrasm has-numbering\" style=\"color: #4f4f4f !important;\">HeroInfo<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.objects<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.filter<\/span>(id=<span class=\"hljs-number\" style=\"color: #006666 !important;\">14<\/span>)<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.delete<\/span>()<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\n<h2 id=\"\u4e94\u67e5\u8be2\u96c6-queryset\" style=\"font-weight: bold; color: #4f4f4f;\"><a style=\"font-weight: normal; color: #4ea1db;\" name=\"t4\"><\/a><a style=\"font-weight: normal; color: #4ea1db;\" name=\"t4\"><\/a>\u4e94\u3001\u67e5\u8be2\u96c6 QuerySet<\/h2>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">1. \u6982\u5ff5<\/span><\/p>\n<p style=\"color: #4d4d4d;\">Django\u7684ORM\u4e2d\u5b58\u5728\u67e5\u8be2\u96c6\u7684\u6982\u5ff5\u3002<\/p>\n<p style=\"color: #4d4d4d;\">\u67e5\u8be2\u96c6\uff0c\u4e5f\u79f0\u67e5\u8be2\u7ed3\u679c\u96c6\u3001QuerySet\uff0c\u8868\u793a\u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u7684\u5bf9\u8c61\u96c6\u5408\u3002<\/p>\n<p style=\"color: #4d4d4d;\">\u5f53\u8c03\u7528\u5982\u4e0b\u8fc7\u6ee4\u5668\u65b9\u6cd5\u65f6\uff0cDjango\u4f1a\u8fd4\u56de\u67e5\u8be2\u96c6\uff08\u800c\u4e0d\u662f\u7b80\u5355\u7684\u5217\u8868\uff09\uff1a<\/p>\n<ul>\n<li>all()\uff1a\u8fd4\u56de\u6240\u6709\u6570\u636e\u3002<\/li>\n<li>filter()\uff1a\u8fd4\u56de\u6ee1\u8db3\u6761\u4ef6\u7684\u6570\u636e\u3002<\/li>\n<li>exclude()\uff1a\u8fd4\u56de\u6ee1\u8db3\u6761\u4ef6\u4e4b\u5916\u7684\u6570\u636e\u3002<\/li>\n<li>order_by()\uff1a\u5bf9\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\u3002<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\u5bf9\u67e5\u8be2\u96c6\u53ef\u4ee5\u518d\u6b21\u8c03\u7528\u8fc7\u6ee4\u5668\u8fdb\u884c\u8fc7\u6ee4\uff0c\u5982<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs livecodeserver has-numbering\" style=\"color: #4f4f4f !important;\">BookInfo.objects.<span class=\"hljs-built_in\">filter<\/span>(bread__gt=<span class=\"hljs-number\" style=\"color: #006666 !important;\">30<\/span>).order_by(<span class=\"hljs-string\" style=\"color: #009900 !important;\">'bpub_date'<\/span>)<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\u4e5f\u5c31\u610f\u5473\u7740\u67e5\u8be2\u96c6\u53ef\u4ee5\u542b\u6709\u96f6\u4e2a\u3001\u4e00\u4e2a\u6216\u591a\u4e2a\u8fc7\u6ee4\u5668\u3002\u8fc7\u6ee4\u5668\u57fa\u4e8e\u6240\u7ed9\u7684\u53c2\u6570\u9650\u5236\u67e5\u8be2\u7684\u7ed3\u679c\u3002<\/p>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">\u4eceSQL\u7684\u89d2\u5ea6\u8bb2\uff0c\u67e5\u8be2\u96c6\u4e0eselect\u8bed\u53e5\u7b49\u4ef7\uff0c\u8fc7\u6ee4\u5668\u50cfwhere\u3001limit\u3001order by\u5b50\u53e5<\/span>\u3002<\/p>\n<p style=\"color: #4d4d4d;\">\u5224\u65ad\u67d0\u4e00\u4e2a\u67e5\u8be2\u96c6\u4e2d\u662f\u5426\u6709\u6570\u636e\uff1a<\/p>\n<ul>\n<li>exists()\uff1a\u5224\u65ad\u67e5\u8be2\u96c6\u4e2d\u662f\u5426\u6709\u6570\u636e\uff0c\u5982\u679c\u6709\u5219\u8fd4\u56deTrue\uff0c\u6ca1\u6709\u5219\u8fd4\u56deFalse\u3002<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">2. \u4e24\u5927\u7279\u6027<\/span><\/p>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">2.1 \u60f0\u6027\u6267\u884c<\/span><br \/>\n\u521b\u5efa\u67e5\u8be2\u96c6\u4e0d\u4f1a\u8bbf\u95ee\u6570\u636e\u5e93\uff0c\u76f4\u5230\u8c03\u7528\u6570\u636e\u65f6\uff0c\u624d\u4f1a\u8bbf\u95ee\u6570\u636e\u5e93\uff0c\u8c03\u7528\u6570\u636e\u7684\u60c5\u51b5\u5305\u62ec\u8fed\u4ee3\u3001\u5e8f\u5217\u5316\u3001\u4e0eif\u5408\u7528<\/p>\n<p style=\"color: #4d4d4d;\">\u4f8b\u5982\uff0c\u5f53\u6267\u884c\u5982\u4e0b\u8bed\u53e5\u65f6\uff0c\u5e76\u672a\u8fdb\u884c\u6570\u636e\u5e93\u67e5\u8be2\uff0c\u53ea\u662f\u521b\u5efa\u4e86\u4e00\u4e2a\u67e5\u8be2\u96c6qs<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs avrasm has-numbering\" style=\"color: #4f4f4f !important;\">qs = BookInfo<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.objects<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.all<\/span>()<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\u7ee7\u7eed\u6267\u884c\u904d\u5386\u8fed\u4ee3\u64cd\u4f5c\u540e\uff0c\u624d\u771f\u6b63\u7684\u8fdb\u884c\u4e86\u6570\u636e\u5e93\u7684\u67e5\u8be2<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs lua has-numbering\" style=\"color: #4f4f4f !important;\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">for<\/span> book <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">in<\/span> qs:\r\n    <span class=\"hljs-built_in\">print<\/span>(book.btitle)<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">2.2 \u7f13\u5b58<\/span><br \/>\n\u4f7f\u7528\u540c\u4e00\u4e2a\u67e5\u8be2\u96c6\uff0c\u7b2c\u4e00\u6b21\u4f7f\u7528\u65f6\u4f1a\u53d1\u751f\u6570\u636e\u5e93\u7684\u67e5\u8be2\uff0c\u7136\u540eDjango\u4f1a\u628a\u7ed3\u679c\u7f13\u5b58\u4e0b\u6765\uff0c\u518d\u6b21\u4f7f\u7528\u8fd9\u4e2a\u67e5\u8be2\u96c6\u65f6\u4f1a\u4f7f\u7528\u7f13\u5b58\u7684\u6570\u636e\uff0c\u51cf\u5c11\u4e86\u6570\u636e\u5e93\u7684\u67e5\u8be2\u6b21\u6570\u3002<\/p>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">\u5b9e\u4f8b1<\/span>\uff1a\u5982\u4e0b\u662f\u4e24\u4e2a\u67e5\u8be2\u96c6\uff0c\u65e0\u6cd5\u91cd\u7528\u7f13\u5b58\uff0c\u6bcf\u6b21\u67e5\u8be2\u90fd\u4f1a\u4e0e\u6570\u636e\u5e93\u8fdb\u884c\u4e00\u6b21\u4ea4\u4e92\uff0c\u589e\u52a0\u4e86\u6570\u636e\u5e93\u7684\u8d1f\u8f7d\u3002<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs avrasm has-numbering\" style=\"color: #4f4f4f !important;\">from booktest<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.models<\/span> import BookInfo\r\n[book<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.id<\/span> for book <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">in<\/span> BookInfo<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.objects<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.all<\/span>()]\r\n[book<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.id<\/span> for book <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">in<\/span> BookInfo<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.objects<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.all<\/span>()]<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<li style=\"color: #999999;\">3<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">\u5b9e\u4f8b\u4e8c<\/span>\uff1a\u7ecf\u8fc7\u5b58\u50a8\u540e\uff0c\u53ef\u4ee5\u91cd\u7528\u67e5\u8be2\u96c6\uff0c\u7b2c\u4e8c\u6b21\u4f7f\u7528\u7f13\u5b58\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs avrasm has-numbering\" style=\"color: #4f4f4f !important;\">qs=BookInfo<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.objects<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.all<\/span>()\r\n[book<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.id<\/span> for book <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">in<\/span> qs]\r\n[book<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.id<\/span> for book <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">in<\/span> qs]<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<li style=\"color: #999999;\">3<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">3. \u9650\u5236\u67e5\u8be2\u96c6<\/span><br \/>\n\u53ef\u4ee5\u5bf9\u67e5\u8be2\u96c6\u8fdb\u884c\u53d6\u4e0b\u6807\u6216\u5207\u7247\u64cd\u4f5c\uff0c\u7b49\u540c\u4e8esql\u4e2d\u7684limit\u548coffset\u5b50\u53e5\u3002<br \/>\n\u6ce8\u610f\uff1a\u4e0d\u652f\u6301\u8d1f\u6570\u7d22\u5f15\u3002<\/p>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">\u5bf9\u67e5\u8be2\u96c6\u8fdb\u884c\u5207\u7247\u540e\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u67e5\u8be2\u96c6\uff0c\u4e0d\u4f1a\u7acb\u5373\u6267\u884c\u67e5\u8be2\u3002<\/span><\/p>\n<p style=\"color: #4d4d4d;\">\u5982\u679c\u83b7\u53d6\u4e00\u4e2a\u5bf9\u8c61\uff0c\u76f4\u63a5\u4f7f\u7528[0]\uff0c\u7b49\u540c\u4e8e[0:1].get()\uff0c\u4f46\u662f\u5982\u679c\u6ca1\u6709\u6570\u636e\uff0c[0]\u5f15\u53d1IndexError\u5f02\u5e38\uff0c[0:1].get()\u5982\u679c\u6ca1\u6709\u6570\u636e\u5f15\u53d1DoesNotExist\u5f02\u5e38\u3002<\/p>\n<p style=\"color: #4d4d4d;\">\u793a\u4f8b\uff1a\u83b7\u53d6\u7b2c1\u30012\u9879\uff0c\u8fd0\u884c\u67e5\u770b\u3002<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs avrasm has-numbering\" style=\"color: #4f4f4f !important;\">qs = BookInfo<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.objects<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.all<\/span>()[<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>:<span class=\"hljs-number\" style=\"color: #006666 !important;\">2<\/span>]<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\n<h2 id=\"\u516d\u7ba1\u7406\u5668manager\" style=\"font-weight: bold; color: #4f4f4f;\"><a style=\"font-weight: normal; color: #4ea1db;\" name=\"t5\"><\/a><a style=\"font-weight: normal; color: #4ea1db;\" name=\"t5\"><\/a>\u516d\u3001\u7ba1\u7406\u5668Manager<\/h2>\n<p style=\"color: #4d4d4d;\">\u7ba1\u7406\u5668\u662fDjango\u7684\u6a21\u578b\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u63a5\u53e3\uff0cDjango\u5e94\u7528\u7684\u6bcf\u4e2a\u6a21\u578b\u7c7b\u90fd\u62e5\u6709\u81f3\u5c11\u4e00\u4e2a\u7ba1\u7406\u5668\u3002<\/p>\n<p style=\"color: #4d4d4d;\">\u6211\u4eec\u5728\u901a\u8fc7\u6a21\u578b\u7c7b\u7684<span style=\"font-weight: bold;\">objects<\/span>\u5c5e\u6027\u63d0\u4f9b\u7684\u65b9\u6cd5\u64cd\u4f5c\u6570\u636e\u5e93\u65f6\uff0c\u5373\u662f\u5728\u4f7f\u7528\u4e00\u4e2a\u7ba1\u7406\u5668\u5bf9\u8c61objects\u3002\u5f53\u6ca1\u6709\u4e3a\u6a21\u578b\u7c7b\u5b9a\u4e49\u7ba1\u7406\u5668\u65f6\uff0cDjango\u4f1a\u4e3a\u6bcf\u4e00\u4e2a\u6a21\u578b\u7c7b\u751f\u6210\u4e00\u4e2a\u540d\u4e3aobjects\u7684\u7ba1\u7406\u5668\uff0c\u5b83\u662f<span style=\"font-weight: bold;\">models.Manager<\/span>\u7c7b\u7684\u5bf9\u8c61\u3002<\/p>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">\u81ea\u5b9a\u4e49\u7ba1\u7406\u5668<\/span><\/p>\n<p style=\"color: #4d4d4d;\">\u6211\u4eec\u53ef\u4ee5\u81ea\u5b9a\u4e49\u7ba1\u7406\u5668\uff0c\u5e76\u5e94\u7528\u5230\u6211\u4eec\u7684\u6a21\u578b\u7c7b\u4e0a\u3002<\/p>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">\u6ce8\u610f\uff1a\u4e00\u65e6\u4e3a\u6a21\u578b\u7c7b\u6307\u660e\u81ea\u5b9a\u4e49\u7684\u8fc7\u6ee4\u5668\u540e\uff0cDjango\u4e0d\u518d\u751f\u6210\u9ed8\u8ba4\u7ba1\u7406\u5bf9\u8c61objects\u3002<\/span><\/p>\n<p style=\"color: #4d4d4d;\">\u81ea\u5b9a\u4e49\u7ba1\u7406\u5668\u7c7b\u4e3b\u8981\u7528\u4e8e\u4e24\u79cd\u60c5\u51b5\uff1a<\/p>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">1. \u4fee\u6539\u539f\u59cb\u67e5\u8be2\u96c6\uff0c\u91cd\u5199all()\u65b9\u6cd5\u3002<\/span><\/p>\n<p style=\"color: #4d4d4d;\">1) \u6253\u5f00booktest\/models.py\u6587\u4ef6\uff0c\u5b9a\u4e49\u7c7bBookInfoManager<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs python has-numbering\" style=\"color: #4f4f4f !important;\"><span class=\"hljs-comment\" style=\"color: #880000 !important;\">#\u56fe\u4e66\u7ba1\u7406\u5668<\/span>\r\n<span class=\"hljs-class\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">class<\/span> <span class=\"hljs-title\">BookInfoManager<\/span><span class=\"hljs-params\">(models.Manager)<\/span>:<\/span>\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">def<\/span> <span class=\"hljs-title\" style=\"color: #009900 !important;\">all<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\r\n        <span class=\"hljs-comment\" style=\"color: #880000 !important;\">#\u9ed8\u8ba4\u67e5\u8be2\u672a\u5220\u9664\u7684\u56fe\u4e66\u4fe1\u606f<\/span>\r\n        <span class=\"hljs-comment\" style=\"color: #880000 !important;\">#\u8c03\u7528\u7236\u7c7b\u7684\u6210\u5458\u8bed\u6cd5\u4e3a\uff1asuper().\u65b9\u6cd5\u540d<\/span>\r\n        <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">return<\/span> super().filter(is_delete=<span class=\"hljs-keyword\" style=\"color: #000088 !important;\">False<\/span>)<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<li style=\"color: #999999;\">3<\/li>\n<li style=\"color: #999999;\">4<\/li>\n<li style=\"color: #999999;\">5<\/li>\n<li style=\"color: #999999;\">6<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">2\uff09\u5728\u6a21\u578b\u7c7bBookInfo\u4e2d\u5b9a\u4e49\u7ba1\u7406\u5668<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs r has-numbering\" style=\"color: #4f4f4f !important;\">class BookInfo(models.Model):\r\n    <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">...<\/span>\r\n    books = BookInfoManager()<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<li style=\"color: #999999;\">3<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">3\uff09\u4f7f\u7528\u65b9\u6cd5<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs avrasm has-numbering\" style=\"color: #4f4f4f !important;\">BookInfo<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.books<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.all<\/span>()<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\"><span style=\"font-weight: bold;\">2. \u5728\u7ba1\u7406\u5668\u7c7b\u4e2d\u8865\u5145\u5b9a\u4e49\u65b0\u7684\u65b9\u6cd5<\/span><\/p>\n<p style=\"color: #4d4d4d;\">1\uff09\u6253\u5f00booktest\/models.py\u6587\u4ef6\uff0c\u5b9a\u4e49\u65b9\u6cd5create\u3002<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs python has-numbering\" style=\"color: #4f4f4f !important;\"><span class=\"hljs-class\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">class<\/span> <span class=\"hljs-title\">BookInfoManager<\/span><span class=\"hljs-params\">(models.Manager)<\/span>:<\/span>\r\n    <span class=\"hljs-comment\" style=\"color: #880000 !important;\">#\u521b\u5efa\u6a21\u578b\u7c7b\uff0c\u63a5\u6536\u53c2\u6570\u4e3a\u5c5e\u6027\u8d4b\u503c<\/span>\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">def<\/span> <span class=\"hljs-title\" style=\"color: #009900 !important;\">create_book<\/span><span class=\"hljs-params\">(self, title, pub_date)<\/span>:<\/span>\r\n        <span class=\"hljs-comment\" style=\"color: #880000 !important;\">#\u521b\u5efa\u6a21\u578b\u7c7b\u5bf9\u8c61self.model\u53ef\u4ee5\u83b7\u5f97\u6a21\u578b\u7c7b<\/span>\r\n        book = self.model()\r\n        book.btitle = title\r\n        book.bpub_date = pub_date\r\n        book.bread=<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>\r\n        book.bcommet=<span class=\"hljs-number\" style=\"color: #006666 !important;\">0<\/span>\r\n        book.is_delete = <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">False<\/span>\r\n        <span class=\"hljs-comment\" style=\"color: #880000 !important;\"># \u5c06\u6570\u636e\u63d2\u5165\u8fdb\u6570\u636e\u8868<\/span>\r\n        book.save()\r\n        <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">return<\/span> book<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<li style=\"color: #999999;\">3<\/li>\n<li style=\"color: #999999;\">4<\/li>\n<li style=\"color: #999999;\">5<\/li>\n<li style=\"color: #999999;\">6<\/li>\n<li style=\"color: #999999;\">7<\/li>\n<li style=\"color: #999999;\">8<\/li>\n<li style=\"color: #999999;\">9<\/li>\n<li style=\"color: #999999;\">10<\/li>\n<li style=\"color: #999999;\">11<\/li>\n<li style=\"color: #999999;\">12<\/li>\n<li style=\"color: #999999;\">13<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">2\uff09\u4e3a\u6a21\u578b\u7c7bBookInfo\u5b9a\u4e49\u7ba1\u7406\u5668books\u8bed\u6cd5\u5982\u4e0b<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs r has-numbering\" style=\"color: #4f4f4f !important;\">class BookInfo(models.Model):\r\n      <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">...<\/span>\r\n    books = BookInfoManager()<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<li style=\"color: #999999;\">3<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">3\uff09\u8c03\u7528\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs avrasm has-numbering\" style=\"color: #4f4f4f !important;\">book=BookInfo<span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.books<\/span><span class=\"hljs-preprocessor\" style=\"color: #009900 !important;\">.create<\/span>_book(<span class=\"hljs-string\" style=\"color: #009900 !important;\">\"abc\"<\/span>,date(<span class=\"hljs-number\" style=\"color: #006666 !important;\">1980<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">1<\/span>,<span class=\"hljs-number\" style=\"color: #006666 !important;\">1<\/span>))<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">4) \u67e5\u8be2\u7ed3\u679c\u96c6\u8bbe\u7f6e( \u6b65\u9aa4 1) \u4e2d\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u5185\u5bb9\u66ff\u4ee3)<br \/>\n\u6253\u5f00booktest\/models.py\u6587\u4ef6\uff0c\u5b9a\u4e49\u65b9\u6cd5all\uff0c\u67e5\u8be2\u8fc7\u6ee4\u6389\u00a0<span style=\"font-weight: bold;\">is_delete=True<\/span>\u00a0\u7684\u7ed3\u679c<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs python has-numbering\" style=\"color: #4f4f4f !important;\"><span class=\"hljs-comment\" style=\"color: #880000 !important;\"># \u5b9a\u4e49\u7ba1\u7406\u5668\u7c7b<\/span>\r\n<span class=\"hljs-class\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">class<\/span> <span class=\"hljs-title\">BookInfoManager<\/span><span class=\"hljs-params\">(models.Manager)<\/span>:<\/span>\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"color: #000088 !important;\">def<\/span> <span class=\"hljs-title\" style=\"color: #009900 !important;\">all<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\r\n        <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">return<\/span> self.filter(is_delete=<span class=\"hljs-keyword\" style=\"color: #000088 !important;\">False<\/span>)<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<li style=\"color: #999999;\">3<\/li>\n<li style=\"color: #999999;\">4<\/li>\n<\/ul>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs r has-numbering\" style=\"color: #4f4f4f !important;\"><span class=\"hljs-comment\" style=\"color: #880000 !important;\"># \u5b9a\u4e49\u56fe\u4e66\u6a21\u578b\u7c7bBookInfo<\/span>\r\nclass BookInfo(models.Model):\r\n    <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">...<\/span>\r\n    <span class=\"hljs-keyword\" style=\"color: #000088 !important;\">...<\/span>\r\n    <span class=\"hljs-comment\" style=\"color: #880000 !important;\"># \u8865\u5145\u81ea\u5b9a\u4e49\u5230\u7ba1\u7406\u5668\u5bf9\u8c61\uff0c\u6a21\u578b\u7c7b\u5c06\u4e0d\u4f1a\u518d\u5b58\u5728objects<\/span>\r\n    query = BookInfoManager()\r\n<\/code><\/pre>\n<ul class=\"pre-numbering\">\n<li style=\"color: #999999;\">1<\/li>\n<li style=\"color: #999999;\">2<\/li>\n<li style=\"color: #999999;\">3<\/li>\n<li style=\"color: #999999;\">4<\/li>\n<li style=\"color: #999999;\">5<\/li>\n<li style=\"color: #999999;\">6<\/li>\n<li style=\"color: #999999;\">7<\/li>\n<\/ul>\n<p style=\"color: #4d4d4d;\">\u67e5\u8be2\u7684\u65b9\u6cd5\u4e3a\uff1a<br \/>\n(\u6ce8\u610f\uff1a\u8fd9\u91cc\u63d0\u524d\u4fee\u6539\u4e86\u6570\u636e\u5e93\u4e2d\u00a0<span style=\"font-weight: bold;\">btitle=\u2019\u7b11\u50b2\u6c5f\u6e56\u2019<\/span>\u00a0\u7684\u00a0<span style=\"font-weight: bold;\">is_delete=1<\/span>\u00a0)<\/p>\n<pre class=\"prettyprint\" style=\"color: #000000;\"><code class=\"hljs handlebars has-numbering\" style=\"color: #4f4f4f !important;\"><span class=\"xml\">&gt;&gt;&gt; BookInfo.query.all()\r\n<span class=\"hljs-tag\" style=\"color: #006666 !important;\">&lt;<span class=\"hljs-title\" style=\"color: #4f4f4f !important;\">QuerySet<\/span> [&lt;<span class=\"hljs-attribute\" style=\"color: #4f4f4f !important;\">BookInfo:<\/span> \u5c04\u96d5\u82f1\u96c4\u4f20&gt;<\/span>, <span class=\"hljs-tag\" style=\"color: #006666 !important;\">&lt;<span class=\"hljs-title\" style=\"color: #4f4f4f !important;\">BookInfo:<\/span> \u5929\u9f99\u516b\u90e8&gt;<\/span>, <span class=\"hljs-tag\" style=\"color: #006666 !important;\">&lt;<span class=\"hljs-title\" style=\"color: #4f4f4f !important;\">BookInfo:<\/span> \u96ea\u5c71\u98de\u72d0&gt;<\/span>,\u6e38\u8bb0&gt;]&gt;<\/span><\/code><\/pre>\n<\/div>\n<\/div>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>from: https:\/\/blog.csdn.net\/yanpenggong\/article\/details\/82316514 ORM\u6846\u67b6 O\u662fobject\uff0c\u4e5f\u5c31\u7c7b\u5bf9\u8c61\u7684\u610f\u601d\uff0cR\u662frelation\uff0c\u7ffb\u8bd1\u6210\u4e2d\u6587\u662f\u5173\u7cfb\uff0c\u4e5f\u5c31\u662f\u5173\u7cfb\u6570\u636e\u5e93\u4e2d\u6570\u636e &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-116","post","type-post","status-publish","format-standard","hentry","category-django"],"_links":{"self":[{"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=116"}],"version-history":[{"count":1,"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/116\/revisions"}],"predecessor-version":[{"id":117,"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/116\/revisions\/117"}],"wp:attachment":[{"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=116"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.jichangqing.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}