我在使用TaggableManager时遇到一些问题。我是使用Django的新手,所以我做了关于教程的教程。现在,我想为每个博客帖子添加标签。 我完成了taggit的安装过程,他们说我需要添加一个自定义显示方法,但是这些都没有用。 (第7页:https://media.readthedocs.org/pdf/django-taggit/stable/django-taggit.pdf)
当我想打开博客文章时,出现该错误:
AttributeError at /post/4/
'_TaggableManager' object has no attribute 'name'
Request Method: GET Request URL: http://127.0.0.1:8000/post/4/
Django Version: 2.0.6
Exception Type: AttributeError
Exception Value:'_TaggableManager' object has no attribute 'name' Exception Location: /usr/local/lib/python3.6/site-packages/django/db/models/manager.py in __str__, line 36
Python Executable: /usr/local/opt/python/bin/python3.6
Python Version: 3.6.5
在下面,您可以查看我的代码: models.py
from django.db import models
from django.contrib import admin
from django.utils import timezone
from taggit.managers import TaggableManager
class Post(models.Model):
objects = models.Manager()
author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
title = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(
default=timezone.now)
published_date = models.DateTimeField(
blank=True, null=True)
tags = TaggableManager()
def publish(self):
self.published_date = timezone.now()
self.save()
def __str__(self):
return self.title
class PostAdmin(admin.ModelAdmin):
list_display = ['tag_list']
def get_queryset(self, request):
return super(PostAdmin, self).get_queryset(request).prefetch_related('tags')
def tag_list(self, obj):
return u", ".join(o.name for o in obj.tags.all())
答案 0 :(得分:0)
views.py
from django.shortcuts import render, get_object_or_404
from django.utils import timezone
from django.views.generic import ListView, DetailView
from .models import Post
from .forms import PostForm
from django.shortcuts import redirect
from django.contrib.auth.decorators import login_required
from taggit.managers import TaggableManager
class TagMixin(object):
def get_context_data(self, **kwargs):
context = super(TagMixin, self).get_context_data(**kwargs)
context['tags'] = Post.objects.all()
return context
def post_list(request):
posts = Post.objects.filter(
published_date__lte=timezone.now()).order_by('published_date')
return render(request, 'blog/post_list.html', {'posts': posts})
def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
post_related = post.tags.similar_objects()
return render(request, 'blog/post_detail.html', {'post': post, 'post_related': post_related})
@login_required
def post_new(request):
if request.method == "POST":
form = PostForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.author = request.user
post.save()
form.save_m2m()
return redirect('post_detail', pk=post.pk)
else:
form = PostForm()
return render(request, 'blog/post_edit.html', {'form': form})
@login_required
def post_edit(request, pk):
post = get_object_or_404(Post, pk=pk)
if request.method == "POST":
form = PostForm(request.POST, instance=post)
if form.is_valid():
post = form.save(commit=False)
post.author = request.user
post.save()
form.save_m2m()
return redirect('post_detail', pk=post.pk)
else:
form = PostForm(instance=post)
return render(request, 'blog/post_edit.html', {'form': form})
@login_required
def post_draft_list(request):
posts = Post.objects.filter(
published_date__isnull=True).order_by('created_date')
return render(request, 'blog/post_draft_list.html', {'posts': posts})
@login_required
def post_publish(request, pk):
post = get_object_or_404(Post, pk=pk)
post.publish()
return redirect('post_detail', pk=pk)
@login_required
def post_remove(request, pk):
post = get_object_or_404(Post, pk=pk)
post.delete()
return redirect('post_list')
class TagListView(TagMixin, ListView):
template_name = "blog/post_list.html"
model = Post
paginate_by = '10'
context_object_name = 'posts'
def get_queryset(self):
return Post.objects.filter(tags__slug=self.kwargs.get("slug")).all()