Referencing Object/Model field with dictionary key










0















I'm looping through a dictionary that has keys that match the fields in an ObjectModel.



With the value from that dictionary, I query an address and gather some data that I want to then save in the Object. My hope is to use the key from the dictionary to reference the field within the object.



I'm having a hard time explaining this with text so I hope the example code below showcases what I'm trying to do:



example_dict = 
"key": "value",
"second_key": "second_value"


class ExampleModel(models.Model):
key = models.CharField(max_length=255)
second_key = models.CharField(max_length=255)
}

object_example = ExampleModel()

for key, value in example_dict:
data = doDataRequest(value)
object_example[key] = data

object_example.save()









share|improve this question




























    0















    I'm looping through a dictionary that has keys that match the fields in an ObjectModel.



    With the value from that dictionary, I query an address and gather some data that I want to then save in the Object. My hope is to use the key from the dictionary to reference the field within the object.



    I'm having a hard time explaining this with text so I hope the example code below showcases what I'm trying to do:



    example_dict = 
    "key": "value",
    "second_key": "second_value"


    class ExampleModel(models.Model):
    key = models.CharField(max_length=255)
    second_key = models.CharField(max_length=255)
    }

    object_example = ExampleModel()

    for key, value in example_dict:
    data = doDataRequest(value)
    object_example[key] = data

    object_example.save()









    share|improve this question


























      0












      0








      0








      I'm looping through a dictionary that has keys that match the fields in an ObjectModel.



      With the value from that dictionary, I query an address and gather some data that I want to then save in the Object. My hope is to use the key from the dictionary to reference the field within the object.



      I'm having a hard time explaining this with text so I hope the example code below showcases what I'm trying to do:



      example_dict = 
      "key": "value",
      "second_key": "second_value"


      class ExampleModel(models.Model):
      key = models.CharField(max_length=255)
      second_key = models.CharField(max_length=255)
      }

      object_example = ExampleModel()

      for key, value in example_dict:
      data = doDataRequest(value)
      object_example[key] = data

      object_example.save()









      share|improve this question
















      I'm looping through a dictionary that has keys that match the fields in an ObjectModel.



      With the value from that dictionary, I query an address and gather some data that I want to then save in the Object. My hope is to use the key from the dictionary to reference the field within the object.



      I'm having a hard time explaining this with text so I hope the example code below showcases what I'm trying to do:



      example_dict = 
      "key": "value",
      "second_key": "second_value"


      class ExampleModel(models.Model):
      key = models.CharField(max_length=255)
      second_key = models.CharField(max_length=255)
      }

      object_example = ExampleModel()

      for key, value in example_dict:
      data = doDataRequest(value)
      object_example[key] = data

      object_example.save()






      python django python-3.x django-models






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 19:18







      Bonteq

















      asked Nov 14 '18 at 19:04









      BonteqBonteq

      5911




      5911






















          1 Answer
          1






          active

          oldest

          votes


















          1














          EDIT:
          You were also trying to set an attribute of a class as if it were a dict, try to use built in methods such as getattr and setaddr to access those. Look here:



          Visit https://docs.python.org/3.7/library/functions.html#setattr



          I would recommend to try iterating differently on the dictionary, you could try something like this below, I think this is where your issue is coming in. If not let me know!



          for key, value in example_dict.items():
          data = doDataRequest(key)
          //Use setattr
          setattr(object_example, key, data)



          And on another note, why use a dict for holding key value pairs if you are not using the value? perhaps try an array with your keys?






          share|improve this answer























          • I was suggested to use the built-in function settattr which seems to be working except for my DateTimeField is bugging out. As for the dictionary, I am actually using the value, it was a typo - I've since corrected it!

            – Bonteq
            Nov 14 '18 at 19:19












          • is USE_TZ=True in your settings.py?

            – Theodore Howell
            Nov 14 '18 at 19:21











          • If thats the case look into pytz and use the correctly formatted datetime object. There is a core django contributor who wrote this article, I attended his talk in San Diego and it was mind blowing. Visit: reinout.vanrees.org/weblog/2018/05/25/09-its-about-time.html

            – Theodore Howell
            Nov 14 '18 at 19:22











          • No, it isn't but I'll give that a read, thanks!

            – Bonteq
            Nov 14 '18 at 19:29











          • If you did not add the .items() in the for loop, that might be why date time is not working. Good luck :)

            – Theodore Howell
            Nov 14 '18 at 19:43










          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%2f53307122%2freferencing-object-model-field-with-dictionary-key%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          EDIT:
          You were also trying to set an attribute of a class as if it were a dict, try to use built in methods such as getattr and setaddr to access those. Look here:



          Visit https://docs.python.org/3.7/library/functions.html#setattr



          I would recommend to try iterating differently on the dictionary, you could try something like this below, I think this is where your issue is coming in. If not let me know!



          for key, value in example_dict.items():
          data = doDataRequest(key)
          //Use setattr
          setattr(object_example, key, data)



          And on another note, why use a dict for holding key value pairs if you are not using the value? perhaps try an array with your keys?






          share|improve this answer























          • I was suggested to use the built-in function settattr which seems to be working except for my DateTimeField is bugging out. As for the dictionary, I am actually using the value, it was a typo - I've since corrected it!

            – Bonteq
            Nov 14 '18 at 19:19












          • is USE_TZ=True in your settings.py?

            – Theodore Howell
            Nov 14 '18 at 19:21











          • If thats the case look into pytz and use the correctly formatted datetime object. There is a core django contributor who wrote this article, I attended his talk in San Diego and it was mind blowing. Visit: reinout.vanrees.org/weblog/2018/05/25/09-its-about-time.html

            – Theodore Howell
            Nov 14 '18 at 19:22











          • No, it isn't but I'll give that a read, thanks!

            – Bonteq
            Nov 14 '18 at 19:29











          • If you did not add the .items() in the for loop, that might be why date time is not working. Good luck :)

            – Theodore Howell
            Nov 14 '18 at 19:43















          1














          EDIT:
          You were also trying to set an attribute of a class as if it were a dict, try to use built in methods such as getattr and setaddr to access those. Look here:



          Visit https://docs.python.org/3.7/library/functions.html#setattr



          I would recommend to try iterating differently on the dictionary, you could try something like this below, I think this is where your issue is coming in. If not let me know!



          for key, value in example_dict.items():
          data = doDataRequest(key)
          //Use setattr
          setattr(object_example, key, data)



          And on another note, why use a dict for holding key value pairs if you are not using the value? perhaps try an array with your keys?






          share|improve this answer























          • I was suggested to use the built-in function settattr which seems to be working except for my DateTimeField is bugging out. As for the dictionary, I am actually using the value, it was a typo - I've since corrected it!

            – Bonteq
            Nov 14 '18 at 19:19












          • is USE_TZ=True in your settings.py?

            – Theodore Howell
            Nov 14 '18 at 19:21











          • If thats the case look into pytz and use the correctly formatted datetime object. There is a core django contributor who wrote this article, I attended his talk in San Diego and it was mind blowing. Visit: reinout.vanrees.org/weblog/2018/05/25/09-its-about-time.html

            – Theodore Howell
            Nov 14 '18 at 19:22











          • No, it isn't but I'll give that a read, thanks!

            – Bonteq
            Nov 14 '18 at 19:29











          • If you did not add the .items() in the for loop, that might be why date time is not working. Good luck :)

            – Theodore Howell
            Nov 14 '18 at 19:43













          1












          1








          1







          EDIT:
          You were also trying to set an attribute of a class as if it were a dict, try to use built in methods such as getattr and setaddr to access those. Look here:



          Visit https://docs.python.org/3.7/library/functions.html#setattr



          I would recommend to try iterating differently on the dictionary, you could try something like this below, I think this is where your issue is coming in. If not let me know!



          for key, value in example_dict.items():
          data = doDataRequest(key)
          //Use setattr
          setattr(object_example, key, data)



          And on another note, why use a dict for holding key value pairs if you are not using the value? perhaps try an array with your keys?






          share|improve this answer













          EDIT:
          You were also trying to set an attribute of a class as if it were a dict, try to use built in methods such as getattr and setaddr to access those. Look here:



          Visit https://docs.python.org/3.7/library/functions.html#setattr



          I would recommend to try iterating differently on the dictionary, you could try something like this below, I think this is where your issue is coming in. If not let me know!



          for key, value in example_dict.items():
          data = doDataRequest(key)
          //Use setattr
          setattr(object_example, key, data)



          And on another note, why use a dict for holding key value pairs if you are not using the value? perhaps try an array with your keys?







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 19:15









          Theodore HowellTheodore Howell

          299211




          299211












          • I was suggested to use the built-in function settattr which seems to be working except for my DateTimeField is bugging out. As for the dictionary, I am actually using the value, it was a typo - I've since corrected it!

            – Bonteq
            Nov 14 '18 at 19:19












          • is USE_TZ=True in your settings.py?

            – Theodore Howell
            Nov 14 '18 at 19:21











          • If thats the case look into pytz and use the correctly formatted datetime object. There is a core django contributor who wrote this article, I attended his talk in San Diego and it was mind blowing. Visit: reinout.vanrees.org/weblog/2018/05/25/09-its-about-time.html

            – Theodore Howell
            Nov 14 '18 at 19:22











          • No, it isn't but I'll give that a read, thanks!

            – Bonteq
            Nov 14 '18 at 19:29











          • If you did not add the .items() in the for loop, that might be why date time is not working. Good luck :)

            – Theodore Howell
            Nov 14 '18 at 19:43

















          • I was suggested to use the built-in function settattr which seems to be working except for my DateTimeField is bugging out. As for the dictionary, I am actually using the value, it was a typo - I've since corrected it!

            – Bonteq
            Nov 14 '18 at 19:19












          • is USE_TZ=True in your settings.py?

            – Theodore Howell
            Nov 14 '18 at 19:21











          • If thats the case look into pytz and use the correctly formatted datetime object. There is a core django contributor who wrote this article, I attended his talk in San Diego and it was mind blowing. Visit: reinout.vanrees.org/weblog/2018/05/25/09-its-about-time.html

            – Theodore Howell
            Nov 14 '18 at 19:22











          • No, it isn't but I'll give that a read, thanks!

            – Bonteq
            Nov 14 '18 at 19:29











          • If you did not add the .items() in the for loop, that might be why date time is not working. Good luck :)

            – Theodore Howell
            Nov 14 '18 at 19:43
















          I was suggested to use the built-in function settattr which seems to be working except for my DateTimeField is bugging out. As for the dictionary, I am actually using the value, it was a typo - I've since corrected it!

          – Bonteq
          Nov 14 '18 at 19:19






          I was suggested to use the built-in function settattr which seems to be working except for my DateTimeField is bugging out. As for the dictionary, I am actually using the value, it was a typo - I've since corrected it!

          – Bonteq
          Nov 14 '18 at 19:19














          is USE_TZ=True in your settings.py?

          – Theodore Howell
          Nov 14 '18 at 19:21





          is USE_TZ=True in your settings.py?

          – Theodore Howell
          Nov 14 '18 at 19:21













          If thats the case look into pytz and use the correctly formatted datetime object. There is a core django contributor who wrote this article, I attended his talk in San Diego and it was mind blowing. Visit: reinout.vanrees.org/weblog/2018/05/25/09-its-about-time.html

          – Theodore Howell
          Nov 14 '18 at 19:22





          If thats the case look into pytz and use the correctly formatted datetime object. There is a core django contributor who wrote this article, I attended his talk in San Diego and it was mind blowing. Visit: reinout.vanrees.org/weblog/2018/05/25/09-its-about-time.html

          – Theodore Howell
          Nov 14 '18 at 19:22













          No, it isn't but I'll give that a read, thanks!

          – Bonteq
          Nov 14 '18 at 19:29





          No, it isn't but I'll give that a read, thanks!

          – Bonteq
          Nov 14 '18 at 19:29













          If you did not add the .items() in the for loop, that might be why date time is not working. Good luck :)

          – Theodore Howell
          Nov 14 '18 at 19:43





          If you did not add the .items() in the for loop, that might be why date time is not working. Good luck :)

          – Theodore Howell
          Nov 14 '18 at 19:43



















          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%2f53307122%2freferencing-object-model-field-with-dictionary-key%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

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo