Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom model name prefix #489

Closed
chris-umed opened this issue Nov 29, 2018 · 4 comments
Closed

Custom model name prefix #489

chris-umed opened this issue Nov 29, 2018 · 4 comments
Assignees
Labels
accepted Issue accepted for completion enhancement

Comments

@chris-umed
Copy link

chris-umed commented Nov 29, 2018

Problem Statement
The table names become quite long when prepending "Historical" to all base class names to produce the Historical model class name and would like to be able to override the default of using "Historical".

Describe the solution you'd like
Add a class instantiation option "custom_model_name_prefix" to override the default "Historical" implemented so that "custom_model_name" feature takes precedence.
Existing code:
name = ( self.custom_model_name if self.custom_model_name is not None else "Historical%s" % model._meta.object_name )
Proposed code change:
name = ( self.custom_model_name if self.custom_model_name is not None else "Historical%s" % model._meta.object_name if self.custom_model_name_prefix is None else self.custom_model_name_prefix + model._meta.object_name )

Describe alternatives you've considered
I am aware of the ability to completely override the class name using the "custom_model_name" option but that mechanism does not facilitate use in a base class that models inherit from because it cannot dynamically create the class name based on the concrete model class (or perhaps I am not smart enough to figure out how to do it).

Additional context
This feature would allow a shorter model name and would even allow model names to be in the form

[appName]_[modelName]

where the history models are created in a dedicated app using the "app" option thus making name much more aligned with the base table it is tracking
So for example you might have a model named "Poll" in an app named "mood" and by creating the historical model using:

history = HistoricalRecords(inherit=True, app="audit", custom_model_name_prefix="")

... you would end up with the table name being tracked as mood_poll and the and the history table as audit_poll

@rossmechanic
Copy link
Collaborator

This seems reasonable to me. Thanks for you long description - you clearly put some thought into this. Rather than add yet another parameter to the HistoricalRecords constructor, what do you think about making custom_model_name so that it can handle a callable? Thus, if you pass custom_model_name a string MyHistoricalModel, then your historical model name is MyHistoricalModel, but if you pass custom_model_name a callable that's given the base model name as input, you could do whatever you want to it:

class DummyClass(models.Model):
    history = HistoricalRecords(custom_model_name=lambda x: f'audit_{x}')

The change in models.py would be

if not self.custom_model_name:
    self.custom_model_name = lambda x: "Historical{}".format(x)
name = self.custom_model_name(model._meta.object_name) if callable(self.custom_model_name) else self.custom_model_name

What do you think of this as an alternative?

@chris-umed
Copy link
Author

Whats not to like about that solution - awesome flexibility.

Want me to implement and do a PR?

@rossmechanic
Copy link
Collaborator

Go for it! Thanks 👍

@jeking3
Copy link
Contributor

jeking3 commented Sep 17, 2021

It appears this was fixed without a This closes #... in the original pull request. Closing.

@jeking3 jeking3 closed this as completed Sep 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted Issue accepted for completion enhancement
Projects
None yet
Development

No branches or pull requests

3 participants