Changes between Version 30 and Version 31 of DescriptorFields


Ignore:
Timestamp:
02/01/2006 12:29:42 PM (10 years ago)
Author:
adrian
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DescriptorFields

    v30 v31  
    1 = Fields as descriptors =
    2 
    3 This proposal changes the Django database API substantially.
    4 
    5 The examples on this page use the following models:
    6 
    7 {{{
    8 #!python
    9 class Reporter(models.Model):
    10     fname = models.CharField(maxlength=30)
    11     lname = models.CharField(maxlength=30)
    12 
    13 class Site(models.Model):
    14     name = models.CharField(maxlength=20)
    15 
    16 class Article(models.Model):
    17     headline = models.CharField(maxlength=50)
    18     reporter = models.ForeignKey(Reporter)
    19     pub_date = models.DateField()
    20     sites = models.ManyToManyField(Site)
    21 }}}
    22 
    23 == Overview ==
    24 
    25 This proposal changes the way foreign-key and many-to-many related objects are accessed via the database-object API. It also changes the functionality of {{{get_list()}}}, {{{get_object()}}}, etc.
    26 
    27 Under this proposal, each manager gets the following methods:
    28 
    29 {{{
    30 #!python
    31 class Manager:
    32     def all(self):
    33         # returns a Query instance -- old get_list()
    34 
    35     def filter(self, **kwargs):
    36         # returns a Query instance -- old get_list(**kwargs)
    37 
    38     def get(self, **kwargs):
    39         # returns a Query instance -- old get_object()
    40 
    41     def values(self, **kwargs):
    42         # returns a ValuesQuery instance -- old get_values()
    43 
    44     def in_bulk(self, **kwargs):
    45         # return an InBulkQuery instance -- old get_in_bulk()
    46 
    47 class Query:
    48     def filter(self, **kwargs):
    49         # returns a Query instance
    50 
    51     def unique(self, true_or_false):
    52         # returns a Query instance
    53 
    54     def order_by(self, *field_names):
    55         # returns a Query instance
    56 
    57     def count(self):
    58         # returns a Query instance
    59 
    60 class ValuesQuery(Query):
    61     # Only difference is the output format.
    62 
    63 class InBulkQuery(Query):
    64     # Only difference is the output format.
    65 
    66 }}}
    67 
    68 Any of these Manager methods can be overridden by custom managers, of course.
    69 
    70 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.
    71 
    72 Another improvement is that the lookup type "{{{__exact}}}" can be left off.
    73 
    74 == Examples ==
    75 
    76 || '''Old syntax'''                                        || '''New syntax'''                             ||
    77 || {{{reporters.get_list()}}}                             || {{{Reporter.objects.all()}}}                       ||
    78 || {{{reporters.get_list(fname__exact='John')}}}          || {{{Reporter.objects.filter(fname='John')}}}        ||
    79 || {{{reporters.get_list(order_by=('-lname', 'fname'))}}} || {{{Reporter.objects.order_by('-lname', 'fname')}}} ||
    80 || {{{reporters.get_list(fname__exact='John', order_by=('lname',))}}} || {{{Reporter.objects.filter(fname='John').order_by('lname')}}} ||
    81 || {{{reporters.get_object(pk=3)}}}                       || {{{Reporter.objects.get(pk=3)}}}                   ||
    82 || {{{reporters.get_object(fname__contains='John')}}}     || {{{Reporter.objects.get(fname__contains='John')}}} ||
    83 || {{{reporters.get_list(distinct=True)}}}                || {{{Reporter.objects.distinct()}}} ||
    84 || {{{reporters.get_values()}}}                           || {{{Reporter.objects.values()}}} ||
    85 || {{{reporters.get_in_bulk([1, 2])}}}                    || {{{Reporter.objects.in_bulk([1, 2])}}} ||
    86 || {{{reporters.get_in_bulk([1, 2], fname__exact='John')}}} || {{{Reporter.objects.filter(fname='John').in_bulk([1, 2])}}} ||
    87 || '''Date lookup'''                                      ||                                              ||
    88 || {{{articles.get_pub_date_list('year')}}}                     || {{{Article.objects.dates('pub_date', 'year')}}} ||
    89 || '''Many-to-one related lookup'''                        ||                                              ||
    90 || {{{article_obj.reporter_id}}}                                 || {{{article_obj.reporter.id}}} ||
    91 || {{{article_obj.get_reporter()}}}                              || {{{article_obj.reporter}}}    ||
    92 || {{{reporter_obj.get_article_list()}}}                         || {{{reporter_obj.article_set.all()}}} ||
    93 || {{{reporter_obj.get_article_list(headline__exact='Hello')}}}  || {{{reporter_obj.article_set.filter(headline='Hello')}}} ||
    94 || {{{reporter_obj.get_article_count()}}}                        || {{{reporter_obj.article_set.count()}}} ||
    95 || {{{reporter_obj.add_article(headline='Foo')}}}                || {{{reporter_obj.article_set.add(headline='Foo')}}} ||
    96 || (Alternate syntax)                                      || {{{reporter_obj.article_set.add(article_obj)}}} ||
    97 || ("values" lookup, etc., not previously possible)        || {{{reporter_obj.article_set.values()}}} ||
    98 || '''Many-to-many related lookup'''                       ||                         ||
    99 || {{{article_obj.get_site_list()}}}                             || {{{article_obj.sites.all()}}} ||
    100 || {{{article_obj.set_sites([s1.id, s2.id])}}}                   || {{{article_obj.sites.clear(); article_obj.sites.add(s1); article_obj.sites.add(s2)}}} ||
    101 || {{{article_obj.set_sites([s1.id]) # deletion}}}               || {{{article_obj.sites.remove(s2)}}} ||
    102 || {{{site_obj.get_reporter_list()}}}                            || {{{site_obj.reporter_set.all()}}} ||
    103 
     1This page's contents have been moved to RemovingTheMagic .
Back to Top