Django ORM how to Round an Avg result












11















I have a model in which I use Django ORM to extract Avg of values from the table. I want to Round that Avg value, how do I do this?



See below I am extracting Avg price from Prices model grouped by date in format YYYY-MM, I want to automatically extract the average values rounded to the closest number.



rs = Prices.objects.all.extra(select={
'for_date': 'CONCAT(CONCAT(extract( YEAR from for_date ), "-"),
LPAD(extract(MONTH from for_date), 2, "00"))'
}).values('for_date').annotate(price=Avg('price')).order_by('-for_date')









share|improve this question




















  • 1





    i don't find a round function on the django queryset api reference, why not using the python native round() to round the returned results?

    – jxs
    Dec 10 '12 at 1:36











  • Have you worked this out by chance?

    – Nicholas Hamilton
    Apr 8 '14 at 13:02
















11















I have a model in which I use Django ORM to extract Avg of values from the table. I want to Round that Avg value, how do I do this?



See below I am extracting Avg price from Prices model grouped by date in format YYYY-MM, I want to automatically extract the average values rounded to the closest number.



rs = Prices.objects.all.extra(select={
'for_date': 'CONCAT(CONCAT(extract( YEAR from for_date ), "-"),
LPAD(extract(MONTH from for_date), 2, "00"))'
}).values('for_date').annotate(price=Avg('price')).order_by('-for_date')









share|improve this question




















  • 1





    i don't find a round function on the django queryset api reference, why not using the python native round() to round the returned results?

    – jxs
    Dec 10 '12 at 1:36











  • Have you worked this out by chance?

    – Nicholas Hamilton
    Apr 8 '14 at 13:02














11












11








11


2






I have a model in which I use Django ORM to extract Avg of values from the table. I want to Round that Avg value, how do I do this?



See below I am extracting Avg price from Prices model grouped by date in format YYYY-MM, I want to automatically extract the average values rounded to the closest number.



rs = Prices.objects.all.extra(select={
'for_date': 'CONCAT(CONCAT(extract( YEAR from for_date ), "-"),
LPAD(extract(MONTH from for_date), 2, "00"))'
}).values('for_date').annotate(price=Avg('price')).order_by('-for_date')









share|improve this question
















I have a model in which I use Django ORM to extract Avg of values from the table. I want to Round that Avg value, how do I do this?



See below I am extracting Avg price from Prices model grouped by date in format YYYY-MM, I want to automatically extract the average values rounded to the closest number.



rs = Prices.objects.all.extra(select={
'for_date': 'CONCAT(CONCAT(extract( YEAR from for_date ), "-"),
LPAD(extract(MONTH from for_date), 2, "00"))'
}).values('for_date').annotate(price=Avg('price')).order_by('-for_date')






django orm model rounding average






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 10 '12 at 2:16









borges

2,09212141




2,09212141










asked Dec 10 '12 at 1:23









AmiAmi

190212




190212








  • 1





    i don't find a round function on the django queryset api reference, why not using the python native round() to round the returned results?

    – jxs
    Dec 10 '12 at 1:36











  • Have you worked this out by chance?

    – Nicholas Hamilton
    Apr 8 '14 at 13:02














  • 1





    i don't find a round function on the django queryset api reference, why not using the python native round() to round the returned results?

    – jxs
    Dec 10 '12 at 1:36











  • Have you worked this out by chance?

    – Nicholas Hamilton
    Apr 8 '14 at 13:02








1




1





i don't find a round function on the django queryset api reference, why not using the python native round() to round the returned results?

– jxs
Dec 10 '12 at 1:36





i don't find a round function on the django queryset api reference, why not using the python native round() to round the returned results?

– jxs
Dec 10 '12 at 1:36













Have you worked this out by chance?

– Nicholas Hamilton
Apr 8 '14 at 13:02





Have you worked this out by chance?

– Nicholas Hamilton
Apr 8 '14 at 13:02












3 Answers
3






active

oldest

votes


















30














Use Func() expressions.



