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

false negative for readonly fields #62

Open
tumb1er opened this issue Aug 2, 2024 · 0 comments
Open

false negative for readonly fields #62

tumb1er opened this issue Aug 2, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@tumb1er
Copy link
Contributor

tumb1er commented Aug 2, 2024

We have "all fields" form with one field marked as "required" and "readonly_fields" hiding some of them on changeform admin page.

class SeasonForm(ModelForm):
    class Meta:
        model = models.Season
        fields = '__all__'

    parent = ModelChoiceField(queryset=..., required=True)

class SeasonAdmin(ModelAdmin):
    form = SeasonForm
    readonly_fields = ('parent',)

Pressing "save" on an existing valid object we got "invisible" error:
image

That's because "parent" field is still required and readonly at the same time.
So, django renders it without any form input and no value is passed in POST request.

But, we can't reproduce it with django-admin-smoke-based tests, because "readonly_fields" are not taken into account.

Below is a fix that makes our error reproducible:

def get_form_data_from_response(self, r: HttpResponseBase
                                    ) -> dict[str, Any]:
        data = super().get_form_data_from_response(r)
        cd = getattr(r, 'context_data')
        readonly_modeladmin_fields = cd['adminform'].readonly_fields
        for f in readonly_modeladmin_fields:
            data.pop(f, None)
        return data
@tumb1er tumb1er added the bug Something isn't working label Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant