Re: Displaying Calculated Values in Django Admin

2009-12-17 Thread Streamweaver
Thanks for the reply.  I'm still working with this and have it working
in cases of editing records but when I try to create a new record it
throws an error as would be expected since there's no instance to read
the initial values from.  I tried putting an is_bound check to give a
devault but of course as that's happening in __init__ it's never bound
so that's not working.  The solution is still escaping me and any
insight would be helpful.  Thanks again.

Some sample of the code I've been trying is:

class ProfileAdminForm(ModelForm):
"""Additional display Fields for Profile"""
age = forms.IntegerField() # Calculated age property
bmr = forms.FloatField(label='BMR') # Display the BMR value

def __init__(self, *args, **kwargs):
super(ProfileAdminForm, self).__init__(*args, **kwargs)
self.fields['age'].initial = self.instance.age
self.fields['age'].widget.attrs['readonly'] = True
self.fields['bmr'].initial = self.instance.weight.bmr
self.fields['bmr'].widget.attrs['readonly'] = True

class Meta:
model = Profile

class ProfileAdmin(admin.ModelAdmin):
form = ProfileAdminForm
fieldsets = [
 (None, {'fields': ['user', 'gender', 'height',
'birthday']}),
 ('Calculated Values', {'fields': ['age', 'bmr'],
'classes': ['collapse']}),
 ]
inlines = [WeightInline]


On Dec 16, 9:47 pm, Matt Schinckel  wrote:
> On Dec 17, 12:03 pm, Streamweaver  wrote:
>
>
>
> > Is it possible to display calculated values for models in the admin
> > interface.  I know about the list_display attribute for model.Admin
> > but all I really want to do is add text to a model edit form so I can
> > see calculated values.
>
> > For instance I have a model called Profile with a DateField called
> > birthday.  I have a method as a @property that returns calculated age
> > as of today.  I'd like to display the calculated age as text in the
> > model editing form in the admin interface.
>
> > How would I do this.
>
> > i.e. displaying the property below as text in the admin interface
>
> > class Profile(models.Model):
> > ...
>
> >     @property
> >     def age(self):
> >         """Returns age in years as of today."""
> >         today = date.today()
> >         return self.age_bydate(today)
>
> > Thanks for any input.
>
> You can add in fields to a custom form, which can do this.
>
> class ProfileAdmin(admin.ModelAdmin):
>     form = forms.ProfileAdminForm
>
> Matt

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.




Re: Displaying Calculated Values in Django Admin

2009-12-16 Thread Matt Schinckel
On Dec 17, 12:03 pm, Streamweaver  wrote:
> Is it possible to display calculated values for models in the admin
> interface.  I know about the list_display attribute for model.Admin
> but all I really want to do is add text to a model edit form so I can
> see calculated values.
>
> For instance I have a model called Profile with a DateField called
> birthday.  I have a method as a @property that returns calculated age
> as of today.  I'd like to display the calculated age as text in the
> model editing form in the admin interface.
>
> How would I do this.
>
> i.e. displaying the property below as text in the admin interface
>
> class Profile(models.Model):
> ...
>
>     @property
>     def age(self):
>         """Returns age in years as of today."""
>         today = date.today()
>         return self.age_bydate(today)
>
> Thanks for any input.

You can add in fields to a custom form, which can do this.

class ProfileAdmin(admin.ModelAdmin):
form = forms.ProfileAdminForm


Matt

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.




Displaying Calculated Values in Django Admin

2009-12-16 Thread Streamweaver
Is it possible to display calculated values for models in the admin
interface.  I know about the list_display attribute for model.Admin
but all I really want to do is add text to a model edit form so I can
see calculated values.

For instance I have a model called Profile with a DateField called
birthday.  I have a method as a @property that returns calculated age
as of today.  I'd like to display the calculated age as text in the
model editing form in the admin interface.

How would I do this.

i.e. displaying the property below as text in the admin interface

class Profile(models.Model):
...

@property
def age(self):
"""Returns age in years as of today."""
today = date.today()
return self.age_bydate(today)

Thanks for any input.

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.