question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Historical model doesn't include fields inherited from superclass

See original GitHub issue

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 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:closed
  • Created 9 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
rossmechaniccommented, Jun 13, 2018

It’s not a third party model but it’s inheriting from a third-party model. Instead of adding HistoricalRecords to the model, try adding register(ModelName) outside of the model definition (either in your models.py file or in the ready function of your AppConfig). Let me know if you run into any issues with that. @irrg

2reactions
irrgcommented, Jun 13, 2018

@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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found