Skip to content

Chapter 4 Models

Bucephalus1 edited this page Nov 6, 2017 · 1 revision

Models##

models represent the database structure

Classes = db table class attribute = table column

Models API

Class.objects.all() = returns all publishers from the database

CreateObject = Class.objects.create() / This creates an object and saves directly to database in one command
CreateObject = Class() / Creates object but does not save SaveObject = Class.objects.save() = This saves all changes to the Db by updating all columns in a row UpdateObject = Class.objects.filter(id=52).update(name=" ") = This updates only one field
DeleteObject = Class.objects.get(" ").delete() FilterDb = Class.objects.filter() = This returns a queryset. Useful for returning more than one FilterDb = Class.objects.get() = returns single row from database
SortResults = Class.objects.order_by("attribute1",'att2') RevResults = Class.objects.order_by('-attribute1')

You can also set a default order by adding a class Meta inside the database table class class Publisher(models.Model): class Meta: ordering = ['name']

Clone this wiki locally