Saving additional information to the database conditionally when using a modelfrom in django

user2959896

I have a modelform and aside from the fields on the form i need to update one more column in the database depending on if there is a registered user submitting information or not. I read that you can use the save method with the argument "commit=False" which will create the database object but not save it to the database so i can check if the user is logged in and then also save additional information if i need to, and then call save myself. Does this seem like a reasonable approach to solve this problem ?

Daniel Roseman

Yes, that is exactly the right thing to do.

if form.is_valid():
    object = form.save(commit=False)
    if request.user.is_authenticated():
        object.user = request.user
    object.save()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to insert additional information when additional rows is added to database

From Dev

How to add a datepicker into Django ModelFrom?

From Dev

Date not saving to database when using form_for

From Dev

Django Form not saving when using a custom field

From Dev

Form information not saving into database with rails

From Dev

saving information to database from webform

From Dev

Django: Saving to database connundrun

From Dev

Django Saving Data into Database

From Dev

Is Django corrupting timezone-aware DateTimeField when saving it to the Database?

From Dev

How to send image file without saving in database using django framework

From Dev

Where to Perform Additional Operations When Using Django Rest Framework

From Dev

Django: When saving issue

From Dev

Django Querysets adding additional information inside View

From Dev

Cancel saving model when using pre_save in django

From Dev

Saving django test database in a fixture?

From Dev

Django Admin not saving data to the Database

From Dev

Django ModelForm not saving data to database

From Dev

Django model prevent saving to database

From Dev

MVVM Saving in database using ICommand

From Dev

Using Django's selected_related, but still resulting in additional database hit

From Dev

Saving delete information to temp tables using into

From Dev

Conditionally including additional characters using python string formatting

From Dev

Additional non model database connections in Django

From Dev

additional column when saving pandas data frame to csv file

From Dev

Yii2 database session - store additional attributes and user information

From Dev

How to add additional information to Axapta Database without administrative privileges?

From Dev

Accessing additional properties of an Identity user when retrieving data from the database using EF and Linq

From Dev

Django - Saving a model using Admin

From Java

What would be the best practice to merge additional variables to data based on specific row information when web scraping in R using 'rvest'?

Related Related

  1. 1

    How to insert additional information when additional rows is added to database

  2. 2

    How to add a datepicker into Django ModelFrom?

  3. 3

    Date not saving to database when using form_for

  4. 4

    Django Form not saving when using a custom field

  5. 5

    Form information not saving into database with rails

  6. 6

    saving information to database from webform

  7. 7

    Django: Saving to database connundrun

  8. 8

    Django Saving Data into Database

  9. 9

    Is Django corrupting timezone-aware DateTimeField when saving it to the Database?

  10. 10

    How to send image file without saving in database using django framework

  11. 11

    Where to Perform Additional Operations When Using Django Rest Framework

  12. 12

    Django: When saving issue

  13. 13

    Django Querysets adding additional information inside View

  14. 14

    Cancel saving model when using pre_save in django

  15. 15

    Saving django test database in a fixture?

  16. 16

    Django Admin not saving data to the Database

  17. 17

    Django ModelForm not saving data to database

  18. 18

    Django model prevent saving to database

  19. 19

    MVVM Saving in database using ICommand

  20. 20

    Using Django's selected_related, but still resulting in additional database hit

  21. 21

    Saving delete information to temp tables using into

  22. 22

    Conditionally including additional characters using python string formatting

  23. 23

    Additional non model database connections in Django

  24. 24

    additional column when saving pandas data frame to csv file

  25. 25

    Yii2 database session - store additional attributes and user information

  26. 26

    How to add additional information to Axapta Database without administrative privileges?

  27. 27

    Accessing additional properties of an Identity user when retrieving data from the database using EF and Linq

  28. 28

    Django - Saving a model using Admin

  29. 29

    What would be the best practice to merge additional variables to data based on specific row information when web scraping in R using 'rvest'?

HotTag

Archive