我在Django中的模型中編寫了以下代碼。當我嘗試使用__str__()
方法時,出現該方法不可調用的錯誤。有什麼我失蹤?Django中的__str __()方法無法調用
from django.db import models
from organizer.models import Startup, Tag
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=63)
slug = models.SlugField(
max_length=63,
help_text='A label for URL config',
unique_for_month='pub_date')
text = models.TextField()
pub_date = models.DateField('date published',
auto_now_add=True)
tags = models.ManyToManyField(
Tag, related_name='blog_posts')
startups = models.ManyToManyField(
Startup, related_name='blog_posts')
def __str__(self):
return "{} on {}" (self.title, self.pub_date.strftime('%Y-%m_%d'))
class Meta:
verbose_name = 'blog post'
ordering = ['-pub_date', 'title']
get_latest_by = 'pub_date'
我使用django.blog_posts.all()調用__str__方法和獲得「‘海峽’對象不是可調用的」 –