﻿{"id":636,"date":"2011-08-09T14:16:29","date_gmt":"2011-08-09T06:16:29","guid":{"rendered":"http:\/\/nick.workao.org\/?p=636"},"modified":"2021-11-17T11:34:04","modified_gmt":"2021-11-17T03:34:04","slug":"django%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0-%e8%bf%9b%e9%98%b6-%e6%a8%a1%e5%9e%8b","status":"publish","type":"post","link":"http:\/\/nick.txtcc.com\/index.php\/python\/636","title":{"rendered":"django\u5b66\u4e60\u7b14\u8bb0\u2014\u2014\u8fdb\u9636\u2014\u2014\u6a21\u578b"},"content":{"rendered":"<h2>\u65b0\u5efa\u5e94\u7528\u7a0b\u5e8f<\/h2>\n<p>\u5728\u5f00\u59cb\u4e4b\u524d\u9700\u8981\u65b0\u5efa\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u3002\u5728\u8fd9\u4e4b\u524d\u6211\u4eec\u5df2\u7ecf\u521b\u5efa\u4e86 project , \u90a3\u4e48 project \u548c app \u4e4b\u95f4\u5230\u5e95\u6709\u4ec0\u4e48\u4e0d\u540c\u5462\uff1f \u5b83\u4eec\u7684\u533a\u522b\u5c31\u662f\u4e00\u4e2a\u662f\u914d\u7f6e\u53e6\u4e00\u4e2a\u662f\u4ee3\u7801\u3002\u4e5f\u5c31\u662f\u8bf4\u4e00\u4e2aapp\u662f\u4e00\u5957Django\u529f\u80fd\u7684\u96c6\u5408\uff0c\u901a\u5e38\u5305\u62ec\u6a21\u578b\u548c\u89c6\u56fe\uff0c\u6309Python\u7684\u5305\u7ed3\u6784\u7684\u65b9\u5f0f\u5b58\u5728\u3002<\/p>\n<p>\u5728\u201c mysite\u201c \u9879\u76ee\u6587\u4ef6\u4e0b\u8f93\u5165\u4e0b\u9762\u7684\u547d\u4ee4\u6765\u521b\u5efa\u201c books\u201c app\npython manage.py startapp books\n\u8fd9\u4e2a\u547d\u4ee4\u5728mysite\u76ee\u5f55\u4e0b\u65b0\u5efa\u4e86\u4e00\u4e2abook\u7684app\u5e94\u7528\u7a0b\u5e8f<\/p>\n<h2>\u6a21\u578b<\/h2>\n<p>\u6a21\u578b\u521b\u5efa\u7684\u6d41\u7a0b\n<strong>1\uff1a\u5728settings.py\u4e2d\u914d\u7f6e\u6570\u636e\u5e93\u4fe1\u606f<\/strong>\n<strong>2\uff1a\u5728app\u4e2dmodels.py\u6587\u4ef6\u91cc\u5b9a\u4e49\u6a21\u578b<\/strong><\/p>\n<div>\n<div>from django.db import models\n\nclass Publisher(models.Model):\nname = models.CharField(max_length=30)\naddress = models.CharField(max_length=50)\ncity = models.CharField(max_length=60)\nstate_province = models.CharField(max_length=30)\ncountry = models.CharField(max_length=50)\nwebsite = models.URLField()\n\nclass Author(models.Model):\nfirst_name = models.CharField(max_length=30)\nlast_name = models.CharField(max_length=40)\nemail = models.EmailField()\n\nclass Book(models.Model):\ntitle = models.CharField(max_length=100)\nauthors = models.ManyToManyField(Author)\npublisher = models.ForeignKey(Publisher)\npublication_date = models.DateField()<\/div>\n<\/div>\n<p>\u6bcf\u4e2a\u6a21\u578b\u76f8\u5f53\u4e8e\u5355\u4e2a\u6570\u636e\u5e93\u8868\uff0c\u6bcf\u4e2a\u5c5e\u6027\u4e5f\u662f\u8fd9\u4e2a\u8868\u4e2d\u7684\u4e00\u4e2a\u5b57\u6bb5<\/p>\n<p><strong>3:\u6a21\u578b\u5b89\u88c5<\/strong>\n\u7f16\u8f91 settings.py \u6587\u4ef6\uff0c \u627e\u5230 INSTALLED_APPS \u8bbe\u7f6e,INSTALLED_APPS \u544a\u8bc9 Django \u9879\u76ee\u54ea\u4e9b app \u5904\u4e8e\u6fc0\u6d3b\u72b6\u6001.<\/p>\n<div>\n<div>INSTALLED_APPS = (\n# &#8216;django.contrib.auth&#8217;,\n# &#8216;django.contrib.contenttypes&#8217;,\n# &#8216;django.contrib.sessions&#8217;,\n# &#8216;django.contrib.sites&#8217;,\n&#8216;mysite.books&#8217;,\n)<\/div>\n<\/div>\n<p>\u9996\u5148\uff0c\u7528\u4e0b\u9762\u7684\u547d\u4ee4\u5bf9\u6821\u9a8c\u6a21\u578b\u7684\u6709\u6548\u6027\uff1a\npython manage.py validate\n\u6ca1\u95ee\u9898\u4e4b\u540e\u8fd0\u884c\u4e0b\u9762\u7684\u547d\u4ee4\u6765\u751f\u6210 CREATE TABLE \u8bed\u53e5\uff1a\npython manage.py sqlall books\n\u7136\u540e\u901a\u8fc7\u4e0b\u5217\u547d\u4ee4\u6267\u884cSQL\npython manage.py syncdb\nsyncdb \u547d\u4ee4\u662f\u540c\u6b65\u4f60\u7684\u6a21\u578b\u5230\u6570\u636e\u5e93\u7684\u4e00\u4e2a\u7b80\u5355\u65b9\u6cd5\u3002 \u5b83\u4f1a\u6839\u636e INSTALLED_APPS \u91cc\u8bbe\u7f6e\u7684app\u6765\u68c0\u67e5\u6570\u636e\u5e93\uff0c \u5982\u679c\u8868\u4e0d\u5b58\u5728\uff0c\u5b83\u5c31\u4f1a\u521b\u5efa\u5b83\u3002 \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c syncdb \u5e76 \u4e0d\u80fd \u540c\u6b65\u6a21\u578b\u7684\u4fee\u6539\u5230\u6570\u636e\u5e93\u3002 \uff08\u672c\u7ae0\u7684\u6700\u540e\u5c06\u8be6\u7ec6\u8ba8\u8bba\u4fee\u6539\u6570\u636e\u5e93\u7684\u67b6\u6784\uff09<\/p>\n<h2>\u57fa\u672c\u6570\u636e\u8bbf\u95ee<\/h2>\n<div>\n<div>from books.models import Publisher\np1 = Publisher(name=&#8217;Apress&#8217;, address=&#8217;2855 Telegraph Avenue&#8217;,\ncity=&#8217;Berkeley&#8217;, state_province=&#8217;CA&#8217;, country=&#8217;U.S.A.&#8217;,\nwebsite=&#8217;http:\/\/www.apress.com\/&#8217;)\np1.save()\npublisher_list = Publisher.objects.all()<\/div>\n<\/div>\n<p>\u9996\u5148\uff0c\u5bfc\u5165Publisher\u6a21\u578b\u7c7b\uff0c \u901a\u8fc7\u8fd9\u4e2a\u7c7b\u6211\u4eec\u53ef\u4ee5\u4e0e\u5305\u542b \u51fa\u7248\u793e \u7684\u6570\u636e\u8868\u8fdb\u884c\u4ea4\u4e92\u3002\n\u63a5\u7740\uff0c\u521b\u5efa\u4e00\u4e2a\u201c Publisher\u201c \u7c7b\u7684\u5b9e\u4f8b\u5e76\u8bbe\u7f6e\u4e86\u5b57\u6bb5\u201c name, address\u201c \u7b49\u7684\u503c\u3002\n\u8c03\u7528\u8be5\u5bf9\u8c61\u7684 save() \u65b9\u6cd5\uff0c\u5c06\u5bf9\u8c61\u4fdd\u5b58\u5230\u6570\u636e\u5e93\u4e2d\u3002 Django \u4f1a\u5728\u540e\u53f0\u6267\u884c\u4e00\u6761 INSERT \u8bed\u53e5\u3002\n\u6700\u540e\uff0c\u4f7f\u7528\u201c Publisher.objects\u201c \u5c5e\u6027\u4ece\u6570\u636e\u5e93\u53d6\u51fa\u51fa\u7248\u5546\u7684\u4fe1\u606f\uff0c\u8fd9\u4e2a\u5c5e\u6027\u53ef\u4ee5\u8ba4\u4e3a\u662f\u5305\u542b\u51fa\u7248\u5546\u7684\u8bb0\u5f55\u96c6\u3002 \u8fd9\u4e2a\u5c5e\u6027\u6709\u8bb8\u591a\u65b9\u6cd5\uff0c\u8fd9\u91cc\u5148\u4ecb\u7ecd\u8c03\u7528\u201c Publisher.objects.all()\u201c \u65b9\u6cd5\u83b7\u53d6\u6570\u636e\u5e93\u4e2d\u201c Publisher\u201c \u7c7b\u7684\u6240\u6709\u5bf9\u8c61\u3002\u8fd9\u4e2a\u64cd\u4f5c\u7684\u5e55\u540e\uff0cDjango\u6267\u884c\u4e86\u4e00\u6761SQL \u201c SELECT\u201c \u8bed\u53e5\u3002<\/p>\n<p>\u6ce8\u610f\uff1a\u5f53\u4f60\u4f7f\u7528Django modle API\u521b\u5efa\u5bf9\u8c61\u65f6Django\u5e76\u672a\u5c06\u5bf9\u8c61\u4fdd\u5b58\u81f3\u6570\u636e\u5e93\u5185\uff0c\u9664\u975e\u4f60\u8c03\u7528\u201c save()\u201c \u65b9\u6cd5\n\u5982\u679c\u9700\u8981\u4e00\u6b65\u5b8c\u6210\u5bf9\u8c61\u7684\u521b\u5efa\u4e0e\u5b58\u50a8\u81f3\u6570\u636e\u5e93\uff0c\u5c31\u4f7f\u7528\u201c objects.create()\u201c \u65b9\u6cd5<\/p>\n<div>\n<div>p1 = Publisher.objects.create(name=&#8217;Apress&#8217;,\naddress=&#8217;2855 Telegraph Avenue&#8217;,\ncity=&#8217;Berkeley&#8217;, state_province=&#8217;CA&#8217;, country=&#8217;U.S.A.&#8217;,\nwebsite=&#8217;http:\/\/www.apress.com\/&#8217;)<\/div>\n<\/div>\n<p><strong>\u67e5\u8be2\u6570\u636e<\/strong>\nPublisher.objects.all() \u67e5\u8be2\u51fa\u8868\u7684\u6240\u6709\u6570\u636e\n<strong>\u6570\u636e\u8fc7\u6ee4<\/strong>\nPublisher.objects.filter(name=\u2019Apress\u2019) \u67e5\u8be2\u51faname\u4e3aapress\u503c\u7684\u8bb0\u5f55\nPublisher.objects.filter(country=\u201dU.S.A.\u201d, state_province=\u201dCA\u201d)\n\u5305\u542b\u6027\u67e5\u627e\nPublisher.objects.filter(name__contains=\u201dpress\u201d) \u67e5\u627e\u51faname\u4e2d\u5305\u542bpress\u7684\u8bb0\u5f55\u3002\u5728 name \u548c contains \u4e4b\u95f4\u6709\u53cc\u4e0b\u5212\u7ebf\u3002\u5176\u4ed6\u7684\u4e00\u4e9b\u67e5\u627e\u7c7b\u578b\u6709\uff1a icontains (\u5927\u5c0f\u5199\u65e0\u5173\u7684 LIKE ), startswith \u548c endswith , \u8fd8\u6709 range\n<strong>\u83b7\u53d6\u803d\u6401\u6570\u636e<\/strong>\nPublisher.objects.get(name=\u201dApress\u201d) \u5982\u679c\u7ed3\u679c\u662f\u591a\u4e2a\u5bf9\u8c61\uff0c\u4f1a\u5bfc\u81f4\u629b\u51fa\u5f02\u5e38\uff1a\u5982\u679c\u67e5\u8be2\u6ca1\u6709\u8fd4\u56de\u7ed3\u679c\u4e5f\u4f1a\u629b\u51fa\u5f02\u5e38<\/p>\n<h2>\u6392\u5e8f<\/h2>\n<p>Publisher.objects.order_by(\u201cstate_province\u201d, \u201caddress\u201d)\n\u9006\u5411\u6392\u5e8f\nPublisher.objects.order_by(\u201c-name\u201d)\n\u8fde\u9501\nPublisher.objects.filter(country=\u201dU.S.A.\u201d).order_by(\u201c-name\u201d)<\/p>\n<p><strong>\u9650\u5236\u8fd4\u56de\u6570\u91cf<\/strong>\nPublisher.objects.order_by(\u2018name\u2019)[0] \u8fd4\u56de\u7b2c\u4e00\u4e2a\n\u76f8\u5f53\u4e8e\nSELECT id, name, address, city, state_province, country, website\nFROM books_publisher\nORDER BY name\nLIMIT 1;\n\u53d6\u51fa\u7279\u5b9a\u5b50\u96c6\nPublisher.objects.order_by(\u2018name\u2019)[0:2]\n\u7c7b\u4f3c\u4e8e\nSELECT id, name, address, city, state_province, country, website\nFROM books_publisher\nORDER BY name\nLIMIT 0,2;<\/p>\n<p><strong>\u66f4\u65b0\u6570\u636e<\/strong>\n\u66f4\u65b0\u5355\u4e2a\u5b57\u6bb5\nPublisher.objects.filter(id=52).update(name=\u2019Apress Publishing\u2019)\n\u7c7b\u4f3c\u4e8e\nUPDATE books_publisher\nSET name = \u2018Apress Publishing\u2019\nWHERE id = 52;<\/p>\n<p>\u66f4\u65b0\u591a\u4e2a\u5b57\u6bb5\np = Publisher.objects.get(name=\u2019Apress\u2019)\np.name = \u2018Apress Publishing\u2019\np.save()\n\u7c7b\u4f3c\u4e8e\nSELECT id, name, address, city, state_province, country, website\nFROM books_publisher\nWHERE name = \u2018Apress\u2019;<\/p>\n<p>UPDATE books_publisher SET\nname = \u2018Apress Publishing\u2019,\naddress = \u20182855 Telegraph Ave.\u2019,\ncity = \u2018Berkeley\u2019,\nstate_province = \u2018CA\u2019,\ncountry = \u2018U.S.A.\u2019,\nwebsite = \u2018<a href=\"http:\/\/www.apress.com\">http:\/\/www.apress.com<\/a>\u2019\nWHERE id = 52;\n\u5728\u8fd9\u4e2a\u4f8b\u5b50\u91cc\u6211\u4eec\u53ef\u4ee5\u770b\u5230Django\u7684save()\u65b9\u6cd5\u66f4\u65b0\u4e86\u4e0d\u4ec5\u4ec5\u662fname\u5217\u7684\u503c\uff0c\u8fd8\u6709\u66f4\u65b0\u4e86\u6240\u6709\u7684\u5217\u3002 \u82e5name\u4ee5\u5916\u7684\u5217\u6709\u53ef\u80fd\u4f1a\u88ab\u5176\u4ed6\u7684\u8fdb\u7a0b\u6240\u6539\u52a8\u7684\u60c5\u51b5\u4e0b\uff0c\u53ea\u66f4\u6539name\u5217\u663e\u7136\u662f\u66f4\u52a0\u660e\u667a\u7684<\/p>\n<p><strong>\u5220\u9664\u6570\u636e<\/strong>\np = Publisher.objects.get(name=\u201dO\u2019Reilly\u201d)\np.delete()\n\u5220\u9664\u591a\u6761\u8bb0\u5f55\nPublisher.objects.filter(country=\u2019USA\u2019).delete()<\/p>\n<h2>\u6307\u5b9a\u6a21\u578b\u7684\u7f3a\u7701\u6392\u5e8f\u65b9\u5f0f<\/h2>\n<div>\n<div>class Publisher(models.Model):\nname = models.CharField(max_length=30)\naddress = models.CharField(max_length=50)\ncity = models.CharField(max_length=60)\nstate_province = models.CharField(max_length=30)\ncountry = models.CharField(max_length=50)\nwebsite = models.URLField()\n\ndef __unicode__(self):\nreturn self.name\n\nclass Meta:\nordering = [&#8216;name&#8217;]<\/div>\n<\/div>\n<p>class Meta\uff0c\u5185\u5d4c\u4e8e Publisher \u8fd9\u4e2a\u7c7b\u7684\u5b9a\u4e49\u4e2d\uff08\u5982\u679c class Publisher \u662f\u9876\u683c\u7684\uff0c\u90a3\u4e48 class Meta \u5728\u5b83\u4e4b\u4e0b\u8981\u7f29\u8fdb4\u4e2a\u7a7a\u683c\uff0d\uff0d\u6309 Python \u7684\u4f20\u7edf \uff09\u3002\u4f60\u53ef\u4ee5\u5728\u4efb\u610f\u4e00\u4e2a \u6a21\u578b \u7c7b\u4e2d\u4f7f\u7528 Meta \u7c7b\uff0c\u6765\u8bbe\u7f6e\u4e00\u4e9b\u4e0e\u7279\u5b9a\u6a21\u578b\u76f8\u5173\u7684\u9009\u9879<\/p>","protected":false},"excerpt":{"rendered":"<p>\u65b0\u5efa\u5e94\u7528\u7a0b\u5e8f \u5728\u5f00\u59cb\u4e4b\u524d\u9700\u8981\u65b0\u5efa\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u3002\u5728\u8fd9\u4e4b\u524d\u6211\u4eec\u5df2\u7ecf\u521b\u5efa\u4e86 project , \u90a3\u4e48 project \u548c app \u4e4b\u95f4\u5230\u5e95\u6709\u4ec0\u4e48\u4e0d\u540c\u5462\uff1f \u5b83\u4eec\u7684\u533a\u522b\u5c31\u662f\u4e00\u4e2a\u662f\u914d\u7f6e\u53e6\u4e00\u4e2a\u662f\u4ee3\u7801\u3002\u4e5f\u5c31\u662f\u8bf4\u4e00\u4e2aa&#46;&#46;&#46;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[274],"tags":[310,315],"class_list":["post-636","post","type-post","status-publish","format-standard","hentry","category-python","tag-django-2","tag-model"],"_links":{"self":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/636","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/comments?post=636"}],"version-history":[{"count":3,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/636\/revisions"}],"predecessor-version":[{"id":2227,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/636\/revisions\/2227"}],"wp:attachment":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/media?parent=636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/categories?post=636"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/tags?post=636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}