So far, all we’ve been doing is querying data. Now, lets see how we can insert objects into the database. We need to create a collection object
from django.shortcuts import renderfrom store.models import Collection
def say_hello(request): collection = Collection() # this is the collection object collection.title = "Video Games" collection.featured_product = Product(pk=1) # this does the same thing as the ff line # collection.featured_product_id = 1 # this does the same thing as the above line collection.save() #this will save to the db
# instead of doing all the above lines, we can simply do this below collection = Collection.objects.create(title='Video Games', featured_product_id=1) pass