Well, there are a couple things you could do:

for the PresupuestoAdminForm, you could do this to the CharField:
name = forms.CharField(max_length=100,
widget=forms.TextInput(attrs={'readonly': 'readonly'}))
(note the CharField is from the 'forms' package, not the 'models'....
you'll also need to remove the 'name' entry from the readonly_fields
tuple)

Alternatively, you could ignore using the custom ModelForm, and add
the 'name' field directly to the PresupuestoAdmin class. In that case
you can use the readonly_fields tuple.

I would probably go with the first choice since it will allow you to
write a custom query to populate the field in the constructor.

Example:

class StoreAdminForm(forms.ModelForm):
    test = forms.CharField(max_length=100,
widget=forms.TextInput(attrs={'readonly': 'readonly'}))

    def __init__(self, *args, **kwargs):
        super(StoreAdminForm, self).__init__(*args, **kwargs)

        if kwargs.has_key('instance'):
            instance = kwargs['instance']
            # do something with the instance here, like use it to
calculate something,
            # then set the value on the 'test' field using
self.fields['test']

    class Meta:
        model = Store

Good luck!

On Jun 7, 9:16 am, mf <mf2...@gmail.com> wrote:
> Thanks christian. I've followed your good advice but I don't know why
> am I getting  this error: "PresupuestoAdmin.readonly_fields[1], 'name'
> is not a callable or an attribute of 'PresupuestoAdmin' or found in
> the model 'Presupuesto' ". It seems that the 'name' field is not added
> to the form used by the admin.
>
> class PresupuestoAdminForm(forms.ModelForm):
>     name = models.CharField(max_length=100)
>
>     class Meta:
>         model = Presupuesto
>
> class PresupuestoAdmin(admin.ModelAdmin):
>     form = PresupuestoAdminForm
>
>     fieldsets = (
>         (None, {
>             'fields': (('id', 'fecha_emision', ), ('obra',
> 'num_pres_ext',),
>             'proveedor', 'descripcion', 'observaciones',
> 'importe_contratado',)
>         }),
>     )
>     readonly_fields = ('id', 'name',)
>
> admin.site.register(Presupuesto, PresupuestoAdmin)
>
> On 7 jun, 11:11, "christian.posta" <christian.po...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Create a different ModelForm that contains your readonly fields (and
> > populate them however you want) and set this on your ModelAdmin form.
>
> > See the ModelAdmin.form option in the 
> > docs.https://docs.djangoproject.com/en/1.2/ref/contrib/admin/#modeladmin-o...
>
> > On Jun 7, 6:01 am, mf <mf2...@gmail.com> wrote:
>
> > > Let's say I've two models:
>
> > >     class Book(models.Model):
> > >         name = models.CharField(max_length=50)
> > >         library = models.ForeignKeyField('Library')
>
> > >     class Library(models.Model):
> > >         name = models.CharField(max_length=50)
> > >         address = models.CharField(max_length=50)
> > >         tel = models.CharField(max_length=50)
>
> > > Is there a nice way to add some html(a readonly input field) between
> > > name and address in the Library change_form template?. I'm doing it
> > > overriding [admin/includes/fieldset.html][1] but it's getting messy
> > > and I can't find a way to display the html exactly where I want to.
> > > For example, if I want to add html displaying the amount of books that
> > > the library has below the name field I woul do this:
>
> > >     {% for field in line %}
> > >         ...
> > >         {% if field.field.name == 'name' %}
> > >             {{ field.field }}
> > >             <div class="form-row total_books">
> > >                 <div>
> > >                     <label for="total_books">Total books:</label>
> > >                     <input type="text" maxlength="10" name="totbooks"
> > > id="totbooks" readonly="readonly">
> > >                 </div>
> > >            </div>
> > >         {% else %}
> > >             {{ field.field }}
> > >         {% endif %}
> > >         ...
> > >     {% endfor %}
>
> > >   
> > > [1]:https://code.djangoproject.com/browser/django/trunk/django/contrib/ad...

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to