Question

I have a model from which I have created a proxy model so that I can override get_query_set method of the default 'objects' manager. The model implements AuditTrail but the proxy ignores it -- which is not what I want. I want a save() via the proxy model to update the audit trail just like the model.

class SubjectConsent(BaseSubjectConsent):

    history = AuditTrail() 


class SubjectConsentY1(SubjectConsent):

    objects = SubjectY1Manager()

    class Meta:
        proxy = True

Is this behaviour expected? That is, are the managers of the model not available to the proxy?

My custom manager looks like this:

class SubjectY1Manager(models.Manager):

   def get_query_set(self):

        return QuerySet(self.model, using=self._db).filter(survey__survey_slug = 'mpp-year-0')
Was it helpful?

Solution

..self.answer...The intention is to get AuditTrail to update the audit trail for the model, SubjectConsent, via an instance of the proxy model. Through normal model class inheritance, managers are not inherited. I am not sure if this also applies to the "proxy model" design but in my case the AuditTrail manager is not present in the proxy model.

Adding "history = AuditTrail()" to the proxy model causes an error as AuditTrail adds itself to the proxy model using the same attributes of the model.

so, all i could come up with for now is to get an instance of the model and call it's save() method after super(SubjectConsentY1, self).save(). It is an extra call to the DB but, in this way, the audit trail is updated via an instance of the proxy model.

class SubjectConsent(BaseSubjectConsent):

    history = AuditTrail() 


class SubjectConsentY1(SubjectConsent):

    objects = SubjectY1Manager()

    def save(self, *args, **kwargs):
        # call super save() method to update the DB and get a pk
        super(SubjectConsentY1, self).save(*args, **kwargs)
        # get instance of model and save to update the audit trail
        SubjectConsent.objects.get(pk=self.pk).save()

    class Meta:
        proxy = True
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top