假设我有3个模型:
棋手
比赛
参与
“参与”是另外两个的连接表。
我在管理面板中使用以下InlineFormSet来显示参与模型中的某些字段。
from .models import Participation
class ParticipationInline(admin.TabularInline):
model = Participation
formset = ParticipationAdminFormset
class BaseParticipationAdminFormset(BaseInlineFormSet):
def clean(self):
# Some code
# ...
def _construct_form(self, i, **kwargs):
# Some code
# ...
ParticipationAdminFormset = inlineformset_factory(
Tournament, Participation,
formset=BaseParticipationAdminFormset,
fields=("chess_player_id", "tournament_id", "is_active")
)
问题: 如何包含“ Chess_Player”模型中的任何字段,例如“ first_name”,放到上方的FormSet中?
答案 0 :(得分:1)
经过几次冲浪,我设法解决了这个问题: 我要做的就是添加只读字段,并包含chess_player属性:
class ParticipationInline(admin.TabularInline):
model = Participation
formset = ParticipationAdminFormset
readonly_fields = ['chess_player_name']
def chess_player_name(self, instance):
return instance.chess_player.first_name