django form not saving updated value












0















I have the following form save process in my view:



if request.method == "POST":
form = PersonDetailForm(request.POST, request.FILES, instance=customer)
if form.is_valid():
print(form.cleaned_data['first_name']) //prints corrected updated value
saved_customer = form.save()
print(saved_customer.first_name) //prints the old wrong value


My model looks like this:



class Customer(models.Model): 
user = models.OneToOneField(User, blank=True, null=True, on_delete=models.CASCADE)
first_name = models.CharField(max_length=50, blank=True, null=True)
last_name = models.CharField(max_length=150, blank=True, null=True)


Here is my form class:



class PersonDetailForm(forms.ModelForm):


class Meta:
model = Customer
fields = (
'first_name',
'last_name'
)


The problem is that the form is not saving the updated first name.



When I print the cleaned_data is prints the correct updated value, but when I print the result of the saved form (saved_customer) it prints the old first name.



What am I doing wrong?










share|improve this question

























  • What does your model look like?

    – Bott0610
    Nov 15 '18 at 18:05











  • did you try to print the request.POST to see whether it has the post values...

    – Lemayzeur
    Nov 15 '18 at 18:07











  • please write your form class in question

    – Kusko Andrew
    Nov 15 '18 at 18:10













  • @KuskoAndrew I added it

    – Atma
    Nov 15 '18 at 18:13
















0















I have the following form save process in my view:



if request.method == "POST":
form = PersonDetailForm(request.POST, request.FILES, instance=customer)
if form.is_valid():
print(form.cleaned_data['first_name']) //prints corrected updated value
saved_customer = form.save()
print(saved_customer.first_name) //prints the old wrong value


My model looks like this:



class Customer(models.Model): 
user = models.OneToOneField(User, blank=True, null=True, on_delete=models.CASCADE)
first_name = models.CharField(max_length=50, blank=True, null=True)
last_name = models.CharField(max_length=150, blank=True, null=True)


Here is my form class:



class PersonDetailForm(forms.ModelForm):


class Meta:
model = Customer
fields = (
'first_name',
'last_name'
)


The problem is that the form is not saving the updated first name.



When I print the cleaned_data is prints the correct updated value, but when I print the result of the saved form (saved_customer) it prints the old first name.



What am I doing wrong?










share|improve this question

























  • What does your model look like?

    – Bott0610
    Nov 15 '18 at 18:05











  • did you try to print the request.POST to see whether it has the post values...

    – Lemayzeur
    Nov 15 '18 at 18:07











  • please write your form class in question

    – Kusko Andrew
    Nov 15 '18 at 18:10













  • @KuskoAndrew I added it

    – Atma
    Nov 15 '18 at 18:13














0












0








0








I have the following form save process in my view:



if request.method == "POST":
form = PersonDetailForm(request.POST, request.FILES, instance=customer)
if form.is_valid():
print(form.cleaned_data['first_name']) //prints corrected updated value
saved_customer = form.save()
print(saved_customer.first_name) //prints the old wrong value


My model looks like this:



class Customer(models.Model): 
user = models.OneToOneField(User, blank=True, null=True, on_delete=models.CASCADE)
first_name = models.CharField(max_length=50, blank=True, null=True)
last_name = models.CharField(max_length=150, blank=True, null=True)


Here is my form class:



class PersonDetailForm(forms.ModelForm):


class Meta:
model = Customer
fields = (
'first_name',
'last_name'
)


The problem is that the form is not saving the updated first name.



When I print the cleaned_data is prints the correct updated value, but when I print the result of the saved form (saved_customer) it prints the old first name.



What am I doing wrong?










share|improve this question
















I have the following form save process in my view:



if request.method == "POST":
form = PersonDetailForm(request.POST, request.FILES, instance=customer)
if form.is_valid():
print(form.cleaned_data['first_name']) //prints corrected updated value
saved_customer = form.save()
print(saved_customer.first_name) //prints the old wrong value


My model looks like this:



class Customer(models.Model): 
user = models.OneToOneField(User, blank=True, null=True, on_delete=models.CASCADE)
first_name = models.CharField(max_length=50, blank=True, null=True)
last_name = models.CharField(max_length=150, blank=True, null=True)


