Code


Version 12 (modified by fumanchu@…, 8 years ago) (diff)

Code you can steal

Fields as Descriptors

In this proposal, fields would become descriptors. This means

  • They would be left as members by the metaclass.
  • The real 'data' would end up either in name-mangled instance members, or in a private dictionary.
  • When accessed via the class, they would return themselves. eg Reporter.name would be a field class you can inspect. This would make introspection as in the admin easier.
  • When accessed via the instance, they would return a representation of the value they hold. This could be
    • the simple data instance from the private dict that was fetched from the db.
    • a lazily computed value, eg a BLOB field which would be fetched from the db on first access, not at initial load time, or a ForeignKey relationship that hasn't been loaded yet. For a ForeignKey,
  article.get_reporter()
  article.reporter_id

would change to:

  article.reporter
  article.reporter.id
  • ForeignKeys and other related fields would place another descriptor on the related class. When accessed via the instance, this would return a lazy collection object. It will act like a set (an ordered one if there is an ordering involved).

This would also support other methods in favour of the get_related_list things.

Other end of ForeignKey relationship:

 reporter.get_article_list()
 reporter.get_article_list(headline__startswith='This', order_by=['headline'])
 reporter.add_article(headline="John's second story", pub_date=datetime(2005, 7, 29))

would change to:

 reporter.article_set
 reporter.article_set.filter(headline__startswith='This').order_by('headline')
 reporter.article_set.add(headline="John's second story", pub_date=datetime(2005, 7, 29))

Methods which change the meaning of the lazy collection are as follows:

  • .filter : adds query parameters using djangos normal db query syntax.
  • .order_by: adds/changes ordering parameters.

The collection supports the appropriate collection protocols : It acts as a set, with ordering if there is an ordering in the underlying query. The query is only done when a collection protocol is used. Sometimes, multiple queries will be used to save traffic, eg if you access reporter.article_set[10000:10020] and then reporter.article_set[:20].

Other methods of a similar style are also possible.

  • .add would add a new instance of the related class to the collection. This would register a callback so that it would be written to the db when .save() is called on the parent object.

ManyToMany examples: (from http://www.djangoproject.com/documentation/models/many_to_many/ )

    a1.set_publications([p1.id]) 

goes to

       a1.publication_set.clear()
       a1.publication_set.add(p1) 

This looks a bit odd because of the clear call . In reality, most of the time you don't want to replace a whole set of many-to-manys, so the new api is more convenient than the old one. The methods are precisely those of a Python 2.4 built in set.


       a2.set_publications([p1.id])
       a2.set_publications([p1.id, p2.id])

goes to

       a2.publication_set.clear()
       a2.publication_set.add(p1)
       a2.publication_set.clear()
       a2.publication_set.update([p1,p2])

or idiomatically

      a2.publication_set.clear()
      a2.publication_set.add(p1)
      a2.publication_set.add(p2)

  p1.get_article_list(order_by=['headline'])

goes to

  p1.article_set.order_by('headline')

When you finally open a ticket on this massive change ;) I'll add a comment that you should feel free to steal code from Dejavu, which has used descriptors successfully for two years now: http://projects.amor.org/dejavu/browser/trunk/units.py -- Robert Brewer