Tuesday, April 28, 2020

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

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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0ba3e0d8-2da1-4a31-823a-6a387b30644d%40googlegroups.com.

No comments:

Post a Comment