Here's an example using the Book model from Django Aggregation topic guide to round to two decimal places in SQLite:



class Round(Func):
function = 'ROUND'
template='%(function)s(%(expressions)s, 2)'

Book.objects.all().aggregate(Round(Avg('price')))


This allows the round function to be parameterised (from @RichardZschech's answer):



class Round(Func):
function = 'ROUND'
arity = 2

Book.objects.all().aggregate(Round(Avg('price'), 2))





share|improve this answer





















  • 1





    Depending on the complexity of your annotation or aggregation, you may need to assign an alias. e.g. Book.objects.all().aggregate(rounded_avg_price=Round(Avg('price')))

    – Duncan
    Jul 20 '16 at 15:21













  • @Blairg23, thanks for the edits! But don't you think that the bold and large headers make the text a bit too loud? In my experience using bold and large headers is rare in StackOverflow and they are mostly used to structure longer answers.

    – mrts
    Oct 11 '18 at 10:00











  • @mrts I was just preparing it to be a longer answer! ;) But also, I never think being more explicit is a bad thing :)

    – Blairg23
    Oct 15 '18 at 6:32











  • @Blairg23, right - but does the current version look good to you?

    – mrts
    Oct 16 '18 at 14:11






  • 1





    @Blairg23, thanks, all good then! Regarding 1.11 docs - I think it is more succinct to have links only to 2.1 docs. The relevant part of the API is the same and we can assume that people are intelligent enough to switch to 1.11 if they need to.

    – mrts
    Oct 17 '18 at 7:48



















7














Improving on @mrts answer.



This allows the round function to be parameterised:



class Round(Func):
function = 'ROUND'
arity = 2

Book.objects.all().aggregate(Round(Avg('price'), 2))





share|improve this answer
























  • This doesn't work in PostgreSQL. Any way to use arity with psql?

    – rain01
    Oct 9 '18 at 1:12











  • What error are you getting?

    – Richard Zschech
    Oct 9 '18 at 18:05











  • I updated your answer, but I also added it to the answer with more votes and will flag that answer to be the accepted answer.

    – Blairg23
    Oct 9 '18 at 19:05











  • I'm getting this django.db.utils.ProgrammingError: function round(double precision, integer) does not exist HINT: No function matches the given name and argument types. You might need to add explicit type casts. msonsona's solution worked for me but can't use arity with that.

    – rain01
    Oct 9 '18 at 19:42













  • @rain01 can you try rounding to 2.0 rather than 2. See stackoverflow.com/questions/13113096/…

    – Richard Zschech
    Oct 10 '18 at 1:14



















4














Building on previous answers, I've come to this solution to make it work for PostgreSQL:



from django.db.models import Func

class Round2(Func):
function = "ROUND"
template = "%(function)s(%(expressions)s::numeric, 2)"

# Then use it as ,e.g.:
# queryset.annotate(ag_roi=Round2("roi"))

