Making queries

우리가 데이터 모델을 만들면, 장고는 우리에게 데이터베이스 추상화 API를 만들어줍니다.이 API는 우리가 객체를 만들고, 검색하고, 업데이트하고, 삭제할 수 있게 해줍니다.
Making queries에서는 이 API를 어떻게 다루는지 설명합니다.
다양한 모델을 조회하는 자세한 옵션에 대해서는 '데이터 모델 레퍼런스'를 참고하십시오.

이번 가이드와 레퍼런스에서 우리는 아래 예제를 계속 사용하겠습니다. 웹 로그 어플리케이션을 구성하는 예제입니다.

from django.db import models

# Blog, Author, Entry 세가지 클래스를 설정합니다.
# Entry는 축구선수 엔트리를 생각하면 될 것 같습니다.
class Blog(models.Model):
    name = models.CharField(max_length=100)
    tagline = models.TextField()

    def __str__(self):              # __unicode__ on Python 2
        return self.name

class Author(models.Model):
    name = models.CharField(max_length=200)
    email = models.EmailField()

    def __str__(self):              # __unicode__ on Python 2
        return self.name

class Entry(models.Model):
    blog = models.ForeignKey(Blog)
    headline = models.CharField(max_length=255)
    body_text = models.TextField()
    pub_date = models.DateField()
    mod_date = models.DateField()
    authors = models.ManyToManyField(Author)
    n_comments = models.IntegerField()
    n_pingbacks = models.IntegerField()
    rating = models.IntegerField()

    def __str__(self):              # __unicode__ on Python 2
        return self.headline

Creating objects

파이썬 객체 안의 데이터베이스-테이블 데이터를 나타내기 위해, 장고는 직관적인 시스템을 이용합니다. 하나의 모델 클래스는 하나의 데이터베이스-테이블을 나타냅니다.

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

Saving changes to objects

- Saving ForeignKey and ManyToManyField fields

Retrieving objects

- Retrieving all objects

Retrieving specific objects with filters

Limiting QuerySets

Field lookups

- exact
- iexact
- contains

Lookups that span relationships

- Spanning multi-valued relationships

filters can reference fields on the model

Caching and QuerySets

- When QuerySets are not cached

Complex lookups with Q objects.

Comparing objects

Updating multiple objects at on

Comparing objects

Deleting objects

Copying model instances

results matching ""

    No results matching ""