Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

python - Django display ForeignKey field in Admin

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
class ReportAdmin(admin.ModelAdmin):
    list_display =  ("id", "someVariable" )

    def someVariable(self, obj):
        # do your logic, query or just return FK model data
        return obj.CurrencyDistributionModel.<WhateverIsYourField>


admin.site.register(ReportsModel, ReportAdmin):

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...