# qs.aggregate(ag_sold_pct=Round2(Sum("sold_uts") / (1.0 * Sum("total_uts"))) * 100





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13793759%2fdjango-orm-how-to-round-an-avg-result%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    30














    Use Func() expressions.



    Here's an example using the Book model from Django Aggregation topic guide to round to two decimal places in SQLite:



    class Round(Func):
    function = 'ROUND'
    template='%(function)s(%(expressions)s, 2)'

    Book.objects.all().aggregate(Round(Avg('price')))


    This allows the round function to be parameterised (from @RichardZschech's answer):



    class Round(Func):
    function = 'ROUND'
    arity = 2

    Book.objects.all().aggregate(Round(Avg('price'), 2))





    share|improve this answer





















    • 1





      Depending on the complexity of your annotation or aggregation, you may need to assign an alias. e.g. Book.objects.all().aggregate(rounded_avg_price=Round(Avg('price')))

      – Duncan
      Jul 20 '16 at 15:21













    • @Blairg23, thanks for the edits! But don't you think that the bold and large headers make the text a bit too loud? In my experience using bold and large headers is rare in StackOverflow and they are mostly used to structure longer answers.

      – mrts
      Oct 11 '18 at 10:00











    • @mrts I was just preparing it to be a longer answer! ;) But also, I never think being more explicit is a bad thing :)

      – Blairg23
      Oct 15 '18 at 6:32











    • @Blairg23, right - but does the current version look good to you?

      – mrts
      Oct 16 '18 at 14:11






    • 1





      @Blairg23, thanks, all good then! Regarding 1.11 docs - I think it is more succinct to have links only to 2.1 docs. The relevant part of the API is the same and we can assume that people are intelligent enough to switch to 1.11 if they need to.

      – mrts
      Oct 17 '18 at 7:48
















    30














    Use Func() expressions.



    Here's an example using the Book model from Django Aggregation topic guide to round to two decimal places in SQLite:



    class Round(Func):
    function = 'ROUND'
    template='%(function)s(%(expressions)s, 2)'

    Book.objects.all().aggregate(Round(Avg('price')))


    This allows the round function to be parameterised (from @RichardZschech's answer):



    class Round(Func):
    function = 'ROUND'
    arity = 2

    Book.objects.all().aggregate(Round(Avg('price'), 2))





    share|improve this answer





















    • 1





      Depending on the complexity of your annotation or aggregation, you may need to assign an alias. e.g. Book.objects.all().aggregate(rounded_avg_price=Round(Avg('price')))

      – Duncan
      Jul 20 '16 at 15:21













    • @Blairg23, thanks for the edits! But don't you think that the bold and large headers make the text a bit too loud? In my experience using bold and large headers is rare in StackOverflow and they are mostly used to structure longer answers.

      – mrts
      Oct 11 '18 at 10:00











    • @mrts I was just preparing it to be a longer answer! ;) But also, I never think being more explicit is a bad thing :)

      – Blairg23
      Oct 15 '18 at 6:32











    • @Blairg23, right - but does the current version look good to you?

      – mrts
      Oct 16 '18 at 14:11






    • 1





      @Blairg23, thanks, all good then! Regarding 1.11 docs - I think it is more succinct to have links only to 2.1 docs. The relevant part of the API is the same and we can assume that people are intelligent enough to switch to 1.11 if they need to.

      – mrts
      Oct 17 '18 at 7:48














    30












    30








    30







    Use Func() expressions.



    Here's an example using the Book model from Django Aggregation topic guide to round to two decimal places in SQLite:



    class Round(Func):
    function = 'ROUND'
    template='%(function)s(%(expressions)s, 2)'

    Book.objects.all().aggregate(Round(Avg('price')))


    This allows the round function to be parameterised (from @RichardZschech's answer):



    class Round(Func):
    function = 'ROUND'
    arity = 2

    Book.objects.all().aggregate(Round(Avg('price'), 2))





    share|improve this answer















    Use Func() expressions.



    Here's an example using the Book model from Django Aggregation topic guide to round to two decimal places in SQLite:



    class Round(Func):
    function = 'ROUND'
    template='%(function)s(%(expressions)s, 2)'

    Book.objects.all().aggregate(Round(Avg('price')))


    This allows the round function to be parameterised (from @RichardZschech's answer):



    class Round(Func):
    function = 'ROUND'
    arity = 2

    Book.objects.all().aggregate(Round(Avg('price'), 2))






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Oct 12 '18 at 9:56

























    answered Jan 5 '16 at 22:30









    mrtsmrts

    4,41313237




    4,41313237








    • 1





      Depending on the complexity of your annotation or aggregation, you may need to assign an alias. e.g. Book.objects.all().aggregate(rounded_avg_price=Round(Avg('price')))

      – Duncan
      Jul 20 '16 at 15:21













    • @Blairg23, thanks for the edits! But don't you think that the bold and large headers make the text a bit too loud? In my experience using bold and large headers is rare in StackOverflow and they are mostly used to structure longer answers.

      – mrts
      Oct 11 '18 at 10:00











    • @mrts I was just preparing it to be a longer answer! ;) But also, I never think being more explicit is a bad thing :)

      – Blairg23
      Oct 15 '18 at 6:32











    • @Blairg23, right - but does the current version look good to you?

      – mrts
      Oct 16 '18 at 14:11






    • 1





      @Blairg23, thanks, all good then! Regarding 1.11 docs - I think it is more succinct to have links only to 2.1 docs. The relevant part of the API is the same and we can assume that people are intelligent enough to switch to 1.11 if they need to.

      – mrts
      Oct 17 '18 at 7:48














    • 1





      Depending on the complexity of your annotation or aggregation, you may need to assign an alias. e.g. Book.objects.all().aggregate(rounded_avg_price=Round(Avg('price')))

      – Duncan
      Jul 20 '16 at 15:21













    • @Blairg23, thanks for the edits! But don't you think that the bold and large headers make the text a bit too loud? In my experience using bold and large headers is rare in StackOverflow and they are mostly used to structure longer answers.

      – mrts
      Oct 11 '18 at 10:00











    • @mrts I was just preparing it to be a longer answer! ;) But also, I never think being more explicit is a bad thing :)

      – Blairg23
      Oct 15 '18 at 6:32











    • @Blairg23, right - but does the current version look good to you?

      – mrts
      Oct 16 '18 at 14:11






    • 1





      @Blairg23, thanks, all good then! Regarding 1.11 docs - I think it is more succinct to have links only to 2.1 docs. The relevant part of the API is the same and we can assume that people are intelligent enough to switch to 1.11 if they need to.

      – mrts
      Oct 17 '18 at 7:48








    1




    1





    Depending on the complexity of your annotation or aggregation, you may need to assign an alias. e.g. Book.objects.all().aggregate(rounded_avg_price=Round(Avg('price')))

    – Duncan
    Jul 20 '16 at 15:21







    Depending on the complexity of your annotation or aggregation, you may need to assign an alias. e.g. Book.objects.all().aggregate(rounded_avg_price=Round(Avg('price')))

    – Duncan
    Jul 20 '16 at 15:21















    @Blairg23, thanks for the edits! But don't you think that the bold and large headers make the text a bit too loud? In my experience using bold and large headers is rare in StackOverflow and they are mostly used to structure longer answers.

    – mrts
    Oct 11 '18 at 10:00





    @Blairg23, thanks for the edits! But don't you think that the bold and large headers make the text a bit too loud? In my experience using bold and large headers is rare in StackOverflow and they are mostly used to structure longer answers.

    – mrts
    Oct 11 '18 at 10:00













    @mrts I was just preparing it to be a longer answer! ;) But also, I never think being more explicit is a bad thing :)

    – Blairg23
    Oct 15 '18 at 6:32





    @mrts I was just preparing it to be a longer answer! ;) But also, I never think being more explicit is a bad thing :)

    – Blairg23
    Oct 15 '18 at 6:32













    @Blairg23, right - but does the current version look good to you?

    – mrts
    Oct 16 '18 at 14:11





    @Blairg23, right - but does the current version look good to you?

    – mrts
    Oct 16 '18 at 14:11




    1




    1





    @Blairg23, thanks, all good then! Regarding 1.11 docs - I think it is more succinct to have links only to 2.1 docs. The relevant part of the API is the same and we can assume that people are intelligent enough to switch to 1.11 if they need to.

    – mrts
    Oct 17 '18 at 7:48





    @Blairg23, thanks, all good then! Regarding 1.11 docs - I think it is more succinct to have links only to 2.1 docs. The relevant part of the API is the same and we can assume that people are intelligent enough to switch to 1.11 if they need to.

    – mrts
    Oct 17 '18 at 7:48













    7














    Improving on @mrts answer.



    This allows the round function to be parameterised:



    class Round(Func):
    function = 'ROUND'
    arity = 2

    Book.objects.all().aggregate(Round(Avg('price'), 2))





    share|improve this answer
























    • This doesn't work in PostgreSQL. Any way to use arity with psql?

      – rain01
      Oct 9 '18 at 1:12











    • What error are you getting?

      – Richard Zschech
      Oct 9 '18 at 18:05











    • I updated your answer, but I also added it to the answer with more votes and will flag that answer to be the accepted answer.

      – Blairg23
      Oct 9 '18 at 19:05











    • I'm getting this django.db.utils.ProgrammingError: function round(double precision, integer) does not exist HINT: No function matches the given name and argument types. You might need to add explicit type casts. msonsona's solution worked for me but can't use arity with that.

      – rain01
      Oct 9 '18 at 19:42













    • @rain01 can you try rounding to 2.0 rather than 2. See stackoverflow.com/questions/13113096/…

      – Richard Zschech
      Oct 10 '18 at 1:14
















    7














    Improving on @mrts answer.



    This allows the round function to be parameterised:



    class Round(Func):
    function = 'ROUND'
    arity = 2

    Book.objects.all().aggregate(Round(Avg('price'), 2))





    share|improve this answer
























    • This doesn't work in PostgreSQL. Any way to use arity with psql?

      – rain01
      Oct 9 '18 at 1:12











    • What error are you getting?

      – Richard Zschech
      Oct 9 '18 at 18:05











    • I updated your answer, but I also added it to the answer with more votes and will flag that answer to be the accepted answer.

      – Blairg23
      Oct 9 '18 at 19:05











    • I'm getting this django.db.utils.ProgrammingError: function round(double precision, integer) does not exist HINT: No function matches the given name and argument types. You might need to add explicit type casts. msonsona's solution worked for me but can't use arity with that.

      – rain01
      Oct 9 '18 at 19:42













    • @rain01 can you try rounding to 2.0 rather than 2. See stackoverflow.com/questions/13113096/…

      – Richard Zschech
      Oct 10 '18 at 1:14














    7












    7








    7







    Improving on @mrts answer.



    This allows the round function to be parameterised:



    class Round(Func):
    function = 'ROUND'
    arity = 2

    Book.objects.all().aggregate(Round(Avg('price'), 2))





    share|improve this answer













    Improving on @mrts answer.



    This allows the round function to be parameterised:



    class Round(Func):
    function = 'ROUND'
    arity = 2

    Book.objects.all().aggregate(Round(Avg('price'), 2))






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered May 11 '18 at 13:21









    Richard ZschechRichard Zschech

    18413




    18413













    • This doesn't work in PostgreSQL. Any way to use arity with psql?

      – rain01
      Oct 9 '18 at 1:12











    • What error are you getting?

      – Richard Zschech
      Oct 9 '18 at 18:05











    • I updated your answer, but I also added it to the answer with more votes and will flag that answer to be the accepted answer.

      – Blairg23
      Oct 9 '18 at 19:05











    • I'm getting this django.db.utils.ProgrammingError: function round(double precision, integer) does not exist HINT: No function matches the given name and argument types. You might need to add explicit type casts. msonsona's solution worked for me but can't use arity with that.

      – rain01
      Oct 9 '18 at 19:42













    • @rain01 can you try rounding to 2.0 rather than 2. See stackoverflow.com/questions/13113096/…

      – Richard Zschech
      Oct 10 '18 at 1:14



















    • This doesn't work in PostgreSQL. Any way to use arity with psql?

      – rain01
      Oct 9 '18 at 1:12











    • What error are you getting?

      – Richard Zschech
      Oct 9 '18 at 18:05











    • I updated your answer, but I also added it to the answer with more votes and will flag that answer to be the accepted answer.

      – Blairg23
      Oct 9 '18 at 19:05











    • I'm getting this django.db.utils.ProgrammingError: function round(double precision, integer) does not exist HINT: No function matches the given name and argument types. You might need to add explicit type casts. msonsona's solution worked for me but can't use arity with that.

      – rain01
      Oct 9 '18 at 19:42













    • @rain01 can you try rounding to 2.0 rather than 2. See stackoverflow.com/questions/13113096/…

      – Richard Zschech
      Oct 10 '18 at 1:14

















    This doesn't work in PostgreSQL. Any way to use arity with psql?

    – rain01
    Oct 9 '18 at 1:12





    This doesn't work in PostgreSQL. Any way to use arity with psql?

    – rain01
    Oct 9 '18 at 1:12













    What error are you getting?

    – Richard Zschech
    Oct 9 '18 at 18:05





    What error are you getting?

    – Richard Zschech
    Oct 9 '18 at 18:05













    I updated your answer, but I also added it to the answer with more votes and will flag that answer to be the accepted answer.

    – Blairg23
    Oct 9 '18 at 19:05





    I updated your answer, but I also added it to the answer with more votes and will flag that answer to be the accepted answer.

    – Blairg23
    Oct 9 '18 at 19:05













    I'm getting this django.db.utils.ProgrammingError: function round(double precision, integer) does not exist HINT: No function matches the given name and argument types. You might need to add explicit type casts. msonsona's solution worked for me but can't use arity with that.

    – rain01
    Oct 9 '18 at 19:42







    I'm getting this django.db.utils.ProgrammingError: function round(double precision, integer) does not exist HINT: No function matches the given name and argument types. You might need to add explicit type casts. msonsona's solution worked for me but can't use arity with that.

    – rain01
    Oct 9 '18 at 19:42















    @rain01 can you try rounding to 2.0 rather than 2. See stackoverflow.com/questions/13113096/…

    – Richard Zschech
    Oct 10 '18 at 1:14





    @rain01 can you try rounding to 2.0 rather than 2. See stackoverflow.com/questions/13113096/…

    – Richard Zschech
    Oct 10 '18 at 1:14











    4














    Building on previous answers, I've come to this solution to make it work for PostgreSQL:



    from django.db.models import Func

    class Round2(Func):
    function = "ROUND"
    template = "%(function)s(%(expressions)s::numeric, 2)"

    # Then use it as ,e.g.:
    # queryset.annotate(ag_roi=Round2("roi"))

    # qs.aggregate(ag_sold_pct=Round2(Sum("sold_uts") / (1.0 * Sum("total_uts"))) * 100





    share|improve this answer






























      4














      Building on previous answers, I've come to this solution to make it work for PostgreSQL:



      from django.db.models import Func

      class Round2(Func):
      function = "ROUND"
      template = "%(function)s(%(expressions)s::numeric, 2)"

      # Then use it as ,e.g.:
      # queryset.annotate(ag_roi=Round2("roi"))

      # qs.aggregate(ag_sold_pct=Round2(Sum("sold_uts") / (1.0 * Sum("total_uts"))) * 100





      share|improve this answer




























        4












        4








        4







        Building on previous answers, I've come to this solution to make it work for PostgreSQL:



        from django.db.models import Func

        class Round2(Func):
        function = "ROUND"
        template = "%(function)s(%(expressions)s::numeric, 2)"

        # Then use it as ,e.g.:
        # queryset.annotate(ag_roi=Round2("roi"))

        # qs.aggregate(ag_sold_pct=Round2(Sum("sold_uts") / (1.0 * Sum("total_uts"))) * 100





        share|improve this answer















        Building on previous answers, I've come to this solution to make it work for PostgreSQL:



        from django.db.models import Func

        class Round2(Func):
        function = "ROUND"
        template = "%(function)s(%(expressions)s::numeric, 2)"

        # Then use it as ,e.g.:
        # queryset.annotate(ag_roi=Round2("roi"))

        # qs.aggregate(ag_sold_pct=Round2(Sum("sold_uts") / (1.0 * Sum("total_uts"))) * 100






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Oct 17 '18 at 14:10

























        answered Aug 27 '18 at 12:28









        msonsonamsonsona

        845615




        845615






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13793759%2fdjango-orm-how-to-round-an-avg-result%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Create new schema in PostgreSQL using DBeaver

            Deepest pit of an array with Javascript: test on Codility

            Fotorealismo