FileField with instance id in DRFDjango admin file upload with current model idShow information of subclass in list_display djangodjango rest framework return selected field in viewHow to serialize GenericForeignKey in DRF?Serializing custom related field in DRFModify ImageField upload_to method to work dynamicallyhow to properly override serializer save on a POST request in DRF?Django not recognizing field override for Current User in SerializerIncorrect path to save the file in DjangoHow to implement update_or_create inside create method of ModelSerializerHow to Create a Django Model Instance That Contains a Required OneToOneField with DRF Serializers

Closest Proximity of Oceans to Freshwater Springs

Copy group of files (Filename*) to backup (Filename*.bak)

Can a successful book series let the bad guy win?

My colleague is constantly blaming me for his errors

Using If statements to conditionally populate a field in Field Calculator

How can I know if a PDF file was created via LaTeX or XeLaTeX?

Does turbulence make sky cities infeasible on Venus?

If I were to build a J3 cub twice the size of the original using the same CG would it fly?

Most elegant way to write a one-shot 'if'

Traversing Eurasia: A Cryptic Journey

Reusable spacecraft: why still have fairings detach, instead of open/close?

Should 私の be omitted?

Checkmate in 1 on a Tangled Board

Conference in Los Angeles, visa?

On what to compliment someone with anorexia in order to improve their body image?

How could an armless race establish civilization?

Why did the Apple //e make a hideous noise if you inserted the disk upside down?

Why wasn't ASCII designed with a contiguous alphanumeric character order?

What do you call a notepad used to keep a record?

Have any large aeroplanes been landed — safely and without damage — in locations that they could not be flown away from?

13th chords on guitar

Adjective for 'made of pus' or 'corrupted by pus' or something of something of pus

How do I ensure my employees don't abuse my flexible work hours policy?

Could human civilization live 150 years in a nuclear-powered aircraft carrier colony without resorting to mass killing/ cannibalism?



FileField with instance id in DRF


Django admin file upload with current model idShow information of subclass in list_display djangodjango rest framework return selected field in viewHow to serialize GenericForeignKey in DRF?Serializing custom related field in DRFModify ImageField upload_to method to work dynamicallyhow to properly override serializer save on a POST request in DRF?Django not recognizing field override for Current User in SerializerIncorrect path to save the file in DjangoHow to implement update_or_create inside create method of ModelSerializerHow to Create a Django Model Instance That Contains a Required OneToOneField with DRF Serializers






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I don't know why filefield return string that include None when post data.



like this




http://example.com/room/None/1.jpg




My instance id was null on save.



In models.py



def room_photo_path(instance, filename):
return 'room//'.format(instance.id, filename)


class Room(models.Model):
photo = models.FileField(upload_to=room_photo_path)


In serializers.py



class RoomSerializer(serializers.ModelSerializer):
class Meta:
model = Room
fields = '__all__'









