Django: How do I make fields non-editable by default in an inline model formset?

2024/10/10 2:19:56

I have an inline model formset, and I'd like to make fields non-editable if those fields already have values when the page is loaded. If the user clicks an "Edit" button on that row, it would become editable and (using JavaScript) I would replace the original widgets with editable ones. I'd like to do something like this when loading the page:

for field in form.fields:if field.value:# display as textelse:# display as my standard editable widget for this field

I see that inlineformset_factory has an argument called formfield_callback. I suspect that this could be useful, but so for I haven't found any documentation for it. Can anyone point me to some useful documentation for this, and how it can help me solve this problem?

Answer

This one stumped me for a bit too. Hopefully this is what you're looking for.

<TABLE><form method="post" action=".">{{ formset.management_form }}{% for form in formset.forms %}{{ form.id }}<tr><td>{{ form.FirstName }}</td> <!-- This is a normal, editable field --><td>{{ form.instance.LastName }}</td> <!-- 'instance' is your actual Django model. LastName displays the text from the last name field --></tr>{% endfor %}</form>
</TABLE>
https://en.xdnf.cn/q/69946.html

Related Q&A

How to store result of an operation (like TOPK) per epoch in keras

I have written a custom layer in keras. in part of this custom layer lets say I have a matrix like this: c = tf.cast(tf.nn.top_k(tf.nn.top_k(n, tf.shape(n)[1])[1][:, ::-1], tf.shape(n)[1])[1][:, ::-1],…

How to start Firefox with with specific profile Selenium Python geckodriver

Here is my code:profile = webdriver.FirefoxProfile(C:\\Users\\Administrator\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\kvycjolb.Prdel) driver = webdriver.Firefox(profile)Im not getting any error an…

Beautiful Soup find elements having hidden style

My simple need. How do I find elements that are not visible on the webpage currently? I am guessing style="visibility:hidden" or style="display:none" are simple ways to hide an ele…

Fastest way to extract dictionary of sums in numpy in 1 I/O pass

Lets say I have an array like:arr = np.array([[1,20,5],[1,20,8],[3,10,4],[2,30,6],[3,10,5]])and I would like to form a dictionary of the sum of the third column for each row that matches each value in …

How to group by and dummies in pandas

I have a pandas dataframe: key valA 1A 2B 1B 3C 1C 4I want to get do some dummies like this:A 1100b 1010c 1001

Iterate over a dict except for x item items

I have a dict in this format:d_data = {key_1:value_1,key_2:value_2,key_3:value_3,key_x:value_x,key_n:value_n}and I have to iterate over its items:for key,value in columns.items():do somethingexcept for…

Best way to do a case insensitive replace but match the case of the word to be replaced?

So far Ive come up with the method below but my question is is there a shorter method out there that has the same result?My Code :input_str = "myStrIngFullOfStUfFiWannAReplaCE_StUfFs" …

Given a list of numbers, find all matrices such that each column and row sum up to 264

Lets say I have a list of 16 numbers. With these 16 numbers I can create different 4x4 matrices. Id like to find all 4x4 matrices where each element in the list is used once, and where the sum of each …

How can I access tablet pen data via Python?

I need to access a windows tablet pen data (such as the surface) via Python. I mainly need the position, pressure, and tilt values.I know how to access the Wacom pen data but the windows pen is differe…

Read Celery configuration from Python properties file

I have an application that needs to initialize Celery and other things (e.g. database). I would like to have a .ini file that would contain the applications configuration. This should be passed to th…