Here is my form class:



class PersonDetailForm(forms.ModelForm):


class Meta:
model = Customer
fields = (
'first_name',
'last_name'
)


The problem is that the form is not saving the updated first name.



When I print the cleaned_data is prints the correct updated value, but when I print the result of the saved form (saved_customer) it prints the old first name.



What am I doing wrong?







django django-forms






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 18:12







Atma

















asked Nov 15 '18 at 17:48









AtmaAtma

12.9k43142229




12.9k43142229













  • What does your model look like?

    – Bott0610
    Nov 15 '18 at 18:05











  • did you try to print the request.POST to see whether it has the post values...

    – Lemayzeur
    Nov 15 '18 at 18:07











  • please write your form class in question

    – Kusko Andrew
    Nov 15 '18 at 18:10













  • @KuskoAndrew I added it

    – Atma
    Nov 15 '18 at 18:13



















  • What does your model look like?

    – Bott0610
    Nov 15 '18 at 18:05











  • did you try to print the request.POST to see whether it has the post values...

    – Lemayzeur
    Nov 15 '18 at 18:07











  • please write your form class in question

    – Kusko Andrew
    Nov 15 '18 at 18:10













  • @KuskoAndrew I added it

    – Atma
    Nov 15 '18 at 18:13

















What does your model look like?

– Bott0610
Nov 15 '18 at 18:05





What does your model look like?

– Bott0610
Nov 15 '18 at 18:05













did you try to print the request.POST to see whether it has the post values...

– Lemayzeur
Nov 15 '18 at 18:07





did you try to print the request.POST to see whether it has the post values...

– Lemayzeur
Nov 15 '18 at 18:07













please write your form class in question

– Kusko Andrew
Nov 15 '18 at 18:10







please write your form class in question

– Kusko Andrew
Nov 15 '18 at 18:10















@KuskoAndrew I added it

– Atma
Nov 15 '18 at 18:13





@KuskoAndrew I added it

– Atma
Nov 15 '18 at 18:13












1 Answer
1






active

oldest

votes


















0














Not sure what the problem is, but you could try:



if form.is_valid():
f_customer = form.save(commit=False)
f_customer.first_name = form.cleaned_data.get('first_name')
f_customer.save()


If that still doesn't work, then I would think something is wrong with your Customer.save() or maybe you have a pre_save receiver set up doing something unexpected.






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',
    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%2f53325227%2fdjango-form-not-saving-updated-value%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









    0














    Not sure what the problem is, but you could try:



    if form.is_valid():
    f_customer = form.save(commit=False)
    f_customer.first_name = form.cleaned_data.get('first_name')
    f_customer.save()


    If that still doesn't work, then I would think something is wrong with your Customer.save() or maybe you have a pre_save receiver set up doing something unexpected.






    share|improve this answer




























      0














      Not sure what the problem is, but you could try:



      if form.is_valid():
      f_customer = form.save(commit=False)
      f_customer.first_name = form.cleaned_data.get('first_name')
      f_customer.save()


      If that still doesn't work, then I would think something is wrong with your Customer.save() or maybe you have a pre_save receiver set up doing something unexpected.






      share|improve this answer


























        0












        0








        0







        Not sure what the problem is, but you could try:



        if form.is_valid():
        f_customer = form.save(commit=False)
        f_customer.first_name = form.cleaned_data.get('first_name')
        f_customer.save()


        If that still doesn't work, then I would think something is wrong with your Customer.save() or maybe you have a pre_save receiver set up doing something unexpected.






        share|improve this answer













        Not sure what the problem is, but you could try:



        if form.is_valid():
        f_customer = form.save(commit=False)
        f_customer.first_name = form.cleaned_data.get('first_name')
        f_customer.save()


        If that still doesn't work, then I would think something is wrong with your Customer.save() or maybe you have a pre_save receiver set up doing something unexpected.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 18:40









        Bott0610Bott0610

        618312




        618312
































            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%2f53325227%2fdjango-form-not-saving-updated-value%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

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python