Set a default value for a field as the instance of a second model (which links to a third)

12 views
Skip to first unread message

Jonathan Acres

unread,
Apr 28, 2020, 1:36:01 PM4/28/20
to Django users
Based on the topic description, I can't help but feel I'm already overcomplicating this...

I have 3 models:
  • Audit
  • LeadAuditor
  • Action
The Audit model links to Lead_Auditor so each Audit instance has a single Audit.lead_auditor:
lead_auditor = models.ForeignKey('LeadAuditor', on_delete=models.SET_NULL, null=True)

The Action model links to Audit so each Action instance has a single Action.audit_ref:
audit_ref = models.ForeignKey('Audit', on_delete=models.SET_NULL, null=True)

That all works fine but I want to include a field in Action which shows the lead_auditor based on the audit_reference and I'm not sure how best to do that (still in the early stages of learning).

Ideally, I would like the Action model to contain an @property field which equates to (Audit.lead_auditor wherever Action.audit_ref is matched)

I'm going round in circles adding complexity that doesn't work anyway.  Any pointers would be much appreciated.

Thanks
Reply all
Reply to author
Forward
0 new messages