django model form fields = () behaviour

ZedsWhatSheSaid

What happens when I define a field in django forms like this:

class FooForm( ModelForm ):
  class Meta:
    model = FooModel
    fields = ( 'foo', 'bar', ) # I am talking about this field

When I have the fields foo, bar and baz in my models, what happens with baz then in this case?

Are these fields required or not automatically? are they just available? What is actually happening with that field when I am doing this?

knbk

When you use fields = ('foo', 'bar',), the field baz does not exist at all in the form, unless you set it explicitly (fields only "fills in" the missing model fields). When you use form.save(), baz is not set to a value on the model either. If the field is required on your model, your code must ensure that it is set before the model is saved to the database.

It is entirely possible, and quite often necessary, to have a required field that's not in the form. E.g. when you have to relate your model to the current user, you can remove the user field from your form, and use:

obj = form.save(commit=False)
obj.user = request.user
obj.save()

The important distinction here is that form fields are for user input, and model fields are for the data itself.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

django model form fields = () behaviour

From Dev

Django unique model fields validation in form

From Dev

Django model form not saving all fields to db

From Dev

Django Form, with Class Meta model based in class model, and more fields

From Dev

Weird behaviour of form and widget in Django

From Dev

Django custom form with non-model fields in Admin/AdminInline

From Dev

Django custom form with non-model fields in Admin/AdminInline

From Dev

Dynamic model fields in Django

From Dev

Django form fields will not display

From Dev

Customize Django Form fields

From Dev

Custom form fields in Django

From Dev

Django form fields will not display

From Dev

Django model extending Default User model (one-to-one field), how to create reg form for all combined model fields

From Dev

django model search form

From Dev

Django Model form is not shown

From Dev

Django Model Form not saving

From Dev

Attributes in django form model

From Dev

Django: Form Model Validation

From Dev

Does Django 1.7 has a automatic way to convert datetime fields from manual HTML form to model?

From Dev

django is_valid returns false for model form even though all fields have a value

From Dev

Does Django 1.7 has a automatic way to convert datetime fields from manual HTML form to model?

From Dev

Django Bootstrap input-group-addon on Model Form without separating fields

From Java

how to sum in django model fields

From Dev

Django model inheritance, overriding fields

From Dev

Django listing model fields in order

From Dev

access model fields and attributes in django

From Dev

Django checking if model fields are equal

From Dev

Django model inheritance and hasattr() on fields

From Dev

Django 1.6: model fields not showing

Related Related

  1. 1

    django model form fields = () behaviour

  2. 2

    Django unique model fields validation in form

  3. 3

    Django model form not saving all fields to db

  4. 4

    Django Form, with Class Meta model based in class model, and more fields

  5. 5

    Weird behaviour of form and widget in Django

  6. 6

    Django custom form with non-model fields in Admin/AdminInline

  7. 7

    Django custom form with non-model fields in Admin/AdminInline

  8. 8

    Dynamic model fields in Django

  9. 9

    Django form fields will not display

  10. 10

    Customize Django Form fields

  11. 11

    Custom form fields in Django

  12. 12

    Django form fields will not display

  13. 13

    Django model extending Default User model (one-to-one field), how to create reg form for all combined model fields

  14. 14

    django model search form

  15. 15

    Django Model form is not shown

  16. 16

    Django Model Form not saving

  17. 17

    Attributes in django form model

  18. 18

    Django: Form Model Validation

  19. 19

    Does Django 1.7 has a automatic way to convert datetime fields from manual HTML form to model?

  20. 20

    django is_valid returns false for model form even though all fields have a value

  21. 21

    Does Django 1.7 has a automatic way to convert datetime fields from manual HTML form to model?

  22. 22

    Django Bootstrap input-group-addon on Model Form without separating fields

  23. 23

    how to sum in django model fields

  24. 24

    Django model inheritance, overriding fields

  25. 25

    Django listing model fields in order

  26. 26

    access model fields and attributes in django

  27. 27

    Django checking if model fields are equal

  28. 28

    Django model inheritance and hasattr() on fields

  29. 29

    Django 1.6: model fields not showing

HotTag

Archive