Skip to content

FieldTracker can't be inherited from abstract model#80

Closed
rouge8 wants to merge 1 commit intojazzband:masterfrom
rouge8:abstract-field-tracker-inheritence
Closed

FieldTracker can't be inherited from abstract model#80
rouge8 wants to merge 1 commit intojazzband:masterfrom
rouge8:abstract-field-tracker-inheritence

Conversation

@rouge8
Copy link
Copy Markdown
Contributor

@rouge8 rouge8 commented Sep 20, 2013

I added a failing test case for inheriting a FieldTracker from an abstract model. It looks like the class_prepared signal isn't sent for abstract classes. :/

@kezabelle
Copy link
Copy Markdown
Collaborator

Yeah, looking at Django's source for ModelBase, abstract models never call _prepare, which is where class_prepared is fired.

Short of requiring the FieldTracker be on concrete models, I think it'd have to be changed upstream?

@carljm
Copy link
Copy Markdown
Collaborator

carljm commented Sep 26, 2013

Even if this is fixed upstream (I've filed https://code.djangoproject.com/ticket/21175), FieldTracker is currently written such that it can only track the fields that are defined on the model class it is attached to. Not sure if this is the behavior people would be looking for in attaching it to an abstract base class.

I think it would be possible to work around the lack of class_prepared on abstract models; it would just require listening to class_prepared on all models and checking them for an inherited or locally-defined FieldTracker.

@treyhunner
Copy link
Copy Markdown
Member

django-save-change implements some field-tracking mixins that override __setattr__ instead of using Django's hooks to track changes. That could be a possible alternative implementation

@ghost
Copy link
Copy Markdown

ghost commented Nov 23, 2013

Until the upstream issue gets addressed, I created a workaround for this problem -- at least the case where the tracker is defined on the same class as the field. Haven't tested it on cases where the field and tracker are defined on separate parent classes, but since the magic happens in init, I don't see a reason for it not to work for those cases too.

Won't say it's pretty (and it'll add some overhead on models that get instantiated frequently), but it seems to get the job done: https://gist.github.com/sbnoemi/7618916

If anyone has a more elegant workaround (alternative metaclasses?), I'd love to see it!

schinckel added a commit to schinckel/django-boardinghouse that referenced this pull request May 13, 2016
We now just add stuff to the global_settings for defaults.

Only install our Schema model if that is the settings.SCHEMA_MODEL

Remove usage of FieldTracker/ModelTracker, they don't work with
an abstract parent class anyway.

See: https://code.djangoproject.com/ticket/21175
And: jazzband/django-model-utils#80

Also, start getting ready for django 1.7.
@bee-keeper
Copy link
Copy Markdown
Member

Using latest django-model-utils==3.1.2 and Django==1.11.11 and it still doesn't seem possible to add fieldtracker to an abstract base class. Am I missing something or was this really never resolved?

@Natim
Copy link
Copy Markdown

Natim commented Aug 20, 2019

@bee-keeper it seems that you are right.
@carljm you seems to have a better understanding of me of what needs to be done there.
Do we want to fix that? Can you provide guidance about how you think would be the best way to tackle this?

@bee-keeper had you had a look at @sbnoemi proposal? Maybe handling this on AbstractModel is something model-util doesn't aim to provide?

@Natim Natim mentioned this pull request Aug 20, 2019
@Natim
Copy link
Copy Markdown

Natim commented Aug 21, 2019

We are keeping track of the FieldTracker for AbstractModel in #382
We are blocking on a Django bug for it: https://code.djangoproject.com/ticket/30427#ticket

I am closing this.

@Natim Natim closed this Aug 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants