I'm writing a reusable app with several abstract models. These models work together and that is all fine when they're normal concrete models. But when they're abstract and you want to work on a model that has not yet been defined in it's final used location it gets trickier.
The use case: The app is for competition logic. You participate in some kind of competition and then you win a prize and claim it.
So I got a
Participant that wins a
Prize and when the prize is won it'll create a
The logic for creating the claim has been put in
Prize but it doesn't have access to
Claim directly. So I added an attribute to my model that has the reference to
from django.db import model
'''To fix object resolution dynamically add a class based off of the
current models app label
_ClaimClass = None
if self._ClaimClass is None:
# self._meta.app_label is available on all models
self._ClaimClass = models.get_model(self._meta.app_label, 'Claim')
And then in my
Prize.claim(...) method where I used to do
Claim.objects.create(...) I now do
Worked like a charm and now my model is a little bit more dynamic.
The heavy lifting is done by django.db.models.get_model.
self._meta.app_label is available on all models.