adamcharnock/django-hordak

Document Evidence

aholtheuerl opened this issue · 3 comments

Hi!
Loved the library.

It would be nice to have a way to save a document as evidence that a transaction has been made. Some way to relate the hordak transaction to the currently existing models of my app.

Does anyone know any workaround for this?

@aholtheuerl The way we do it is a join model.

Your question, more generically, is about polymorphism - how do I foreign-key relate multiple different models to the same model? This blog post goes over many of the different options. The join model is just one method.

class Payment

class Refund

# apps/audit/models.py
from hordak.models import Transaction

class HordakAudit
  payment = models.ForeignKey(Payment...)
  refund = models.ForeignKey(Refund...)
  hordak_transaction = models.OneToOne(Transaction)

Benefits

  • You don't have to modify or subclass Transaction. You know where your code starts and ends.
  • Hard links allows foreign key constraints and all benefits

Drawbacks

  • Have to "ask" the HordakAudit obj what is the "source" by iterating if there's something in that column
  • Must manage that multiple "sources" are not linked to the same HordakAudit, you'll never know what is the true source.
  • If you have a LOT of source classes, this becomes unwieldy.

The simplest being, a direct link from the source object:

class Payment
  hordak_transaction = models.ForeignKey(Transaction)

This works if you NEVER/RARELY have to go from hordak's Transaction looking for your source.