Home:ALL Converter>django simple history - using model methods?

django simple history - using model methods?

Ask Time:2016-05-15T19:27:54         Author:user3599803

Json Formatter

I'm using django-simple-history: http://django-simple-history.readthedocs.io/en/latest/
I have a model, which I would like to apply its methods on an historical instance. Example:

from simple_history.models import HistoricalRecords

class Person(models.Model):
   firstname = models.CharField(max_length=20)
   lastname = models.CharField(max_length=20)
   history = HistoricalRecords()
   def fullName(self):
       return firstname + lastname

person = Person.objects.get(pk=1) # Person instance
for historyPerson in person.history:
    historyPerson.fullName() # wont work.

Since the class HistoricalPerson does not inherit the methods of Person. But using Person methods actually make sense, since they share the same fields..

Any solution for this? I'd prefer something simple, not like duplicating every method in my models for the history instances..

Author:user3599803,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/37237704/django-simple-history-using-model-methods
amoskaliov :

I found another workaround (maybe it's just the addon had been updated and got this feature). It's based on the documentation: adding-additional-fields-to-historical-models\nHistoricalRecords field accepts bases parameter which sets a class that history objects will inherit. But you can't just set bases=[Person] inside Person class description, because it's not yet initialized.\nSo I ended up with an abstract class, which is inherited by both Person class and HistoricalRecords field. So the example from the question would look like:\nclass AbstractPerson(models.Model):\n class Meta:\n abstract = True\n\n firstname = models.CharField(max_length=20)\n lastname = models.CharField(max_length=20)\n\n def fullName(self):\n return firstname + lastname\n\nclass Person(AbstractPerson):\n history = HistoricalRecords(bases=[AbstractPerson])\n\nAnd now history objects can use fullName method.",
2020-08-14T17:44:20
yy