ដក​ស្រង់​ពី​​ឯកសារ​ផ្លូការ​របស់​មូលនិធិ Django

 

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.

 

Throughout this guide (and in the reference), we’ll refer to the following models, which comprise a Weblog application:

 

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()
    authors = models.ManyToManyField(Author)
    number_of_comments = models.IntegerField()
    number_of_pingbacks = models.IntegerField()
    rating = models.IntegerField()

    def __str__(self):
        return self.headline

 

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 save() to save it to the database.

 

Assuming models live in a file mysite/blog/models.py, here’s an example:

 

>>> from blog.models import Blog
>>> b = Blog(name='Beatles Blog', tagline='All the latest Beatles news.')
>>> b.save()

 

This performs an INSERT SQL statement behind the scenes. Django doesn’t hit the database until you explicitly call save().

 

The save() method has no return value.

 

To save changes to an object that’s already in the database, use save().

 

Given a Blog instance b5 that has already been saved to the database, this example changes its name and updates its record in the database:

 

>>> b5.name = 'New name'
>>> b5.save()

 

This performs an UPDATE SQL statement behind the scenes. Django doesn’t hit the database until you explicitly call save().

 

Updating a ForeignKey field works exactly the same way as saving a normal field – assign an object of the right type to the field in question. This example updates the blog attribute of an Entry instance entry, assuming appropriate instances of Entry and Blog are already saved to the database (so we can retrieve them below):

 

>>> 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 ManyToManyField works a little differently – use the add() method on the field to add a record to the relation. This example adds the Author instance joe to the entry object:

 

>>> from blog.models import Author
>>> joe = Author.objects.create(name="Joe")
>>> entry.authors.add(joe)

 

To add multiple records to a ManyToManyField in one go, include multiple arguments in the call to add(), like this:

 

>>> 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.