django-hordak
django-hordak copied to clipboard
Document Evidence
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?
Maybe something like https://github.com/counsyl/capone#evidence-models
@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.
I would also achieve this as @nitsujri suggests. I can see an argument for pluggable models a la Django's AUTH_USER_MODEL / get_user_model(), but I think that is going to introduce a bunch of complexity (and therefore maintenance overhead).
Feel free to pitch the idea to either myself of @PetrDlouhy (@PetrDlouhy is more regularly around than I am) in a new issue. But with both only work on this project occasionally, so I'd personally prize simplicity and maintainability pretty highly.