What changes does the database documenter feature make to the objects in the database?
Making queries¶Once you’ve created your data models, Django automatically gives you a database-abstraction API that lets you create, retrieve, update and delete objects. This document explains how to use this API. Refer to the data model reference for full details of all the various model lookup options. Show
Throughout this guide (and in the reference), we’ll refer to the following models, which comprise a blog application: from datetime import date from django.db import models class Blog(models.Model): name = models.CharField(max_length=100) tagline = models.TextField() def __str__(self): return self.name class Author(models.Model): name = models.CharField(max_length=200) email = models.EmailField() def __str__(self): return self.name class Entry(models.Model): blog = models.ForeignKey(Blog, on_delete=models.CASCADE) headline = models.CharField(max_length=255) body_text = models.TextField() pub_date = models.DateField() mod_date = models.DateField(default=date.today) authors = models.ManyToManyField(Author) number_of_comments = models.IntegerField(default=0) number_of_pingbacks = models.IntegerField(default=0) rating = models.IntegerField(default=5) def __str__(self): return self.headline Creating objects¶To represent database-table data in Python objects, Django uses an intuitive system: A model class represents a database table, and an instance of that class represents a particular record in the database table. To create an object, instantiate it using keyword arguments to the model class, then call
Assuming models live in a file >>> from blog.models import Blog >>> b = Blog(name='Beatles Blog', tagline='All the latest Beatles news.') >>> b.save() This performs an The See also
To create and save an object in a single step, use the
Saving changes to objects¶To save changes to an object that’s already in the database, use Given a >>> b5.name = 'New name' >>> b5.save() This performs an Saving ForeignKey and ManyToManyField fields¶Updating a
>>> from blog.models import Blog, Entry >>> entry = Entry.objects.get(pk=1) >>> cheese_blog = Blog.objects.get(name="Cheddar Talk") >>> entry.blog = cheese_blog >>> entry.save() Updating a >>> from blog.models import Author >>> joe = Author.objects.create(name="Joe") >>> entry.authors.add(joe) To add multiple records to a >>> john = Author.objects.create(name="John") >>> paul = Author.objects.create(name="Paul") >>> george = Author.objects.create(name="George") >>> ringo = Author.objects.create(name="Ringo") >>> entry.authors.add(john, paul, george, ringo) Django will complain if you try to assign or add an object of the wrong type. Retrieving objects¶To retrieve objects from your database, construct a A You get a
>>> Blog.objects Note
The Retrieving all objects¶The simplest way to retrieve objects from a table is to get
all of them. To do this, use the >>> all_entries = Entry.objects.all() The
Retrieving specific objects with filters¶The To create such a subset, you refine the initial
filter(**kwargs) Returns a new
QuerySet containing objects that match the given lookup parameters.exclude(**kwargs) Returns a new QuerySet containing objects that do not match the given
lookup parameters.The lookup parameters ( For example, to get a Entry.objects.filter(pub_date__year=2006) With the default manager class, it is the same as: Entry.objects.all().filter(pub_date__year=2006) Chaining filters¶The result of
refining a >>> Entry.objects.filter( ... headline__startswith='What' ... ).exclude( ... pub_date__gte=datetime.date.today() ... ).filter( ... pub_date__gte=datetime.date(2005, 1, 30) ... ) This takes the initial
Filtered |