鉴于模型:
#models.py
class Author(models.Model):
name = models.CharField(max_length=200)
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.ManyToManyField(Author, through = 'AuthorBook')
class AuthorBook(models.Model):
author = models.ForeignKey(Author)
book = models.ForeignKey(Book)
some_info = models.CharField(max_length=200)
和
#admin.py
from django.contrib import admin
from .models import Author, Book, AuthorBook
from import_export.admin import ExportMixin
from import_export import resources, fields
from import_export.widgets import ManyToManyWidget
class BookResource(resources.ModelResource):
author = fields.Field(widget=ManyToManyWidget(Author))
class Meta(object):
model = Book
exclude = ('id',)
def dehydrate_author(self,Author):
return Author.name
当我尝试在Book的管理面板上导出数据时,我收到错误:
'Book'对象没有属性'name'
我被困在这里几个小时,我能找到的最接近的答案是this,但没有解决我的问题。
答案 0 :(得分:0)
您指定的模型是Book,它没有name属性。我猜你的函数dehydrate_livro()是用一本书作为参数调用的,你误导性地调用了Author(只是因为变量以大写字母开头并不能使它成为任何类的实例)。然后,您的方法尝试访问本书中的name属性Author.name
,但没有。
尝试:
def dehydrate_livro(self,book):
return book.author.all()
这将返回您图书作者的列表。