相关疑难解决方法(0)

管理员中Django的外键参考

我一直试图在Django管理员中解决这个问题但仍无法找到文档.

在我的models.py中,我有以下代码:

from django.db import models

class Post(models.Model):
  title = models.CharField(max_length=200)
  author = models.ForeignKey('Author', blank=False)

class Author(models.Model):
  first_name = models.CharField('First Name',max_length=50)
  last_name = models.CharField('Last Name', max_length=50, blank=True)
  description = models.CharField(max_length=500, blank=True)

  def __str__(self):
    return (self.first_name + ' ' + self.last_name)
Run Code Online (Sandbox Code Playgroud)

在 django.contrib导入管理员的admin.py中

# Register your models here.
from .models import Author, Post

class PostAdmin(admin.ModelAdmin):
  list_display = ['title', 'author', 'get_author_description']

admin.site.register(Post, PostAdmin)
Run Code Online (Sandbox Code Playgroud)

但是,每次运行服务器时,我都会收到错误消息

<class 'blog.admin.PostAdmin'>: (admin.E108) The value of         
'list_display[2]' refers to 'get_author_description', which is not a 
callable, …
Run Code Online (Sandbox Code Playgroud)

django python-3.4

5
推荐指数
1
解决办法
1290
查看次数

标签 统计

django ×1

python-3.4 ×1