{"id":205209,"date":"2025-05-29T10:19:24","date_gmt":"2025-05-29T02:19:24","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205209\/"},"modified":"2025-05-29T10:19:24","modified_gmt":"2025-05-29T02:19:24","slug":"%e4%ba%86%e8%a7%a3-django-%e4%b8%ad%e5%8a%a8%e6%80%81%e5%85%b3%e7%b3%bb%e7%9a%84-contenttype-%e6%a8%a1%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205209\/","title":{"rendered":"\u4e86\u89e3 Django \u4e2d\u52a8\u6001\u5173\u7cfb\u7684 ContentType \u6a21\u578b"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u4e86\u89e3 Django \u4e2d\u52a8\u6001\u5173\u7cfb\u7684 ContentType \u6a21\u578b<\/h1>\n<p><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4e86\u89e3 Django \u4e2d\u52a8\u6001\u5173\u7cfb\u7684 ContentType \u6a21\u578b\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241126\/1732590042674539dadb27f.jpg\" class=\"aligncenter\"><\/p>\n<p>\u5728 django \u4e2d\uff0ccontenttype \u6a21\u578b\u662f\u7ba1\u7406\u4e0d\u540c\u6a21\u578b\u4e4b\u95f4\u901a\u7528\u5173\u7cfb\u7684\u5f3a\u5927\u5de5\u5177\u3002\u5b83\u5141\u8bb8\u60a8\u901a\u8fc7\u63d0\u4f9b\u4e00\u79cd\u52a8\u6001\u5f15\u7528\u9879\u76ee\u4e2d\u4efb\u4f55\u6a21\u578b\u7684\u65b9\u6cd5\u6765\u521b\u5efa\u5173\u7cfb\uff0c\u800c\u65e0\u9700\u5b9a\u4e49\u7279\u5b9a\u7684\u5916\u952e (foreignkeys)\u3002<\/p>\n<p>contenttype \u6a21\u578b\u662f django django.contrib.contenttypes \u5e94\u7528\u7a0b\u5e8f\u7684\u4e00\u90e8\u5206\u3002\u6bcf\u4e2a contenttype \u5b9e\u4f8b\u4ee3\u8868\u9879\u76ee\u4e2d\u7684\u4e00\u4e2a\u7279\u5b9a\u6a21\u578b\uff0c\u5177\u6709\u4e09\u4e2a\u4e3b\u8981\u5b57\u6bb5\uff1a<\/p>\n<ul>\n<li> app_label\uff1a\u5b9a\u4e49\u6a21\u578b\u7684\u5e94\u7528\u7a0b\u5e8f\u7684\u540d\u79f0\u3002<\/li>\n<li> model: \u6a21\u578b\u672c\u8eab\u7684\u540d\u79f0\u3002<\/li>\n<li> pk\uff1a\u6b64\u5185\u5bb9\u7c7b\u578b\u7684\u4e3b\u952e\uff0c\u7528\u4e8e\u5c06\u5176\u94fe\u63a5\u5230\u5176\u4ed6\u6a21\u578b\u3002<\/li>\n<\/ul>\n<p>django \u4f7f\u7528\u6b64\u6a21\u578b\u52a8\u6001\u5b58\u50a8\u5bf9\u5176\u4ed6\u6a21\u578b\u7684\u5f15\u7528\u3002\u60a8\u53ef\u4ee5\u6307\u5b9a\u201c\u6b64\u5bf9\u8c61\u5c5e\u4e8e\u7531\u5177\u6709\u7ed9\u5b9a id \u7684 contenttype \u6807\u8bc6\u7684\u6a21\u578b\u201d\uff0c\u800c\u4e0d\u662f\u6307\u5b9a\u201c\u6b64\u5bf9\u8c61\u5c5e\u4e8e article\u201d\u3002<\/p>\n<p>contenttype \u6a21\u578b\u7684\u4e3b\u8981\u7528\u9014\u4e4b\u4e00\u662f\u901a\u8fc7 genericforeignkey \u5b57\u6bb5\u542f\u7528\u901a\u7528\u5173\u7cfb\u3002\u5176\u5de5\u4f5c\u539f\u7406\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\n<p><strong>\u5b9a\u4e49 contenttype \u5b57\u6bb5\u548c\u5bf9\u8c61 id \u5b57\u6bb5<\/strong>:<\/p>\n<p> \u9996\u5148\u5411\u6a21\u578b\u6dfb\u52a0\u4e24\u4e2a\u5b57\u6bb5\uff1a<\/p>\n<ul>\n<li>\u6307\u5411contenttype\u7684foreignkey\u5b57\u6bb5\u3002<\/li>\n<li>\u7528\u4e8e\u5b58\u50a8\u76ee\u6807\u5bf9\u8c61 id \u7684 positiveintegerfield\uff08\u6216 uuidfield\uff0c\u5982\u679c\u9700\u8981\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u901a\u7528\u5916\u952e\uff08genericforeignkey\uff09<\/strong>:<\/p>\n<p> \u63a5\u4e0b\u6765\uff0c\u4f7f\u7528\u4e0a\u9762\u5b9a\u4e49\u7684\u4e24\u4e2a\u5b57\u6bb5\u7684\u540d\u79f0\u5b9a\u4e49 genericforeignkey \u5b57\u6bb5\u3002\u8be5\u5b57\u6bb5\u4e0d\u4f1a\u5728\u6570\u636e\u5e93\u4e2d\u521b\u5efa\u5b9e\u9645\u7684\u5217\uff0c\u4f46\u5b83\u4e3a django \u63d0\u4f9b\u4e86\u4e00\u79cd\u52a8\u6001\u94fe\u63a5\u5230\u76ee\u6807\u5bf9\u8c61\u7684\u65b9\u6cd5\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u8fd9\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre>from django.db import models\nfrom django.contrib.contenttypes.models import ContentType\nfrom django.contrib.contenttypes.fields import GenericForeignKey\n\nclass Comment(models.Model):\n    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)\n    object_id = models.PositiveIntegerField()\n    content_object = GenericForeignKey('content_type', 'object_id')\n    text = models.TextField()\n\n# Usage:\n# Let's say we have an `Article` model\nclass Article(models.Model):\n    title = models.CharField(max_length=100)\n    body = models.TextField()\n\n# Creating a comment for an article\narticle = Article.objects.create(title=\"My Article\", body=\"Article body\")\ncomment = Comment.objects.create(\n    content_type=ContentType.objects.get_for_model(Article),\n    object_id=article.id,\n    text=\"Great article!\"\n)\n<\/pre>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u8bc4\u8bba\u8bc4\u8bba\u4e00\u822c\u901a\u8fc7 contenttype \u6a21\u578b\u94fe\u63a5\u5230\u6587\u7ae0\u5b9e\u4f8b\u3002<\/p>\n<p>\u8981\u68c0\u7d22\u5185\u5bb9\u7c7b\u578b\uff0c\u8bf7\u4f7f\u7528 contenttype.objects.get_for_model(model)\uff0c\u5b83\u8fd4\u56de\u4e0e\u6307\u5b9a\u6a21\u578b\u5bf9\u5e94\u7684 contenttype \u5b9e\u4f8b\u3002\u8fd9\u5141\u8bb8\u60a8\u68c0\u7d22\u4e0e\u8be5\u6a21\u578b\u5173\u8054\u7684\u6240\u6709\u5bf9\u8c61\u6216\u5411\u5176\u6dfb\u52a0\u52a8\u6001\u5173\u7cfb\u3002<\/p>\n<p>contenttypes \u901a\u5e38\u7528\u4e8e\uff1a<\/p>\n<ul>\n<li>\u901a\u7528\u8bc4\u8bba\u7cfb\u7edf\uff08\u5982\u4e0a\u9762\u7684\u793a\u4f8b\uff09\uff0c<\/li>\n<li>\u81ea\u5b9a\u4e49\u6743\u9650\u7cfb\u7edf\uff0c<\/li>\n<li>\u901a\u77e5\u548c\u6d3b\u52a8\u7cfb\u7edf\uff0c<\/li>\n<li>\u5404\u79cd\u5185\u5bb9\u7c7b\u578b\u7684\u6807\u7b7e\u7cfb\u7edf\u3002<\/li>\n<\/ul>\n<ul>\n<li> <strong>\u4f18\u70b9<\/strong>\uff1a\u65e0\u9700\u4e8b\u5148\u4e86\u89e3\u76ee\u6807\u6a21\u578b\u5373\u53ef\u7075\u6d3b\u5730\u5728\u6a21\u578b\u4e4b\u95f4\u521b\u5efa\u5173\u7cfb\u3002<\/li>\n<li> <strong>\u9650\u5236<\/strong>\uff1a\u53ef\u80fd\u4f1a\u4f7f\u67e5\u8be2\u590d\u6742\u5316\uff0c\u5c24\u5176\u662f\u5f53\u5b58\u5728\u5f88\u591a\u5173\u7cfb\u65f6\uff0c\u5e76\u4e14\u590d\u6742\u7684\u8054\u63a5\u53ef\u80fd\u4f1a\u5f71\u54cd\u6027\u80fd\u3002<\/li>\n<\/ul>\n<p>\u603b\u4e4b\uff0ccontenttype \u6a21\u578b\u63d0\u4f9b\u4e86\u4e00\u79cd\u5728\u4e0d\u540c\u6a21\u578b\u4e4b\u95f4\u521b\u5efa\u901a\u7528\u548c\u52a8\u6001\u5173\u7cfb\u7684\u65b9\u6cd5\uff0c\u4f7f\u5176\u5728\u5177\u6709\u9ad8\u53ef\u6269\u5c55\u6027\u9700\u6c42\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7279\u522b\u6709\u7528\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u4e86\u89e3 Django \u4e2d\u52a8\u6001\u5173\u7cfb\u7684 ContentType \u6a21\u578b\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3 Django \u4e2d\u52a8\u6001\u5173\u7cfb\u7684&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205209","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205209"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205209\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}