Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Dec. 7, 2010 17:29:14

m.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

returning type and value


Hi all,

I have this in my model.py
def recipe_cost(self):

total = IngredientInfo.objects.filter(recipe =
self.id).aggregate(Sum('total')).values()

return total

Then in my admin.py I call that to display the cost.
class RecipeAdmin(admin.ModelAdmin):
list_display = ('title','recipe_cost')

I can see the cost but it's being returned like this:


But obviously I'd just want the value 500. What should I change?

Thanks

--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 7, 2010 17:52:30

Tom E.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

returning type and value


On Tue, Dec 7, 2010 at 5:29 PM, mongoose <darrenma1...@gmail.com> wrote:
> Hi all,
>
> I have this in my model.py
>        def recipe_cost(self):
>
>                total = IngredientInfo.objects.filter(recipe =
> self.id).aggregate(Sum('total')).values()
>
>                return total
>
> Then in my admin.py I call that to display the cost.
>        class RecipeAdmin(admin.ModelAdmin):
>                list_display = ('title','recipe_cost')
>
> I can see the cost but it's being returned like this:
>        
>
> But obviously I'd just want the value 500. What should I change?
>
> Thanks
>

What you are calling total isn't the total, its a ValuesQuerySet. You
want to extract the value from there, and convert it into whatever
format you want.

Eg:

def recipe_cost(self):
qs = IngredientInfo.objects.filter(
recipe=self.id).aggregate(Sum('total')).values()
return unicode(qs)

Cheers

Tom

--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Dec. 8, 2010 18:13:38

m.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

returning type and value


Thanks Tom, worked like a charm.
So actually qs is a collection of results and I can iterate through
them. Good to know.


Cheers.

On Dec 7, 7:52 pm, Tom Evans <tevans...@googlemail.com> wrote:
> On Tue, Dec 7, 2010 at 5:29 PM, mongoose <darrenma1...@gmail.com> wrote:
> > Hi all,
>
> > I have this in my model.py
> >        def recipe_cost(self):
>
> >                total = IngredientInfo.objects.filter(recipe =
> > self.id).aggregate(Sum('total')).values()
>
> >                return total
>
> > Then in my admin.py I call that to display the cost.
> >        class RecipeAdmin(admin.ModelAdmin):
> >                list_display = ('title','recipe_cost')
>
> > I can see the cost but it's being returned like this:
> >        
>
> > But obviously I'd just want the value 500. What should I change?
>
> > Thanks
>
> What you are calling total isn't the total, its a ValuesQuerySet. You
> want to extract the value from there, and convert it into whatever
> format you want.
>
> Eg:
>
> def recipe_cost(self):
>     qs = IngredientInfo.objects.filter(
>         recipe=self.id).aggregate(Sum('total')).values()
>     return unicode(qs)
>
> Cheers
>
> Tom

--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 23rd of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support