Associating ImageField url with another object in django











up vote
1
down vote

favorite












I have two models,



class Core:

def logo_path(instance, filename):
pattern = re.compile(r's+')
filename = re.sub(pattern, '', filename)
return 'images/core/logos/core_{0}/{1}'.format(instance.pk, filename)

name = models.CharField(db_index=True, max_length=255)
logo = models.ImageField(upload_to=logo_path, blank=True, null=True)


and



class CoreRoom(Room):
core = models.ForeignKey(Core, on_delete=models.CASCADE, null=True)
logo_url = models.CharField(max_length=1024, null=True)


I want to populate the logo_url in the CoreRoom object with the path to the logo in the Core object.
How do I do this?










share|improve this question




























    up vote
    1
    down vote

    favorite












    I have two models,



    class Core:

    def logo_path(instance, filename):
    pattern = re.compile(r's+')
    filename = re.sub(pattern, '', filename)
    return 'images/core/logos/core_{0}/{1}'.format(instance.pk, filename)

    name = models.CharField(db_index=True, max_length=255)
    logo = models.ImageField(upload_to=logo_path, blank=True, null=True)


    and



    class CoreRoom(Room):
    core = models.ForeignKey(Core, on_delete=models.CASCADE, null=True)
    logo_url = models.CharField(max_length=1024, null=True)


    I want to populate the logo_url in the CoreRoom object with the path to the logo in the Core object.
    How do I do this?










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have two models,



      class Core:

      def logo_path(instance, filename):
      pattern = re.compile(r's+')
      filename = re.sub(pattern, '', filename)
      return 'images/core/logos/core_{0}/{1}'.format(instance.pk, filename)

      name = models.CharField(db_index=True, max_length=255)
      logo = models.ImageField(upload_to=logo_path, blank=True, null=True)


      and



      class CoreRoom(Room):
      core = models.ForeignKey(Core, on_delete=models.CASCADE, null=True)
      logo_url = models.CharField(max_length=1024, null=True)


      I want to populate the logo_url in the CoreRoom object with the path to the logo in the Core object.
      How do I do this?










      share|improve this question















      I have two models,



      class Core:

      def logo_path(instance, filename):
      pattern = re.compile(r's+')
      filename = re.sub(pattern, '', filename)
      return 'images/core/logos/core_{0}/{1}'.format(instance.pk, filename)

      name = models.CharField(db_index=True, max_length=255)
      logo = models.ImageField(upload_to=logo_path, blank=True, null=True)


      and



      class CoreRoom(Room):
      core = models.ForeignKey(Core, on_delete=models.CASCADE, null=True)
      logo_url = models.CharField(max_length=1024, null=True)


      I want to populate the logo_url in the CoreRoom object with the path to the logo in the Core object.
      How do I do this?







      python django






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 3:15









      Selcuk

      25.8k75368




      25.8k75368










      asked Nov 8 at 2:21









      Melissa Stewart

      769622




      769622
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          I doubt this is needed as . you can always access it using my_core_room.core.logo.url, but the easiest way would be to override the .save() method of your CoreRoom model:



          class CoreRoom(Room):
          ...
          def save(self, *args, **kwargs):
          self.logo_url = self.core.logo.url
          super(CoreRoom, self).save(*args, **kwargs)





          share|improve this answer




























            up vote
            1
            down vote













            logo_url model field can be converted to property method as well, because, it is not necessary to save logo url information as it is already stored in DB. For example:



            class CoreRoom(Room):
            core = models.ForeignKey(Core, on_delete=models.CASCADE, null=True)
            # remove logo url model field

            @property
            def logo_url(self):
            return self.core.logo.url





            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',
              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%2f53200648%2fassociating-imagefield-url-with-another-object-in-django%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              2
              down vote



              accepted










              I doubt this is needed as . you can always access it using my_core_room.core.logo.url, but the easiest way would be to override the .save() method of your CoreRoom model:



              class CoreRoom(Room):
              ...
              def save(self, *args, **kwargs):
              self.logo_url = self.core.logo.url
              super(CoreRoom, self).save(*args, **kwargs)





              share|improve this answer

























                up vote
                2
                down vote



                accepted










                I doubt this is needed as . you can always access it using my_core_room.core.logo.url, but the easiest way would be to override the .save() method of your CoreRoom model:



                class CoreRoom(Room):
                ...
                def save(self, *args, **kwargs):
                self.logo_url = self.core.logo.url
                super(CoreRoom, self).save(*args, **kwargs)





                share|improve this answer























                  up vote
                  2
                  down vote



                  accepted







                  up vote
                  2
                  down vote



                  accepted






                  I doubt this is needed as . you can always access it using my_core_room.core.logo.url, but the easiest way would be to override the .save() method of your CoreRoom model:



                  class CoreRoom(Room):
                  ...
                  def save(self, *args, **kwargs):
                  self.logo_url = self.core.logo.url
                  super(CoreRoom, self).save(*args, **kwargs)





                  share|improve this answer












                  I doubt this is needed as . you can always access it using my_core_room.core.logo.url, but the easiest way would be to override the .save() method of your CoreRoom model:



                  class CoreRoom(Room):
                  ...
                  def save(self, *args, **kwargs):
                  self.logo_url = self.core.logo.url
                  super(CoreRoom, self).save(*args, **kwargs)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 8 at 2:41









                  Selcuk

                  25.8k75368




                  25.8k75368
























                      up vote
                      1
                      down vote













                      logo_url model field can be converted to property method as well, because, it is not necessary to save logo url information as it is already stored in DB. For example:



                      class CoreRoom(Room):
                      core = models.ForeignKey(Core, on_delete=models.CASCADE, null=True)
                      # remove logo url model field

                      @property
                      def logo_url(self):
                      return self.core.logo.url





                      share|improve this answer

























                        up vote
                        1
                        down vote













                        logo_url model field can be converted to property method as well, because, it is not necessary to save logo url information as it is already stored in DB. For example:



                        class CoreRoom(Room):
                        core = models.ForeignKey(Core, on_delete=models.CASCADE, null=True)
                        # remove logo url model field

                        @property
                        def logo_url(self):
                        return self.core.logo.url





                        share|improve this answer























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          logo_url model field can be converted to property method as well, because, it is not necessary to save logo url information as it is already stored in DB. For example:



                          class CoreRoom(Room):
                          core = models.ForeignKey(Core, on_delete=models.CASCADE, null=True)
                          # remove logo url model field

                          @property
                          def logo_url(self):
                          return self.core.logo.url





                          share|improve this answer












                          logo_url model field can be converted to property method as well, because, it is not necessary to save logo url information as it is already stored in DB. For example:



                          class CoreRoom(Room):
                          core = models.ForeignKey(Core, on_delete=models.CASCADE, null=True)
                          # remove logo url model field

                          @property
                          def logo_url(self):
                          return self.core.logo.url






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 8 at 3:05









                          ruddra

                          9,62332547




                          9,62332547






























                              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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2f53200648%2fassociating-imagefield-url-with-another-object-in-django%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







                              這個網誌中的熱門文章

                              Academy of Television Arts & Sciences

                              L'Équipe

                              1995 France bombings