#11 Django tutorials | How to add Initial values to Django Model forms | Default values for forms

Опубликовано: 19 Март 2026
на канале: CodeShika
3,183
55

#11 Django tutorials | How to add Initial values to Django Model forms | Default values for forms,    • #11 Django tutorials | How to add Initial ...  .In this tutorial, I will teach you how to set initial data to Django forms. There are two ways to set initial data:
1. Using Python dictionary
2. Using Django Model instance

For first method, you need to initialize the form for the GET request like
form = PersonForm(initial=person_data)

where person_data is the dictionary of form data and PersonForm is the Django form

For second method, first get the model instance based on primary key
Ex. person_instance = Person.objects.get(id=1)

Above gets the Person instance having the primary key ie. ID=1
Next, initialize the form
form = PersonForm(instance=person_instance)

Watch my recent tutorials here.

#10 Django tutorials | Setup International Phone number widget using django-phonenumber-field
   • #10 Django tutorials | Setup International...  

#6 Django Oscar 2.1 tutorials - Add Google Login to signup user in Django Oscar
   • #6 Django Oscar 2.1 tutorials - Add Google...  

Django Testing Tutorials- How to test Inclusion Template Tags |Test Custom template tags in Django#6
   • Django Testing Tutorials- How to test Incl...  

watch all Django Tutorials here.

Django tutorials
   • Django tutorials  

Django Testing Tutorials using unittest
   • Django Testing Tutorials using unittest  

Watch Django Crispy Form tutorials here.

Django Crispy Forms
   • Django Crispy Forms  

Watch all parts of Django Razorpay Integration in the following series.

Integrate Razorpay with Django
   • Integrate Razorpay with Django  

Watch all my Django Oscar tutorials for django-oscar version 2.1 from following series

Django Oscar 2.1 tutorials
   • Django Oscar 2.1 tutorials  

Watch my Django Oscar tutorials for django-oscar version 1.6 from following series

Django Oscar E-commerce tutorials for beginners in English
   • Django Oscar E-commerce tutorials for begi...  

Watch my django-oscar-accounts package tutorials i.e. a managed account for django-oscar here:

Django Oscar Accounts 2.1 tutorials for beginners
   • Django Oscar Accounts 2.1 tutorials for be...  

Watch my Django Ajax tutorials in the following series

Django Ajax tutorials for begginers
   • Django Ajax tutorials for begginers  

Friends, if you like this tutorial, please hit the like button and share, and if you want more tutorials like this, please subscribe to my channel CodeShika.


#django_phonenumber
#CodeShika