Skip to content

DatabaseQueryCookbook

rjsparks edited this page May 16, 2013 · 6 revisions

Cookbook for common database querys

Fetching a particular document

Documents can have many names. For example, a given document may be known as an RFC or as an internet draft. The DocAlias objects contain these names.

  > from ietf.doc.models import *
  > d = DocAlias.objects.get(name='rfc5470')
  > d
  <DocAlias: rfc5470-->draft-ietf-ipfix-architecture>
  > d.document
  <Document: draft-ietf-ipfix-architecture>

If you just need the document, you can get it directly through Django's join syntax:

  > from ietf.doc.models import *
  > d = Document.objects.get(docalias__name='rfc5470')

Finding the documents currently in IETF LC (Querying for a particular state)

Document states have a type and a slug:

>>> s = State.objects.get(type="draft-iesg", slug="lc")
>>> s
<State: In Last Call>

Since a document can have multiple types of states associated with it, querying is through a many-to-many relation:

>>> docs = Document.objects.filter(type="draft", states__type="draft-iesg", states__slug="lc")
>>> len(docs)
26
>>> docs[0].get_state("draft-iesg")
<State: In Last Call>
>>> docs[0].get_state_slug("draft-iesg")
u'lc'

Working with related documents

To find the set of documents this document replaces:

>>> doc=Document.objects.get(docalias__name="draft-ietf-ecrit-lost-sync")

>>> doc.relateddocument_set.filter(relationship='replaces')
[<RelatedDocument: draft-ietf-ecrit-lost-sync replaces draft-schulzrinne-ecrit-lost-sync>]

>>> [d.target.document for d in doc.relateddocument_set.filter(relationship='replaces')]
[<Document: draft-schulzrinne-ecrit-lost-sync>]

Following the relationship the other direction (that is, finding the documents that replaced a given document) is a little more complicated:

>>> ind_doc=Document.objects.get(docalias__name="draft-schulzrinne-ecrit-lost-sync")

>>> [ rel.source for alias in ind_doc.docalias_set.all() for rel in alias.relateddocument_set.filter(relationship='replaces') ]
[<Document: draft-ietf-ecrit-lost-sync>]

Working with the current set of ADs

>>> from ietf.group.models import Role
>>> Role.objects.filter(name__slug='ad',group__state__slug='active')
[<Role: Pete Resnick is Area Director in app>, <Role: Stephen Farrell is Area Director in sec>, <Role: Stewart Bryant is Area Director in rtg>, <Role: Sean Turner is Area Director in sec>, <Role: Gonzalo Camarillo is Area Director in rai>, <Role: Adrian Farrel is Area Director in rtg>, <Role: Martin Stiemerling is Area Director in tsv>, <Role: Benoit Claise is Area Director in ops>, <Role: Brian Haberman is Area Director in int>, <Role: Barry Leiba is Area Director in app>, <Role: Jari Arkko is Area Director in gen>, <Role: Ted Lemon is Area Director in int>, <Role: Joel Jaeggli is Area Director in ops>, <Role: Spencer Dawkins is Area Director in tsv>, <Role: Richard Barnes is Area Director in rai>]


Clone this wiki locally