python
Create or save object data in Django
October 24, 2022
You can use the code examples explained in this post to create and save data in Django. You just need to create the instance of the model in which you want to put the data.
//ONE LINE SYNTAX TO SAVE DATA
person = Person.objects.create(first_name="John", last_name="Deo")
//YOU CAN ALSO USE BELOW CODE TO SAVE DATA
person = Person(first_name="John", last_name="Deo")
person.save()
Creating or saving object data in Django is very simple and it can be achieved using the above code snippet.
In the code snippet, we are using a 'Person' model which has fields named 'first_name' and 'last_name'.
The above code will insert data into the table where 'John' will be inserted in the table.
You can create the Person model using the below code.
from django.db import models
class Person(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
Was this helpful?
Similar Posts