简体   繁体   English

让Django-taggit在admin中显示标签的值

[英]Make Django-taggit show the value of the tags in admin

So i want to make simple blog with django and djanggo-taggit. 所以我想用django和djanggo-taggit制作简单的博客。

This is my models.py 这是我的models.py

from django.db import models
from django.db.models import permalink
from taggit.managers import TaggableManager

class Post(models.Model):
    title = models.CharField(max_length=100, unique=True)
    slug = models.SlugField(max_length=100, unique=True)
    body = models.TextField()
    posted = models.DateField(db_index=True, auto_now_add=True)
    category = models.ForeignKey('Category')
    tag = TaggableManager()

    def __unicode__(self):
        return '%s' % self.title


class Category(models.Model):
    title = models.CharField(max_length=100, db_index=True)
    slug = models.SlugField(max_length=100, db_index=True)

    def __unicode__(self):
        return '%s' % self.title

    class Meta:
        verbose_name_plural = 'categories'

This is my admin.py 这是我的admin.py

from django.contrib import admin
from .models import Post, Category


class PostAdmin(admin.ModelAdmin):
    exclude = ('posted',)
    list_display = ('title', 'category', 'tag', 'posted')
    list_filter = ('posted', 'tag')
    search_fields = ('title', 'body', 'category', 'tag')
    prepopulated_fields = {'slug': ('title',)}


class CategoryAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug': ('title',)}

admin.site.register(Post, PostAdmin)
admin.site.register(Category, CategoryAdmin)

Right now at http://127.0.0.1:8000/admin/blog/post/ at the tags tab show <taggit.managers._TaggableManager object at 0x01AE4650> . 现在位于http://127.0.0.1:8000/admin/blog/post/的标签选项卡上显示<taggit.managers._TaggableManager object at 0x01AE4650> How can I make it show the title of the object ? 如何让它显示对象的标题? Thanks in Advance 提前致谢

It says here that you can't use it directly with list display: http://django-taggit.readthedocs.org/en/latest/admin.html 它在这里说你不能直接使用列表显示: http//django-taggit.readthedocs.org/en/latest/admin.html

You can get your tags like this: 你可以得到这样的标签:

class PostAdmin(admin.ModelAdmin):    
    list_display=['get_tags']

    def get_tags(self, post):
        tags = []
        for tag in post.tags.all():
            tags.append(str(tag))
        return ', '.join(tags)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM