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;
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
add a comment |
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
add a comment |
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
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
django django-models django-rest-framework
asked Mar 25 at 14:47
iamdifferentiamdifferent
93 bronze badges
93 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
Hi, I understood your answer. So, I tried this.. but It's not working for me.. Can I use overridesavefor solve this problem? There are different betweensavefunction andpost_savesignal?
– iamdifferent
Mar 26 at 2:38
You can't usesaveeither. 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_savesignals 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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
Hi, I understood your answer. So, I tried this.. but It's not working for me.. Can I use overridesavefor solve this problem? There are different betweensavefunction andpost_savesignal?
– iamdifferent
Mar 26 at 2:38
You can't usesaveeither. 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_savesignals 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
add a comment |
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
Hi, I understood your answer. So, I tried this.. but It's not working for me.. Can I use overridesavefor solve this problem? There are different betweensavefunction andpost_savesignal?
– iamdifferent
Mar 26 at 2:38
You can't usesaveeither. 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_savesignals 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
add a comment |
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
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
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 overridesavefor solve this problem? There are different betweensavefunction andpost_savesignal?
– iamdifferent
Mar 26 at 2:38
You can't usesaveeither. 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_savesignals 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
add a comment |
Hi, I understood your answer. So, I tried this.. but It's not working for me.. Can I use overridesavefor solve this problem? There are different betweensavefunction andpost_savesignal?
– iamdifferent
Mar 26 at 2:38
You can't usesaveeither. 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_savesignals 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
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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