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

Can't add change_reason field to the admin form #853

Open
jorguerra opened this issue Jul 22, 2021 · 2 comments · May be fixed by #1232
Open

Can't add change_reason field to the admin form #853

jorguerra opened this issue Jul 22, 2021 · 2 comments · May be fixed by #1232

Comments

@jorguerra
Copy link

I don't know how to add the change reason field in the admin form.
I have followed the instructions adding SIMPLE_HISTORY_HISTORY_CHANGE_REASON_USE_TEXT_FIELD=True to the settings.py file.

I also tried defining a model like this.

from django import forms
from .models import Identity

class IdentityForm(forms.ModelForm):
  class Meta:
    model = Identity
    fields = ['type', 'country', '_change_reason'] #Also tried with 'history_change_reason'   

But I got an error saying that the field _change_reason couldn't be found. Unknown field(s) (change_reason) specified for Identity

@ntouran
Copy link

ntouran commented Oct 19, 2021

That particular error is probably coming from the fact that _change_reason is not an official field on the Model object, but rather just a special attribute that django-simple-history uses to fill in the change reason on the history model. That said, I also don't know how to allow change reason to be specified in the admin panel. I think it may just not be a supported capability at the moment. Would be useful though for sure.

@vidz1979
Copy link

vidz1979 commented Apr 21, 2022

I think that this feature would be awesome! Did you guys figured out a way to do this?

Edit:

I've just found a way.

forms.py:

from django import forms

from app.models import MyModel

class MyForm(forms.ModelForm):
    class Meta:
        model = MyModel
        fields = "__all__"

    change_reason = forms.CharField(max_length=100)

In your admin.py, override save_model method:

    def save_model(self, request, obj, form, change):
        obj._change_reason = form.cleaned_data["change_reason"]
        super().save_model(request, obj, form, change)

mjsir911 added a commit to terrapower/django-simple-history that referenced this issue Aug 19, 2023
@mjsir911 mjsir911 linked a pull request Aug 19, 2023 that will close this issue
11 tasks
mjsir911 added a commit to terrapower/django-simple-history that referenced this issue Aug 19, 2023
mjsir911 added a commit to terrapower/django-simple-history that referenced this issue Oct 6, 2023
mjsir911 added a commit to terrapower/django-simple-history that referenced this issue Oct 6, 2023
mjsir911 added a commit to terrapower/django-simple-history that referenced this issue Oct 6, 2023
mjsir911 added a commit to terrapower/django-simple-history that referenced this issue Nov 14, 2023
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 a pull request may close this issue.

3 participants