如何添加一个计算字段到django admin inline



我试图添加一个计算字段到Django Admin内联。考虑这组类:

models.py:

class MyGroup(models.Model):
group_name = models.CharField()
size = models.IntegerField()
class MyUser(models.Model):
user_name = models.CharField()
groups = models.ManyToMany(MyGroup, related_name="users", through=MyMembership)

class MyMembership(models.Model):
group = models.ForeignKey(MyGroup)
user = models.ForeignKey(MyUser)
timestamp = models.DateTimeField(auto_add_now=True)

我想在内联中显示group.size字段。我认为下面的代码可以工作(基于这个答案):

admin.py:


class MyMembershipInline(admin.TabularInline):
model = MyUser.groups.through
fields = (
"group",
"timestamp",
"size",
)
readonly_fields = (
"timestamp",
"size",
)
def size(self, instance):
return instance.group.size
@admin.register(MyUser):
class MyUserAdmin(admin.ModelAdmin)
fields = ("user_name",)
inlines = (MyMembershipInline,)

但是我得到以下错误:

MyMembership指定的未知字段(s) (size)

任何建议吗?

在寻找解决这个问题的方法时,我在以下URL找到了一个适合我的解决方案:https://www.boris.co/2013/04/computed-field-on-tabularinline.html

给出的例子是:

class StatsInline(admin.TabularInline):
model = Stats
fields = ('clicked', 'shown', 'avg')
readonly_fields = ('avg',)
verbose_name = 'Stats'
verbose_name_plural = 'Stats'
can_delete = False
def avg(self, obj):
return float(obj.clicked) / obj.shown if obj.shown else 0

计算字段需要在fieldsreadonly_fields属性中同时提供。

我不得不在内联中使用自定义ModelForm。我是这样做的:

admin.py:

class MyMembershipInlineForm(forms.ModelForm):
class Meta:
model = MyUser.groups.through
fields = ("group", )
readonly_fields = (
"timestamp",
"size",
)
size = forms.IntegerField(disabled=True)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
my_membership = self.instance
self.initial = {
"size": my_membership.group.size if my_membership.pk else None
}
class MyMembershipInline(admin.TabularInline):
model = MyUser.groups.through
fields = (
"group",
"timestamp",
"size",
)
form = MyMembershipInlineForm
@admin.register(MyUser):
class MyUserAdmin(admin.ModelAdmin)
fields = ("user_name",)
inlines = (MyMembershipInline,)

相关内容

  • 没有找到相关文章

最新更新