Django:model 클래스의 objects 속성
10670 단어 Django
Each QuerySet has the following methods, which return a clone of the query set with the appropriate changes made:
Here are some examples, which use the following models:
class Reporter(models.Model):
fname = models.CharField(maxlength=30)
lname = models.CharField(maxlength=30)
class Site(models.Model):
name = models.CharField(maxlength=20)
class Article(models.Model):
headline = models.CharField(maxlength=50)
reporter = models.ForeignKey(Reporter)
pub_date = models.DateField()
sites = models.ManyToManyField(Site)
Old syntax
New syntax
reporters.get_list()
Reporter.objects.all()
reporters.get_list(fname__exact='John')
Reporter.objects.filter(fname='John')
reporters.get_list(order_by=('-lname', 'fname'))
Reporter.objects.order_by('-lname', 'fname')
reporters.get_list(fname__exact='John', order_by=('lname',))
Reporter.objects.filter(fname='John').order_by('lname')
reporters.get_object(pk=3)
Reporter.objects.get(pk=3)
reporters.get_object(complex=(Q(...)|Q(...)))
Reporter.objects.get(Q(...)|Q(...))
reporters.get_object(fname__contains='John')
Reporter.objects.get(fname__contains='John')
reporters.get_list(fname__ne='John')
Reporter.objects.exclude(fname='John') (note that ne is no longer a valid lookup type)
(not previously possible)
Reporter.objects.exclude(fname__contains='n')
reporters.get_list(distinct=True)
Reporter.objects.distinct()
reporters.get_list(offset=10, limit=5)
Reporter.objects.all()[10:15]
reporters.get_values()
Reporter.objects.values()
reporters.get_in_bulk([1, 2])
Reporter.objects.in_bulk([1, 2])
reporters.get_in_bulk([1, 2], fname__exact='John')
Reporter.objects.filter(fname='John').in_bulk([1, 2])
Date lookup
articles.get_pub_date_list('year')
Article.objects.dates('pub_date', 'year')
Latest-object lookup
articles.get_latest() (required get_latest_by in model)
Article.objects.latest() (with get_latest_by in model)
(Not previously possible)
Article.objects.latest('pub_date') # Latest by pub_date (overrides get_latest_by field in model)
Many-to-one related lookup
article_obj.reporter_id
article_obj.reporter.id
article_obj.get_reporter()
article_obj.reporter
reporter_obj.get_article_list()
reporter_obj.article_set.all()
reporter_obj.get_article_list(headline__exact='Hello')
reporter_obj.article_set.filter(headline='Hello')
reporter_obj.get_article_count()
reporter_obj.article_set.count()
reporter_obj.add_article(headline='Foo')
reporter_obj.article_set.create(headline='Foo')
(Alternate syntax)
reporter_obj.article_set.add(article_obj)
("values"lookup, etc., not previously possible)
reporter_obj.article_set.values()
Many-to-many related lookup
article_obj.get_site_list()
article_obj.sites.all()
article_obj.set_sites([s1.id, s2.id])
article_obj.sites.clear(); article_obj.sites.add(s1); article_obj.sites.add(s2)
article_obj.set_sites([s1.id]) # deletion
article_obj.sites.remove(s2)
site_obj.get_article_list()
site_obj.article_set.all()
Note that related-object lookup uses the default manager of the related object, which means the API for accessing related objects is completely consistent with the API for accessing objects via a manager.
Also note that managers can't be accessed from instances:
p = Person.objects.get(pk=1)
p.objects.all() # Raises AttributeError
Override default manager name ('objects') ¶
If a model already has an objects attribute, you'll need to specify an alternate name for the objects manager.
class Person(models.Model):
first_name = models.CharField(maxlength=30)
last_name = models.CharField(maxlength=30)
objects = models.TextField()
people = models.Manager()
p = Person(first_name='Mary', last_name='Jones', objects='Hello there.')
p.save()
p.objects == 'Hello there.'
Person.people.all()
텍스트 링크:
http://hi.baidu.com/javalang/blog/item/7dd63c9bbf571db1c8eaf437.html
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Django 라우팅 계층 URLconf 작용 및 원리 해석URL 구성(URLconf)은 Django가 지원하는 웹 사이트의 디렉토리와 같습니다.그것의 본질은 URL과 이 URL을 호출할 보기 함수 사이의 맵표입니다. 위의 예제에서는 URL의 값을 캡처하고 위치 매개 변수로...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.