Extract only values without key from QuerySet and save them to list









up vote
2
down vote

favorite












I have problem with getting only values from QuerySet.
I have my table:



class Temperature(models.Model):
id = models.IntegerField(primary_key=True) # AutoField?
name = models.TextField(blank=True, null=True)
value = models.IntegerField(blank=True, null=True)
time = models.TextField(blank=True, null=True)

class Meta:
managed = False
db_table = 'temperature'


and in views.py:



class ChartData(APIView):
authentication_classes =
permission_classes =

def get(self, request, format=None):
labels = list(Temperature.objects.using('sensors').values('time'))
temp_values = list(Temperature.objects.using('sensors').values('value'))

data =
"labels": labels,
"temperature": temp_values,

return Response(data)


and with console.log() in html file I'm checking values, currently they are:enter image description here



But I want to retrive only value, without key. For e.g. :
enter image description here



Any help would be highly appreciated










share|improve this question



























    up vote
    2
    down vote

    favorite












    I have problem with getting only values from QuerySet.
    I have my table:



    class Temperature(models.Model):
    id = models.IntegerField(primary_key=True) # AutoField?
    name = models.TextField(blank=True, null=True)
    value = models.IntegerField(blank=True, null=True)
    time = models.TextField(blank=True, null=True)

    class Meta:
    managed = False
    db_table = 'temperature'


    and in views.py:



    class ChartData(APIView):
    authentication_classes =
    permission_classes =

    def get(self, request, format=None):
    labels = list(Temperature.objects.using('sensors').values('time'))
    temp_values = list(Temperature.objects.using('sensors').values('value'))

    data =
    "labels": labels,
    "temperature": temp_values,

    return Response(data)


    and with console.log() in html file I'm checking values, currently they are:enter image description here



    But I want to retrive only value, without key. For e.g. :
    enter image description here



    Any help would be highly appreciated










    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have problem with getting only values from QuerySet.
      I have my table:



      class Temperature(models.Model):
      id = models.IntegerField(primary_key=True) # AutoField?
      name = models.TextField(blank=True, null=True)
      value = models.IntegerField(blank=True, null=True)
      time = models.TextField(blank=True, null=True)

      class Meta:
      managed = False
      db_table = 'temperature'


      and in views.py:



      class ChartData(APIView):
      authentication_classes =
      permission_classes =

      def get(self, request, format=None):
      labels = list(Temperature.objects.using('sensors').values('time'))
      temp_values = list(Temperature.objects.using('sensors').values('value'))

      data =
      "labels": labels,
      "temperature": temp_values,

      return Response(data)


      and with console.log() in html file I'm checking values, currently they are:enter image description here



      But I want to retrive only value, without key. For e.g. :
      enter image description here



      Any help would be highly appreciated










      share|improve this question















      I have problem with getting only values from QuerySet.
      I have my table:



      class Temperature(models.Model):
      id = models.IntegerField(primary_key=True) # AutoField?
      name = models.TextField(blank=True, null=True)
      value = models.IntegerField(blank=True, null=True)
      time = models.TextField(blank=True, null=True)

      class Meta:
      managed = False
      db_table = 'temperature'


      and in views.py:



      class ChartData(APIView):
      authentication_classes =
      permission_classes =

      def get(self, request, format=None):
      labels = list(Temperature.objects.using('sensors').values('time'))
      temp_values = list(Temperature.objects.using('sensors').values('value'))

      data =
      "labels": labels,
      "temperature": temp_values,

      return Response(data)


      and with console.log() in html file I'm checking values, currently they are:enter image description here



      But I want to retrive only value, without key. For e.g. :
      enter image description here



      Any help would be highly appreciated







      python django django-queryset






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 13:41









      thebjorn

      12.5k44884




      12.5k44884










      asked Nov 10 at 13:38









      Aleksandra Skoczypiec

      415




      415






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          You can use values_list(..) [Django-doc] instead, and specify flat=True, like:



          def get(self, request, format=None):
          labels = list(Temperature.objects.using('sensors').values_list('time', flat=True))
          temp_values = list(Temperature.objects.using('sensors').values_list('value', flat=True))
          # ...


          but the above is not safe. A queryset is - unless you specify it - unordered. That means that two queries can result in data that does not "match" in the sense that the first time value does not per se corresponds to the first value value, although this might be atypical behavior (in some/most database systems), you typically never want this to happen. It also here results in two queries, which is not efficient.



          You can first fetch the values, and then use maps (or zip) to make a transpose, like:



          from operator import itemgetter

          def get(self, request, format=None):
          qs = Temperature.objects.using('sensors').values_list('time', 'value')
          labels = list(map(itemgetter(0), qs))
          temp_values = list(map(itemgetter(1), qs))
          # ...





          share|improve this answer


















          • 1




            Thank you! You solved my first and second problem which I was about to face :)
            – Aleksandra Skoczypiec
            Nov 10 at 13:48

















          up vote
          4
          down vote













          For this you have to use the 'values_list' function instead of 'values', and if you only want one field, use flat=True:



          temp_values = Temperature.objects.using('sensors').values_list('value', flat=True)





          share|improve this answer




















          • This was what I was searching for! Thank you very much! I was going insane with finding and trying different solutions...
            – Aleksandra Skoczypiec
            Nov 10 at 13:46










          • You are welcome!
            – Juan Ignacio Sánchez
            Nov 10 at 13:48

















          up vote
          1
          down vote













          If I'm understanding your question correctly, I think you're looking for values_list instead of values.



          Also, you probably don't need that list() call in there -- it'll just slow things down and waste memory.






          share|improve this answer




















          • This was really close. This returned values but as a lists
            – Aleksandra Skoczypiec
            Nov 10 at 13:45










          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',
          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%2f53239534%2fextract-only-values-without-key-from-queryset-and-save-them-to-list%23new-answer', 'question_page');

          );

          Post as a guest






























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          You can use values_list(..) [Django-doc] instead, and specify flat=True, like:



          def get(self, request, format=None):
          labels = list(Temperature.objects.using('sensors').values_list('time', flat=True))
          temp_values = list(Temperature.objects.using('sensors').values_list('value', flat=True))
          # ...


          but the above is not safe. A queryset is - unless you specify it - unordered. That means that two queries can result in data that does not "match" in the sense that the first time value does not per se corresponds to the first value value, although this might be atypical behavior (in some/most database systems), you typically never want this to happen. It also here results in two queries, which is not efficient.



          You can first fetch the values, and then use maps (or zip) to make a transpose, like:



          from operator import itemgetter

          def get(self, request, format=None):
          qs = Temperature.objects.using('sensors').values_list('time', 'value')
          labels = list(map(itemgetter(0), qs))
          temp_values = list(map(itemgetter(1), qs))
          # ...





          share|improve this answer


















          • 1




            Thank you! You solved my first and second problem which I was about to face :)
            – Aleksandra Skoczypiec
            Nov 10 at 13:48














          up vote
          2
          down vote



          accepted










          You can use values_list(..) [Django-doc] instead, and specify flat=True, like:



          def get(self, request, format=None):
          labels = list(Temperature.objects.using('sensors').values_list('time', flat=True))
          temp_values = list(Temperature.objects.using('sensors').values_list('value', flat=True))
          # ...


          but the above is not safe. A queryset is - unless you specify it - unordered. That means that two queries can result in data that does not "match" in the sense that the first time value does not per se corresponds to the first value value, although this might be atypical behavior (in some/most database systems), you typically never want this to happen. It also here results in two queries, which is not efficient.



          You can first fetch the values, and then use maps (or zip) to make a transpose, like:



          from operator import itemgetter

          def get(self, request, format=None):
          qs = Temperature.objects.using('sensors').values_list('time', 'value')
          labels = list(map(itemgetter(0), qs))
          temp_values = list(map(itemgetter(1), qs))
          # ...





          share|improve this answer


















          • 1




            Thank you! You solved my first and second problem which I was about to face :)
            – Aleksandra Skoczypiec
            Nov 10 at 13:48












          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          You can use values_list(..) [Django-doc] instead, and specify flat=True, like:



          def get(self, request, format=None):
          labels = list(Temperature.objects.using('sensors').values_list('time', flat=True))
          temp_values = list(Temperature.objects.using('sensors').values_list('value', flat=True))
          # ...


          but the above is not safe. A queryset is - unless you specify it - unordered. That means that two queries can result in data that does not "match" in the sense that the first time value does not per se corresponds to the first value value, although this might be atypical behavior (in some/most database systems), you typically never want this to happen. It also here results in two queries, which is not efficient.



          You can first fetch the values, and then use maps (or zip) to make a transpose, like:



          from operator import itemgetter

          def get(self, request, format=None):
          qs = Temperature.objects.using('sensors').values_list('time', 'value')
          labels = list(map(itemgetter(0), qs))
          temp_values = list(map(itemgetter(1), qs))
          # ...





          share|improve this answer














          You can use values_list(..) [Django-doc] instead, and specify flat=True, like:



          def get(self, request, format=None):
          labels = list(Temperature.objects.using('sensors').values_list('time', flat=True))
          temp_values = list(Temperature.objects.using('sensors').values_list('value', flat=True))
          # ...


          but the above is not safe. A queryset is - unless you specify it - unordered. That means that two queries can result in data that does not "match" in the sense that the first time value does not per se corresponds to the first value value, although this might be atypical behavior (in some/most database systems), you typically never want this to happen. It also here results in two queries, which is not efficient.



          You can first fetch the values, and then use maps (or zip) to make a transpose, like:



          from operator import itemgetter

          def get(self, request, format=None):
          qs = Temperature.objects.using('sensors').values_list('time', 'value')
          labels = list(map(itemgetter(0), qs))
          temp_values = list(map(itemgetter(1), qs))
          # ...






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 13:58

























          answered Nov 10 at 13:43









          Willem Van Onsem

          139k16131221




          139k16131221







          • 1




            Thank you! You solved my first and second problem which I was about to face :)
            – Aleksandra Skoczypiec
            Nov 10 at 13:48












          • 1




            Thank you! You solved my first and second problem which I was about to face :)
            – Aleksandra Skoczypiec
            Nov 10 at 13:48







          1




          1




          Thank you! You solved my first and second problem which I was about to face :)
          – Aleksandra Skoczypiec
          Nov 10 at 13:48




          Thank you! You solved my first and second problem which I was about to face :)
          – Aleksandra Skoczypiec
          Nov 10 at 13:48












          up vote
          4
          down vote













          For this you have to use the 'values_list' function instead of 'values', and if you only want one field, use flat=True:



          temp_values = Temperature.objects.using('sensors').values_list('value', flat=True)





          share|improve this answer




















          • This was what I was searching for! Thank you very much! I was going insane with finding and trying different solutions...
            – Aleksandra Skoczypiec
            Nov 10 at 13:46










          • You are welcome!
            – Juan Ignacio Sánchez
            Nov 10 at 13:48














          up vote
          4
          down vote













          For this you have to use the 'values_list' function instead of 'values', and if you only want one field, use flat=True:



          temp_values = Temperature.objects.using('sensors').values_list('value', flat=True)





          share|improve this answer




















          • This was what I was searching for! Thank you very much! I was going insane with finding and trying different solutions...
            – Aleksandra Skoczypiec
            Nov 10 at 13:46










          • You are welcome!
            – Juan Ignacio Sánchez
            Nov 10 at 13:48












          up vote
          4
          down vote










          up vote
          4
          down vote









          For this you have to use the 'values_list' function instead of 'values', and if you only want one field, use flat=True:



          temp_values = Temperature.objects.using('sensors').values_list('value', flat=True)





          share|improve this answer












          For this you have to use the 'values_list' function instead of 'values', and if you only want one field, use flat=True:



          temp_values = Temperature.objects.using('sensors').values_list('value', flat=True)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 13:42









          Juan Ignacio Sánchez

          303110




          303110











          • This was what I was searching for! Thank you very much! I was going insane with finding and trying different solutions...
            – Aleksandra Skoczypiec
            Nov 10 at 13:46










          • You are welcome!
            – Juan Ignacio Sánchez
            Nov 10 at 13:48
















          • This was what I was searching for! Thank you very much! I was going insane with finding and trying different solutions...
            – Aleksandra Skoczypiec
            Nov 10 at 13:46










          • You are welcome!
            – Juan Ignacio Sánchez
            Nov 10 at 13:48















          This was what I was searching for! Thank you very much! I was going insane with finding and trying different solutions...
          – Aleksandra Skoczypiec
          Nov 10 at 13:46




          This was what I was searching for! Thank you very much! I was going insane with finding and trying different solutions...
          – Aleksandra Skoczypiec
          Nov 10 at 13:46












          You are welcome!
          – Juan Ignacio Sánchez
          Nov 10 at 13:48




          You are welcome!
          – Juan Ignacio Sánchez
          Nov 10 at 13:48










          up vote
          1
          down vote













          If I'm understanding your question correctly, I think you're looking for values_list instead of values.



          Also, you probably don't need that list() call in there -- it'll just slow things down and waste memory.






          share|improve this answer




















          • This was really close. This returned values but as a lists
            – Aleksandra Skoczypiec
            Nov 10 at 13:45














          up vote
          1
          down vote













          If I'm understanding your question correctly, I think you're looking for values_list instead of values.



          Also, you probably don't need that list() call in there -- it'll just slow things down and waste memory.






          share|improve this answer




















          • This was really close. This returned values but as a lists
            – Aleksandra Skoczypiec
            Nov 10 at 13:45












          up vote
          1
          down vote










          up vote
          1
          down vote









          If I'm understanding your question correctly, I think you're looking for values_list instead of values.



          Also, you probably don't need that list() call in there -- it'll just slow things down and waste memory.






          share|improve this answer












          If I'm understanding your question correctly, I think you're looking for values_list instead of values.



          Also, you probably don't need that list() call in there -- it'll just slow things down and waste memory.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 13:41









          jacobian

          4,06021511




          4,06021511











          • This was really close. This returned values but as a lists
            – Aleksandra Skoczypiec
            Nov 10 at 13:45
















          • This was really close. This returned values but as a lists
            – Aleksandra Skoczypiec
            Nov 10 at 13:45















          This was really close. This returned values but as a lists
          – Aleksandra Skoczypiec
          Nov 10 at 13:45




          This was really close. This returned values but as a lists
          – Aleksandra Skoczypiec
          Nov 10 at 13:45

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239534%2fextract-only-values-without-key-from-queryset-and-save-them-to-list%23new-answer', 'question_page');

          );

          Post as a guest














































































          Popular posts from this blog

          27

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          Category:Rhetoric