share|improve this question




























    0















    I don't know why filefield return string that include None when post data.



    like this




    http://example.com/room/None/1.jpg




    My instance id was null on save.



    In models.py



    def room_photo_path(instance, filename):
    return 'room//'.format(instance.id, filename)


    class Room(models.Model):
    photo = models.FileField(upload_to=room_photo_path)


    In serializers.py



    class RoomSerializer(serializers.ModelSerializer):
    class Meta:
    model = Room
    fields = '__all__'









    share|improve this question
























      0












      0








      0








      I don't know why filefield return string that include None when post data.



      like this




      http://example.com/room/None/1.jpg




      My instance id was null on save.



      In models.py



      def room_photo_path(instance, filename):
      return 'room//'.format(instance.id, filename)


      class Room(models.Model):
      photo = models.FileField(upload_to=room_photo_path)


      In serializers.py



      class RoomSerializer(serializers.ModelSerializer):
      class Meta:
      model = Room
      fields = '__all__'









      share|improve this question














      I don't know why filefield return string that include None when post data.



      like this




      http://example.com/room/None/1.jpg




      My instance id was null on save.



      In models.py



      def room_photo_path(instance, filename):
      return 'room//'.format(instance.id, filename)


      class Room(models.Model):
      photo = models.FileField(upload_to=room_photo_path)


      In serializers.py



      class RoomSerializer(serializers.ModelSerializer):
      class Meta:
      model = Room
      fields = '__all__'






      django django-models django-rest-framework






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 25 at 14:47









      iamdifferentiamdifferent

      93 bronze badges




      93 bronze badges






















          1 Answer
          1






          active

          oldest

          votes


















          2














          id gets assigned in database so the instance needs to be saved to have an id.



          upload_to function gets called before the object is saved to database so you can't access the id because there is no id yet.



          You can use signals and use a post_save signal to make the changes if you really have to use ids.



          Django docs: Signals






          share|improve this answer























          • Hi, I understood your answer. So, I tried this.. but It's not working for me.. Can I use override save for solve this problem? There are different between save function and post_save signal?

            – iamdifferent
            Mar 26 at 2:38












          • You can't use save either. id gets assigned after save method. Basically you won't have an id unless the database gets the data and save them which means there is nothing you can do in django level. post_save signals sends a signal to a function after save so you can make changes while you have the id too.

            – Navid Zarepak
            Mar 26 at 7:13










          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%2f55340475%2ffilefield-with-instance-id-in-drf%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









          2














          id gets assigned in database so the instance needs to be saved to have an id.



          upload_to function gets called before the object is saved to database so you can't access the id because there is no id yet.



          You can use signals and use a post_save signal to make the changes if you really have to use ids.



          Django docs: Signals






          share|improve this answer























          • Hi, I understood your answer. So, I tried this.. but It's not working for me.. Can I use override save for solve this problem? There are different between save function and post_save signal?

            – iamdifferent
            Mar 26 at 2:38












          • You can't use save either. id gets assigned after save method. Basically you won't have an id unless the database gets the data and save them which means there is nothing you can do in django level. post_save signals sends a signal to a function after save so you can make changes while you have the id too.

            – Navid Zarepak
            Mar 26 at 7:13















          2














          id gets assigned in database so the instance needs to be saved to have an id.



          upload_to function gets called before the object is saved to database so you can't access the id because there is no id yet.



          You can use signals and use a post_save signal to make the changes if you really have to use ids.



          Django docs: Signals






          share|improve this answer























          • Hi, I understood your answer. So, I tried this.. but It's not working for me.. Can I use override save for solve this problem? There are different between save function and post_save signal?

            – iamdifferent
            Mar 26 at 2:38












          • You can't use save either. id gets assigned after save method. Basically you won't have an id unless the database gets the data and save them which means there is nothing you can do in django level. post_save signals sends a signal to a function after save so you can make changes while you have the id too.

            – Navid Zarepak
            Mar 26 at 7:13













          2












          2








          2







          id gets assigned in database so the instance needs to be saved to have an id.



          upload_to function gets called before the object is saved to database so you can't access the id because there is no id yet.



          You can use signals and use a post_save signal to make the changes if you really have to use ids.



          Django docs: Signals






          share|improve this answer













          id gets assigned in database so the instance needs to be saved to have an id.



          upload_to function gets called before the object is saved to database so you can't access the id because there is no id yet.



          You can use signals and use a post_save signal to make the changes if you really have to use ids.



          Django docs: Signals







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 25 at 14:59









          Navid ZarepakNavid Zarepak

          1,8401 gold badge6 silver badges18 bronze badges




          1,8401 gold badge6 silver badges18 bronze badges












          • Hi, I understood your answer. So, I tried this.. but It's not working for me.. Can I use override save for solve this problem? There are different between save function and post_save signal?

            – iamdifferent
            Mar 26 at 2:38












          • You can't use save either. id gets assigned after save method. Basically you won't have an id unless the database gets the data and save them which means there is nothing you can do in django level. post_save signals sends a signal to a function after save so you can make changes while you have the id too.

            – Navid Zarepak
            Mar 26 at 7:13

















          • Hi, I understood your answer. So, I tried this.. but It's not working for me.. Can I use override save for solve this problem? There are different between save function and post_save signal?

            – iamdifferent
            Mar 26 at 2:38












          • You can't use save either. id gets assigned after save method. Basically you won't have an id unless the database gets the data and save them which means there is nothing you can do in django level. post_save signals sends a signal to a function after save so you can make changes while you have the id too.

            – Navid Zarepak
            Mar 26 at 7:13
















          Hi, I understood your answer. So, I tried this.. but It's not working for me.. Can I use override save for solve this problem? There are different between save function and post_save signal?

          – iamdifferent
          Mar 26 at 2:38






          Hi, I understood your answer. So, I tried this.. but It's not working for me.. Can I use override save for solve this problem? There are different between save function and post_save signal?

          – iamdifferent
          Mar 26 at 2:38














          You can't use save either. id gets assigned after save method. Basically you won't have an id unless the database gets the data and save them which means there is nothing you can do in django level. post_save signals sends a signal to a function after save so you can make changes while you have the id too.

          – Navid Zarepak
          Mar 26 at 7:13





          You can't use save either. id gets assigned after save method. Basically you won't have an id unless the database gets the data and save them which means there is nothing you can do in django level. post_save signals sends a signal to a function after save so you can make changes while you have the id too.

          – Navid Zarepak
          Mar 26 at 7:13








          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















          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%2f55340475%2ffilefield-with-instance-id-in-drf%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

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

          155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해