{"id":90015,"date":"2020-05-15T03:15:07","date_gmt":"2020-05-14T19:15:07","guid":{"rendered":"http:\/\/4563.org\/?p=90015"},"modified":"2020-05-15T03:15:07","modified_gmt":"2020-05-14T19:15:07","slug":"django-%e6%9c%89%e4%bb%80%e4%b9%88%e5%a5%bd%e6%96%b9%e6%b3%95%e5%af%b9-tags-%e7%9a%84%e6%a0%87%e9%a2%98%e6%8c%89%e5%ad%97%e6%8e%92%e5%ba%8f%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=90015","title":{"rendered":"django \u6709\u4ec0\u4e48\u597d\u65b9\u6cd5\u5bf9 tags \u7684\u6807\u9898\u6309\u5b57\u6392\u5e8f\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  django \u6709\u4ec0\u4e48\u597d\u65b9\u6cd5\u5bf9 tags \u7684\u6807\u9898\u6309\u5b57\u6392\u5e8f\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : python30 <\/span>  <span><i><\/i> 10<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<pre><code>class Tags(models.Model):  name = models.CharField(_(u'\u6807\u7b7e'), max_length=255)  excerpt = models.TextField(_(u'\u7b80\u4ecb'))      class Topic(models.Model):  title = models.CharField(_(u'\u6807\u9898'), max_length=255)  body = models.TextField(_(u'\u5185\u5bb9'))     tags = models.ManyToManyField('Tags', verbose_name = _(u'\u6807\u7b7e'), blank = True) <\/code><\/pre>\n<p>\u5982\u679c tags \u90fd\u662f\u8bcd\u8bed\uff0c\u5982\uff1a\u9ad8\u9ad8\u5174\u5174 \u9ad8\u5927 \u5f88\u9ad8 \u8dbe\u9ad8\u6c14\u626c \u9ad8\u89c1 \u53d1\u9ad8\u70e7 \u6311\u9ad8\u7403 \u4e09\u9ad8 \u5347\u9ad8 \u5bf9\u5e94\uff1atags.name<\/p>\n<p>topic \u662f\u5355\u4e2a\u5b57\uff1a\u9ad8 \u5bf9\u5e94\uff1atopic.title<\/p>\n<p>\u600e\u4e48\u6837 \u7ed9 tags \u6392\u5e8f \u5206\u522b\u6311\u51fa \u9ad8(topic.title) \u5f00\u5934\u7684\u8bcd \uff0c \u9ad8\u5728\u4e2d\u95f4\u7684\u8bcd\uff0c \u9ad8\u5728\u672b\u5c3e\u7684\u8bcd\uff1f<\/p>\n<pre><code>    \u5982\u4e0b\u4ee3\u7801\u662f\u60f3\u6311\u51fa \u5b57\u9ad8 \u5f00\u5934\u7684\u8bcd\u8bed\u3002\u5e76\u4e0d\u6210\u529f\uff01\uff01 <\/code><\/pre>\n<pre><code>     tagslist = topic.tags.all()         index_list = []   for a, b in enumerate(tagslist):     if str(b).find(str(topic.title))==0:      index_list.append(b)   context['index_list'] = index_list <\/code><\/pre>\n<p>\u5dee\u4e0d\u591a\u5c31\u662f\u8981\u5b9e\u73b0\u4e0b\u9762\u8fd9\u4e2a\u529f\u80fd<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/05\/20200518_5ec2f502b9f03.jpg\" alt=\"django \u6709\u4ec0\u4e48\u597d\u65b9\u6cd5\u5bf9 tags \u7684\u6807\u9898\u6309\u5b57\u6392\u5e8f\uff1f\" \/><\/p>\n<p>\u6709\u4ec0\u4e48\u597d\u529e\u6cd5\u5b9e\u73b0\u5417\uff1f<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>4<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1334027\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Vegetable <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/docs.djangoproject.com\/en\/3.0\/ref\/models\/querysets\/#startswith \u5f00\u59cb<br \/>https:\/\/docs.djangoproject.com\/en\/3.0\/ref\/models\/querysets\/#endswith \u7ed3\u675f<\/p>\n<p>\u5728\u4e2d\u95f4\u7684\u8fd9\u4e2a\u6709\u70b9\u4e0d\u592a\u597d\u5904\u7406\uff0c\u524d\u4e24\u4e2a\u672c\u8d28\u4e0a\u90fd\u662f<br \/>&#8220;`SQL<br \/>SELECT &#8230; WHERE headline LIKE &#8216;%Lennon&#8217;;<br \/>&#8220;`<br \/>\u4f60\u53ef\u4ee5\u8003\u8651\u76f4\u63a5\u4f7f\u7528 https:\/\/docs.djangoproject.com\/en\/3.0\/ref\/models\/querysets\/#regex \u6b63\u5219\u8868\u8fbe\u5f0f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1334028\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Hstar <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u5e16\u5b50\u770b\u5f97\u4e00\u5934\u96fe\u6c34\uff0c\u6807\u9898\u662f\u6309\u5b57\u6392\u5e8f\uff0c\u5185\u5bb9\u662f\u5982\u4f55\u5173\u952e\u8bcd\u7b5b\u9009\u3002<br \/>\u5982\u679c\u662f\u4f60\u6587\u4e2d\u7684\u95ee\u9898\uff0c\u5c31\u662f\u4e0a\u7684\u65b9\u6cd5\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1334029\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : python30 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Vegetable <\/p>\n<p>index_listcc =Tags.objects.filter(name__startswith=&#8217;\u9ad8&#8217;)<\/p>\n<p>\u597d\u50cf\u53d6\u4e0d\u5230\u503c<\/p>\n<p>\u8fd9\u4e2a\u67e5\u8be2\u53ea\u80fd\u82f1\u6587\u5b57\u7b26\uff1f<br \/>\u4e0d\u80fd\u662f\u4e2d\u6587\u7684\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1334030\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : python30 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Vegetable <br \/>@Hstar <\/p>\n<p>&#8220;`<br \/>class Topic(models.Model):<br \/> title = models.CharField(_(u&#8217;\u6807\u9898&#8217;), max_length=255)<br \/> body = models.TextField(_(u&#8217;\u5185\u5bb9&#8217;))<br \/> tags = models.ManyToManyField(&#8216;Tags&#8217;, verbose_name = _(u&#8217;\u6807\u7b7e&#8217;), blank = True)<\/p>\n<p> #\u8fd9\u5199\u6cd5\u53d6\u4e0d\u5230\u503c<br \/> def get_list_index(self):<br \/> print (self.tags.filter(name__startswith=str(self.title)))<br \/> return self.tags.filter(name__startswith=self.title)<\/p>\n<p> #\u8fd9\u6837\u5199\u4e5f\u4e0d\u5bf9<br \/> def get_index(self):<br \/> print (Tags.objects.filter(name__startswith=str(self.title)))<br \/> return Tags.objects.filter(name__startswith=self.title)<br \/>&#8220;`<\/p>\n<p>\u628a\u4e0a\u9762\u7684\u51fd\u6570\u5199\u5728 views.py \u4e5f\u662f\u53d6\u4e0d\u5230\u503c<br \/>\u662f\u5199\u6cd5\u4e0d\u5bf9\u8fd8\u662f\u53d6\u4e0d\u5230\u4e2d\u6587\u5b57\u7b26\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>django \u6709\u4ec0\u4e48\u597d\u65b9\u6cd5\u5bf9 ta&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/90015"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=90015"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/90015\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=90015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=90015"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=90015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}