I'm working on a project using Python(3.7) and Django(3) in which I have implemented a few models. One of them is ReportsModel
which has ForeignKey
field to other models. Now I want to display other model data in ReportsModel
admin.
Here what I have tried so far:
From models.py
:
class ReportsModel(models.Model):
cdr_report = models.ForeignKey(CurrencyDistributionModel,
on_delete=models.CASCADE, null=True, default=None)
cme_report = models.ForeignKey(CurrencyManagementExpenditureModel,
on_delete=models.CASCADE, null=True, default=None)
cps_report = models.ForeignKey(CurrencyProcessingStorageModel,
on_delete=models.CASCADE, null=True, default=None)
cma_report = models.ForeignKey(CurrencyManagementAssetsModel,
on_delete=models.CASCADE, null=True, default=None)
def __str__(self):
if self.cdr_report is not None:
return self.cdr_report.RequestId
elif self.cme_report is not None:
return self.cme_report.RequestId
elif self.cps_report is not None:
return self.cps_report.RequestId
elif self.cma_report is not None:
return self.cma_report.RequestId
To display the ForeignKey
field in admin I'm using the django_reverse_admin
package, here how I did that:
From admin.py
:
class ReportAdmin(ReverseModelAdmin):
report = None
if ReportsModel.cdr_report is not None:
report = 'cdr_report'
elif ReportsModel.cme_report is not None:
report = 'cme_report'
elif ReportsModel.cps_report is not None:
report = 'cps_report'
elif ReportsModel.cma_report is not None:
report = 'cma_report'
search_fields = ['name']
inline_reverse = [report]
inline_type = 'stacked'
admin.site.register(ReportsModel, ReportAdmin)
now in the admin, it only works for the cdr_report
, when I add a report of type cme_report
, I'm getting the RequestField
correctly, but the cme_report
field is empty.
How can I display the Inline Admin
on the base of condition?
question from:
https://stackoverflow.com/questions/65871860/django-display-foreignkey-field-in-admin 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…