Historical model doesn't include fields inherited from superclass
See original GitHub issueI ran into this issue trying to add simple_history to a model using django-mptt ( https://github.com/django-mptt/django-mptt/ )
The model in question extends MPTTModel, which adds the fields lft
, rght
, tree_id
and level
to the model. However, if I try and save the model, it fails, with the exception:
TypeError: 'rght' is an invalid keyword argument for this function
I did some digging, and when the create_history_model() executes, these fields did not exist. The contents of model._meta.fields at that time was:
[<django.db.models.fields.AutoField: id>, <django.db.models.fields.CharField: name>, <django.db.models.fields.SlugField: url>, <django.db.models.fields.CharField: code>, <mptt.fields.TreeForeignKey: parent>, <django.db.models.fields.TextField: hidden_js>, <django.db.models.fields.related.ForeignKey: faculty>]
However, if I opened up a django shell, and output the contents of the same variable, I got:
[<django.db.models.fields.AutoField: id>, <django.db.models.fields.CharField: name>, <django.db.models.fields.SlugField: url>, <django.db.models.fields.CharField: code>, <mptt.fields.TreeForeignKey: parent>, <django.db.models.fields.TextField: hidden_js>, <django.db.models.fields.related.ForeignKey: faculty>, <django.db.models.fields.PositiveIntegerField: lft>, <django.db.models.fields.PositiveIntegerField: rght>, <django.db.models.fields.PositiveIntegerField: tree_id>, <django.db.models.fields.PositiveIntegerField: level>]
Which does have the additional fields. My conclusion is that the class has not yet been properly initialized at the time class_prepared is fired.
Issue Analytics
- State:
- Created 9 years ago
- Comments:10 (2 by maintainers)
Top Results From Across the Web
Historical model doesn't include fields inherited from superclass
I ran into this issue trying to add simple_history to a model using django-mptt ( https://github.com/django-mptt/django-mptt/ ) The model in ...
Read more >Do subclasses inherit private fields? - java - Stack Overflow
The answer is No. They do not. OBJECTS of subclasses contain private fields of their superclasses. The subclass itself has NO NOTION of...
Read more >Subclassing and Inheritance - Learning Java, 4th Edition [Book]
A subclass inherits variables and methods from its superclass and can use them as if they were declared within the subclass itself:
Read more >Inheritance - Learning the Java Language
A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited...
Read more >Object Serialization with Inheritance in Java - GeeksforGeeks
Case 2: If a superclass is not serializable, then subclass can still be serialized. Even though the superclass doesn't implement a Serializable ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It’s not a third party model but it’s inheriting from a third-party model. Instead of adding
HistoricalRecords
to the model, try addingregister(ModelName)
outside of the model definition (either in yourmodels.py
file or in theready
function of yourAppConfig
). Let me know if you run into any issues with that. @irrg@rossmechanic I don’t know where my brain went on this one…that’s a super easy solution. Seems to be sticking. Will report back if anything wonky happens in relation to django-simple-history. Thanks!