views:

315

answers:

4

Possible Duplicate:
In a django form, How to make a field readonly (or disabled) so that it cannot be edited?

I am making a production server in django admin where I have an integerfield

available = models.IntegerField(blank=True, help_text="(updated on save)")

I was wondering if I could make this a readonly field that wont be edited by the user but by the program such that on save a calculation is made for available(this code has been written already). All I want to know is how I can make this integerfield readonly and editable but only by the program on save?

Thank you

+2  A: 

See this answer.

You would use it like this;

class YourModelAdmin(ModelAdmin):
    readonly_fields = ('my_readonly_field',)
drmegahertz
readonly_fields is only supported in trunk (post django-1.1.1) at the moment. Just something to keep in mind if you try to use it.
istruble
A: 

see http://stackoverflow.com/questions/324477/in-a-django-form-how-to-make-a-field-readonly-or-disabled-so-that-it-cannot-be

Bear
I wish I could do this but I don't know or have the knowledge of how this works and would be implemented as a widget?
Jon
+1  A: 

Here is an implementation of a ReadonlyWidget: http://github.com/becomingGuru/djangoutils/blob/master/readonlyfield.py

I tend to use it.

class ReadOnlyWidget2(forms.Widget):

    def __init__(self, original_value, display_value):
    self.original_value = original_value
    self.display_value = display_value
    super(ReadOnlyWidget2, self).__init__()

    def _has_changed(self, initial, data):
    return False

    def render(self, name, value, attrs=None):
    if self.display_value is not None:
        return unicode(self.display_value)
    return unicode(self.original_value)

    def value_from_datadict(self, data, files, name):
    return self.original_value
Lakshman Prasad
I guess this comes in handy if you're always displaying your form just with {{form}} in the template... but otherwise, I'd skip all that and just do {{form.instance.field_i_want_to_display}]
lawrence
+1  A: 
available = models.IntegerField(editable=False)

You can still set it to whatever value you want in the code; it just won't show up in forms.

Or, you can also control the fields that get shown for any given form, e.g.:

class MyForm(forms.ModelForm):
    class Meta:
        model = MyModel
        exclude = ('available','other_field_to_skip')

Note that everyone else seems to interpret your question as "I want to see the value in the form but not be able to edit it", whereas I've interpreted it as "I want to exclude the value from the form". Because... forms are for editing. If you want to display a value, just display a value; no form is needed for that.

EDIT in response to comment: OK. So if your save function is overriding the value, then you're already set on the server side. You just need to handle the display. To make the actual HTML form input non-editable, just set the "disabled" html attribute on the widget:

available = models.IntegerField(widget=forms.TextInput(attrs={'disabled':'disabled'}))
lawrence
I actually want to show the data in the form just not have it editable. I have an override save function for the purpose that it shows calculated data on save in the available integer field. Now i just want to make that field uneditable by user but by program editable
Jon
See my edited answer.
lawrence
k I'm getting this error __init__() got an unexpected keyword argument 'widget'this is my code available = models.IntegerField(widget=forms.TextInput(attrs={'disabled':'disabled'}), help_text="(updated on save)")
Jon