我有带有字段的市场模型:
class VacationEvent(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
process_fba = models.BooleanField(default=True)
@property
def has_amazon_fba_vacation(self):
return hasattr(self, 'amazon_fba_vacation')
在 admin.py 中:
class FBAInline(admin.StackedInline):
model = AmazonFBAVacation
can_delete = False
verbose_name_plural = 'amazon fba vacation'
fk_name = 'event'
在创建\更新 VacationEventAdmin 时,我需要有条件地显示 FBAInline 类。list_filter 显示“process_fba”字段的 true\false 值,因此如果为 true - 应显示 FBAInline:
@admin.register(VacationEvent)
class VacationEventAdmin(admin.ModelAdmin):
inlines = []
list_display = ('id', 'user', 'holiday_name', 'start_date', 'end_date', 'process_fba', 'process_fbm', 'process_walmart', 'process_ebay')
list_filter = ['process_fba', 'process_fbm', 'process_walmart', 'process_ebay', 'status',]
search_fields = ['holiday_name', 'user__username']
# if VacationEvent.process_fba and VacationEvent.has_amazon_fba_vacation:
# inlines.append(FBAInline)
# try:
# getattr(VacationEvent, 'process_fba')
# except AttributeError:
# print "doesn't"
# else:
# inlines.append(FBAInline)
我尝试获取 attr 但不了解如何比较管理员的字段值。谢谢你的帮助。