Fixed sortable tabular inline visualization bug for view-only users #268
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Code:
The admin page for the Menu model has the following appearance:
For an user without change permissions, however, the form will look rather different:
A quick inspection of the HTML reveals the problem. The field responsible for ordering the inlines, which is normally represented as an input element, is being rendered as a dictionary instead.
These two elements are added to the HTML by the following line:
where field is an instance of either AdminField or AdminReadonlyField. These two classes are also the root of the problem, their init mehtod, specifically:
The difference is therefore located here. For view-only users, self.field corresponds to a simple dictionary, which does not contain any HTML. For users with more permissions, on the other hand, self.field will correspond to a BoundField, which will eventually return the expected HTML element.
In my implementation I tried to do the exact same thing that the init method does, aka accessing the form dictionary to retrieve a BoundField instance. This can be done quite easily by accessing field.form instead of field.field and then using a filter to retrieve the required Object.