{"id":188,"date":"2021-10-18T22:39:48","date_gmt":"2021-10-18T14:39:48","guid":{"rendered":"http:\/\/47.98.165.52\/?p=188"},"modified":"2022-06-22T21:55:58","modified_gmt":"2022-06-22T13:55:58","slug":"djangoorm%e5%8f%8d%e5%90%91%e7%94%9f%e6%88%90modles","status":"publish","type":"post","link":"\/?p=188","title":{"rendered":"djangoORM\u53cd\u5411\u751f\u6210modles"},"content":{"rendered":"<h2>\u95ee\u9898\uff1a\u5047\u8bbe\u5df2\u6709\u4e00\u4e2a\u6570\u636e\u5e93\uff0c\u73b0\u8981\u7528Django\u9879\u76ee\uff0c\u5bf9\u8be5\u6570\u636e\u5e93\u8fdb\u884c\u8fde\u63a5\uff0c\u8fdb\u884cORM\u64cd\u4f5c<\/h2>\n<h2>\u6761\u4ef6\uff1a<\/h2>\n<h4>1. \u6570\u636e\u5e93\uff08PGsql\uff09:<\/h4>\n<ul>\n<li>database:industrial_map_db<\/li>\n<li>table: industrial_map.company<br \/>\n<code>\u5c31\u4e24\u6761\u6570\u636e\uff0c\u610f\u601d\u610f\u601d~<\/code><br \/>\n<img src=\"https:\/\/img2020.cnblogs.com\/blog\/2458170\/202110\/2458170-20211012163715935-392389940.png\" alt=\"\" \/><\/p>\n<h4>2. Django\u9879\u76ee<\/h4>\n<\/li>\n<li>\u60f3\u628a\u521a\u521a\u7684\u8868\u6620\u5c04\u5230\u8be5\u9879\u76ee\u7684models\u4e2d\uff0c\u5e76\u91c7\u7528\u76f8\u540c\u7684\u6570\u636e\u5e93\u8fdb\u884cORM\u64cd\u4f5c<\/li>\n<li><img src=\"https:\/\/img2020.cnblogs.com\/blog\/2458170\/202110\/2458170-20211012163820401-574617222.png\" alt=\"\" \/><\/li>\n<\/ul>\n<h2>1.\u5148\u8fde\u63a5\u6570\u636e\u5e93<\/h2>\n<p><strong>\u4e24\u4e2a\u9879\u76ee\u5982\u679c\u8fde\u63a5\u76f8\u540c\u7684\u6570\u636e\u5e93\uff0c\u8be5\u6570\u636e\u662f\u53ef\u4ee5\u5171\u4eab\u7684\uff0c\u4f46\u524d\u63d0\u662f\u6709\u76f8\u5e94\u7684models<\/strong><br \/>\n\u6bd4\u5982B\u9879\u76ee\u7684\u8868\u672c\u65e0\u6570\u636e\uff0c\u8fde\u63a5\u540e\uff0c\u64cd\u4f5c\u6570\u636e\u8bd5\u4e00\u8bd5\uff1a<\/p>\n<pre><code class=\"language-bash\">C:\\Users\\Administrator\\Desktop\\\u534e\u793e\u6d4b\u8bd5\\rqapiv2&gt;python manage.py shell\nPython 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32\nType &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.\n(InteractiveConsole)\n>&gt;&gt; from aqcapi.models import Base\n>&gt;&gt; Base.objects.all()\n&lt;QuerySet [&lt;Base: 1&gt;, &lt;Base: 2&gt;, &lt;Base: 3&gt;, &lt;Base: 4&gt;, &lt;Base: 5&gt;, &lt;Base: 6&gt;, &lt;Base: 7&gt;, &lt;Base: 8&gt;, &lt;Base: 9&gt;, &lt;Base: 10&gt;, &lt;Base: 108&gt;, &lt;Base: 109&gt;, &lt;Base: 1168&gt;, &lt;Base: 11&gt;, &lt;Base: 12&gt;, &lt;Base:\n 13&gt;, &lt;Base: 14&gt;, &lt;Base: 15&gt;, &lt;Base: 16&gt;, &lt;Base: 17&gt;, &#039;...(remaining elements truncated)...&#039;]&gt;<\/code><\/pre>\n<h2>2.\u53cd\u5411\u751f\u6210models<\/h2>\n<p><code>python manage.py inspectdb<\/code>\u8fd9\u4e2a\u64cd\u4f5c\u662f\u5c06\u6574\u4e2aA\u9879\u76ee\u7684\u8868\u79fb\u690d\u8fc7\u6765\u4e86<br \/>\n<code>python manage.py inspectdb &gt; APP\u540d\/models.py <\/code>\u6307\u5b9a\u751f\u6210APP\u540d\u4e0b\u9762\u7684models.py<\/p>\n<pre><code class=\"language-bash\">C:\\Users\\Administrator\\Desktop\\\u534e\u793e\u6d4b\u8bd5\\rqapiv2&gt;python manage.py inspectdb\n# This is an auto-generated Django model module.\n# You&#039;ll have to do the following manually to clean this up:\n#   * Rearrange models&#039; order\n#   * Make sure each model has one field with primary_key=True\n#   * Make sure each ForeignKey and OneToOneField has `on_delete` set to the desired behavior\n#   * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table\n# Feel free to rename the models, but don&#039;t rename db_table values or field names.\nfrom django.db import models\n\nclass AuthGroup(models.Model):\n    name = models.CharField(unique=True, max_length=150)\n\n    class Meta:\n        managed = False\n        db_table = &#039;auth_group&#039;\n\nclass AuthGroupPermissions(models.Model):\n    id = models.BigAutoField(primary_key=True)\n    group = models.ForeignKey(AuthGroup, models.DO_NOTHING)\n    permission = models.ForeignKey(&#039;AuthPermission&#039;, models.DO_NOTHING)\n\n    class Meta:\n        managed = False\n        db_table = &#039;auth_group_permissions&#039;\n        unique_together = ((&#039;group&#039;, &#039;permission&#039;),)\n\nclass AuthPermission(models.Model):\n    name = models.CharField(max_length=255)\n    content_type = models.ForeignKey(&#039;DjangoContentType&#039;, models.DO_NOTHING)\n    codename = models.CharField(max_length=100)\n\n    class Meta:\n        managed = False\n        db_table = &#039;auth_permission&#039;\n        unique_together = ((&#039;content_type&#039;, &#039;codename&#039;),)\n\nclass AuthUser(models.Model):\n    password = models.CharField(max_length=128)\n    last_login = models.DateTimeField(blank=True, null=True)\n    is_superuser = models.BooleanField()\n    username = models.CharField(unique=True, max_length=150)\n    first_name = models.CharField(max_length=150)\n    last_name = models.CharField(max_length=150)\n    email = models.CharField(max_length=254)\n    is_staff = models.BooleanField()\n    is_active = models.BooleanField()\n    date_joined = models.DateTimeField()\n\n    class Meta:\n        managed = False\n        db_table = &#039;auth_user&#039;\n\nclass AuthUserGroups(models.Model):\n    id = models.BigAutoField(primary_key=True)\n    user = models.ForeignKey(AuthUser, models.DO_NOTHING)\n    group = models.ForeignKey(AuthGroup, models.DO_NOTHING)\n\n    class Meta:\n        managed = False\n        db_table = &#039;auth_user_groups&#039;\n        unique_together = ((&#039;user&#039;, &#039;group&#039;),)\n\nclass AuthUserUserPermissions(models.Model):\n    id = models.BigAutoField(primary_key=True)\n    user = models.ForeignKey(AuthUser, models.DO_NOTHING)\n    permission = models.ForeignKey(AuthPermission, models.DO_NOTHING)\n\n    class Meta:\n        managed = False\n        db_table = &#039;auth_user_user_permissions&#039;\n        unique_together = ((&#039;user&#039;, &#039;permission&#039;),)\n\nclass Company(models.Model):\n    id = models.IntegerField(primary_key=True)\n    name = models.CharField(max_length=20)\n    age = models.IntegerField()\n    address = models.CharField(max_length=25, blank=True, null=True)\n    salary = models.DecimalField(max_digits=18, decimal_places=2, blank=True, null=True)\n\n    class Meta:\n        managed = False\n        db_table = &#039;company&#039;\n\nclass DjangoAdminLog(models.Model):\n    action_time = models.DateTimeField()\n    object_id = models.TextField(blank=True, null=True)\n    object_repr = models.CharField(max_length=200)\n    action_flag = models.SmallIntegerField()\n    change_message = models.TextField()\n    content_type = models.ForeignKey(&#039;DjangoContentType&#039;, models.DO_NOTHING, blank=True, null=True)\n    user = models.ForeignKey(AuthUser, models.DO_NOTHING)\n\n    class Meta:\n        managed = False\n        db_table = &#039;django_admin_log&#039;\n\nclass DjangoApschedulerDjangojob(models.Model):\n    id = models.CharField(primary_key=True, max_length=255)\n    next_run_time = models.DateTimeField(blank=True, null=True)\n    job_state = models.BinaryField()\n\n    class Meta:\n        managed = False\n        db_table = &#039;django_apscheduler_djangojob&#039;\n\nclass DjangoApschedulerDjangojobexecution(models.Model):\n    id = models.BigAutoField(primary_key=True)\n    status = models.CharField(max_length=50)\n    run_time = models.DateTimeField()\n    duration = models.DecimalField(max_digits=15, decimal_places=2, blank=True, null=True)\n    finished = models.DecimalField(max_digits=15, decimal_places=2, blank=True, null=True)\n    exception = models.CharField(max_length=1000, blank=True, null=True)\n    traceback = models.TextField(blank=True, null=True)\n    job = models.ForeignKey(DjangoApschedulerDjangojob, models.DO_NOTHING)\n\n    class Meta:\n        managed = False\n        db_table = &#039;django_apscheduler_djangojobexecution&#039;\n\nclass DjangoContentType(models.Model):\n    app_label = models.CharField(max_length=100)\n    model = models.CharField(max_length=100)\n\n    class Meta:\n        managed = False\n        db_table = &#039;django_content_type&#039;\n        unique_together = ((&#039;app_label&#039;, &#039;model&#039;),)\n\nclass DjangoMigrations(models.Model):\n    id = models.BigAutoField(primary_key=True)\n    app = models.CharField(max_length=255)\n    name = models.CharField(max_length=255)\n    applied = models.DateTimeField()\n\n    class Meta:\n        managed = False\n        db_table = &#039;django_migrations&#039;\n\nclass DjangoSession(models.Model):\n    session_key = models.CharField(primary_key=True, max_length=40)\n    session_data = models.TextField()\n    expire_date = models.DateTimeField()\n\n    class Meta:\n        managed = False\n        db_table = &#039;django_session&#039;\n\nclass EoiasCompanyBaseInfoExtend(models.Model):\n    id = models.BigAutoField(primary_key=True)\n    company_name = models.TextField()\n    company_name_alias = models.TextField(blank=True, null=True)\n    legal_person = models.TextField(blank=True, null=True)\n    province = models.TextField(blank=True, null=True)\n    city = models.TextField(blank=True, null=True)\n    county = models.TextField(blank=True, null=True)\n    website_url = models.TextField(blank=True, null=True)\n    phone_number = models.TextField(blank=True, null=True)\n    email = models.TextField(blank=True, null=True)\n    end_date = models.TextField(blank=True, null=True)\n    ent_type = models.TextField(blank=True, null=True)\n    status = models.TextField(blank=True, null=True)\n    industries = models.TextField(blank=True, null=True)\n    regist_capital = models.TextField(blank=True, null=True)\n    paid_capital = models.TextField(blank=True, null=True)\n    insured_persons_number = models.TextField(blank=True, null=True)\n    staff_number = models.TextField(blank=True, null=True)\n    credit_code = models.TextField(blank=True, null=True)\n    taxpayer_identification_number = models.TextField(blank=True, null=True)\n    registration_number = models.TextField(blank=True, null=True)\n    import_and_export_enterprise_code = models.TextField(blank=True, null=True)\n    org_code = models.TextField(blank=True, null=True)\n    check_date = models.DateTimeField(blank=True, null=True)\n    term_start_end = models.TextField(blank=True, null=True)\n    addr = models.TextField(blank=True, null=True)\n    company_kind = models.TextField(blank=True, null=True)\n    company_en_name = models.TextField(blank=True, null=True)\n    company_origin_name = models.TextField(blank=True, null=True)\n    start_date = models.DateTimeField(blank=True, null=True)\n    belong_org = models.TextField(blank=True, null=True)\n    scope = models.TextField(blank=True, null=True)\n    company_profile = models.TextField(blank=True, null=True)\n    is_on_stock = models.TextField(blank=True, null=True)\n    listed_status = models.TextField(blank=True, null=True)\n    stock_number = models.TextField(blank=True, null=True)\n    company_label = models.TextField(blank=True, null=True)\n    taxpayers_type = models.TextField(blank=True, null=True)\n    data_update_time = models.TextField(blank=True, null=True)\n    logo = models.TextField(blank=True, null=True)\n    update_time = models.DateTimeField()\n    created_time = models.DateTimeField()\n\n    class Meta:\n        managed = False\n        db_table = &#039;eoias_company_base_info_extend&#039;\n\nclass Info(models.Model):\n    id = models.BigAutoField(primary_key=True)\n    company_name = models.CharField(unique=True, max_length=100)\n    date_base = models.DateTimeField(blank=True, null=True)\n    date_leads = models.DateTimeField(blank=True, null=True)\n    date_share_holder = models.DateTimeField(blank=True, null=True)\n    date_sub_company = models.DateTimeField(blank=True, null=True)\n    date_change = models.DateTimeField(blank=True, null=True)\n    date_finance = models.DateTimeField(blank=True, null=True)\n    date_tender = models.DateTimeField(blank=True, null=True)\n    date_investment = models.DateTimeField(blank=True, null=True)\n    date_copyright = models.DateTimeField(blank=True, null=True)\n    date_software_copyright = models.DateTimeField(blank=True, null=True)\n    date_certification = models.DateTimeField(blank=True, null=True)\n    date_website = models.DateTimeField(blank=True, null=True)\n    date_patents = models.DateTimeField(blank=True, null=True)\n    date_customer = models.DateTimeField(blank=True, null=True)\n    date_supplier = models.DateTimeField(blank=True, null=True)\n    date_annual_report = models.DateTimeField(blank=True, null=True)\n    date_brand = models.DateTimeField(blank=True, null=True)\n    update_time = models.DateTimeField()\n    created_time = models.DateTimeField()\n\n    class Meta:\n        managed = False\n        db_table = &#039;info&#039;<\/code><\/pre>\n<p>\u6211\u4eec\u628acompany\u8868\u7684models\u6307\u4ee4\u627e\u51fa\u6765,\u7c98\u5230B\u9879\u76ee\u7684\u6587\u4ef6\u4e2d\uff1a<\/p>\n<pre><code class=\"language-python\">class Company(models.Model):\n    id = models.IntegerField(primary_key=True)\n    name = models.CharField(max_length=20)\n    age = models.IntegerField()\n    address = models.CharField(max_length=25, blank=True, null=True)\n    salary = models.DecimalField(max_digits=18, decimal_places=2, blank=True, null=True)\n\n    class Meta:\n        managed = False\n        db_table = &#039;company&#039;\n==============================================================\n\n# \u52a0\u4e0a\u522b\u540d\n\nclass Company(models.Model):\n    id = models.IntegerField(primary_key=True)\n    name = models.CharField(verbose_name=&#039;\u540d\u79f0&#039;, max_length=20)\n    age = models.IntegerField(verbose_name=&#039;\u5e74\u9f84&#039;)\n    address = models.CharField(verbose_name=&#039;\u5730\u5740&#039;, max_length=25, blank=True, null=True)\n    salary = models.DecimalField(verbose_name=&#039;\u85aa\u8d44&#039;, max_digits=18, decimal_places=2, blank=True, null=True)\n\n    class Meta:\n        verbose_name = verbose_name_plural = &#039;\u516c\u53f8\u4eba\u5458\u4fe1\u606f&#039;\n        managed = False\n        db_table = &#039;company&#039;<\/code><\/pre>\n<h4>\u751f\u6210\u5355\u8868<\/h4>\n<p><code>python manage.py inspectdb --database default TableName<\/code>\u5c06\u6307\u5b9a\u7684\u8868\u751f\u6210\u5bf9\u5e94\u7684Model<br \/>\n<code>python manage.py inspectdb --database default district_info &gt; zsdt\/models.py<\/code>\u5c06\u6307\u5b9a\u7684\u8868\u751f\u6210\u5bf9\u5e94\u7684Model \u5bfc\u5165\u5230\u6307\u5b9a\u7684models.py \u6587\u4ef6<\/p>\n<pre><code class=\"language-bash\">C:\\Users\\Administrator\\Desktop\\\u534e\u793e\u6d4b\u8bd5\\rqapiv2&gt;python manage.py inspectdb --database default company\n# This is an auto-generated Django model module.\n# You&#039;ll have to do the following manually to clean this up:\n#   * Rearrange models&#039; order\n#   * Make sure each model has one field with primary_key=True\n#   * Make sure each ForeignKey and OneToOneField has `on_delete` set to the desired behavior\n#   * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table\n# Feel free to rename the models, but don&#039;t rename db_table values or field names.\nfrom django.db import models\n\nclass Company(models.Model):\n    id = models.IntegerField(primary_key=True)\n    name = models.CharField(max_length=20)\n    age = models.IntegerField()\n    address = models.CharField(max_length=25, blank=True, null=True)\n    salary = models.DecimalField(max_digits=18, decimal_places=2, blank=True, null=True)\n\n    class Meta:\n        managed = False\n        db_table = &#039;company&#039;<\/code><\/pre>\n<h2>3.\u6570\u636e\u8fc1\u79fb<\/h2>\n<pre><code class=\"language-bash\">C:\\Users\\Administrator\\Desktop\\\u534e\u793e\u6d4b\u8bd5\\rqapiv2&gt;python manage.py makemigrations aqcapi\nMigrations for &#039;aqcapi&#039;:\n  aqcapi\\migrations\\0002_company.py\n    - Create model Company\n\nC:\\Users\\Administrator\\Desktop\\\u534e\u793e\u6d4b\u8bd5\\rqapiv2&gt;python manage.py migrate aqcapi\nOperations to perform:\n  Apply all migrations: aqcapi\nRunning migrations:\n  Applying aqcapi.0002_company... OK\n<\/code><\/pre>\n<h2>4.\u9a8c\u8bc1<\/h2>\n<pre><code class=\"language-bash\">C:\\Users\\Administrator\\Desktop\\\u534e\u793e\u6d4b\u8bd5\\rqapiv2&gt;python manage.py shell\nPython 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32\nType &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.\n(InteractiveConsole)\n>&gt;&gt; from aqcapi.models import Company\n>&gt;&gt; Company.objects.values()\n&lt;QuerySet [{&#039;id&#039;: 1, &#039;name&#039;: &#039;Paul&#039;, &#039;age&#039;: 32, &#039;address&#039;: &#039;California               &#039;, &#039;salary&#039;: Decimal(&#039;20000.00&#039;)}, {&#039;id&#039;: 2, &#039;name&#039;: &#039;Tom&#039;, &#039;age&#039;: 29, &#039;address&#039;: &#039;Beijing\n &#039;, &#039;salary&#039;: Decimal(&#039;32000.00&#039;)}]&gt;\n>&gt;&gt;<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u6570\u636e\u5df2\u7ecf\u53ef\u4ee5\u64cd\u4f5c\u4e86<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u95ee\u9898\uff1a\u5047\u8bbe\u5df2\u6709\u4e00\u4e2a\u6570\u636e\u5e93\uff0c\u73b0\u8981\u7528Django\u9879\u76ee\uff0c\u5bf9\u8be5\u6570\u636e\u5e93\u8fdb\u884c\u8fde\u63a5\uff0c\u8fdb\u884cORM\u64cd\u4f5c \u6761\u4ef6\uff1a 1. \u6570\u636e\u5e93\uff08PGsql\uff09: databa   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[27],"tags":[19,18,16],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/188"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=188"}],"version-history":[{"count":4,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/188\/revisions"}],"predecessor-version":[{"id":1134,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/188\/revisions\/1134"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=188"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}