Email or username:

Password:

Forgot your password?
Nikita

#Django devs, I need your help!

Is there a way to display a model object’s data in a ModelForm as an uneditable field?

Use case: User A submits an order for User B to execute. On creation time, User A can choose an executor. User B can edit the order submitted to him, but can’t edit the ‘executor’ field any more. I do still want to show it in the HTML form, though.

Is there a way to cleanly implement it with ModelForm?

Plz boost because I’ve been sitting at this for two hours now 😩

7 comments
Ru (Tech) :blobcatsadlife:

@kytta might have done something like this but don’t recall it to be a model form… can check tomorrow when I’m at my desk and get back? Remind me in 14 hours pls if I haven’t gotten back to you?

Nikita

@ru sure; thank you for even taking the time! 🙌

Ru (Tech) :blobcatsadlife:

@kytta so, yes, i didn't use a modelform while implementing sth like this.

seeing your other conversation i doubt i can help without looking at the code. plus i haven't used crispy forms. :(

Brian :fedora: :python:

@kytta If I understand your problem correctly, you could inject arbitrary HTML key value pairs via the widget attrs property in the ModelForm or add a conditional check that if an order exists in the form template disable the widget? Sorta like this:

{% if obj %}
<fieldset disabled><div class="control">{{ field }}</div></fieldset>
{% else %}
<div class="control">{{ field }}</div>
{% endif %}

Nikita

@bpepple the thing is, if I have `field` specified as field of the ModelForm, then it should be sent on every POST request. However, if a field is disabled in HTML, it will not be sent on form submit; this triggers a ValidationError for a field user can't edit. I don't know what the behaviour of the fieldset is, though.

On the other note, I use Crispy Forms so I don't really have a template. Using HTML feels clunky, and writing custom Layout() objects is a pain 🙄

Thank you nevertheless!

Anthony Sorace

@kytta @ru (I have not done this particular case myself, but…) You can define attrs on a widget; I *think* this is just a question of setting the “readonly” attr on the one in question. Note that you’ll likely want to validate that field server side on submission to guard against folks editing the html directly.

Nikita

For all interested, here's how I solved it:

1. I defined the "executor" <input> as disabled and readonly
2. I made the "executor" field in the form not required
3. I also put self.object.executor inside form.initial on form render
4. On form_save(), I replace form.instance.executor with values from form.initial

May be hacky, but seems pretty bulletproof to me. And the code is pretty clean!

Thanks to @ru, @bpepple, and @a for helpful tips! ✨

For all interested, here's how I solved it:

1. I defined the "executor" <input> as disabled and readonly
2. I made the "executor" field in the form not required
3. I also put self.object.executor inside form.initial on form render
4. On form_save(), I replace form.instance.executor with values from form.initial

Go Up