Django

Code

Changeset 2510

Show
Ignore:
Timestamp:
03/11/06 21:58:11 (2 years ago)
Author:
russellm
Message:

magic-removal: Added descriptor code for assignment of related object sets (Reporter.article_set = [a,b,c]).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/magic-removal/django/db/models/fields/related.py

    r2502 r2510  
    9595         
    9696    def __set__(self, instance, value): 
     97        if instance is None: 
     98            raise AttributeError, "%s must be accessed via instance" % self.related.opts.object_name 
    9799        # Set the value of the related field 
    98100        setattr(value, self.related.field.attname, instance) 
     
    127129 
    128130    def __set__(self, instance, value): 
     131        if instance is None: 
     132            raise AttributeError, "%s must be accessed via instance" % self._field.name 
    129133        # Set the value of the related field 
    130134        try: 
     
    198202        return manager 
    199203 
     204    def __set__(self, instance, value): 
     205        if instance is None: 
     206            raise AttributeError, "Manager must be accessed via instance" 
     207 
     208        manager = self.__get__(instance)         
     209        # If the foreign key can support nulls, then completely clear the related set. 
     210        # Otherwise, just move the named objects into the set.  
     211        if self.related.field.null: 
     212            manager.clear()             
     213        for obj in value: 
     214            manager.add(obj) 
     215 
    200216def _add_m2m_items(rel_manager_inst, managerclass, rel_model, join_table, source_col_name, 
    201217        target_col_name, source_pk_val, *objs): 
     
    330346        return manager 
    331347 
     348    def __set__(self, instance, value): 
     349        if instance is None: 
     350            raise AttributeError, "Manager must be accessed via instance" 
     351         
     352        manager = self.__get__(instance) 
     353        manager.clear() 
     354        for obj in value: 
     355            manager.add(obj) 
     356 
    332357class ReverseManyRelatedObjectsDescriptor(object): 
    333358    # This class provides the functionality that makes the related-object 
     
    401426 
    402427        return manager 
     428 
     429    def __set__(self, instance, value): 
     430        if instance is None: 
     431            raise AttributeError, "Manager must be accessed via instance" 
     432             
     433        manager = self.__get__(instance) 
     434        manager.clear() 
     435        for obj in value: 
     436            manager.add(obj) 
    403437 
    404438class ForeignKey(RelatedField, Field): 
  • django/branches/magic-removal/tests/modeltests/many_to_many/models.py

    r2409 r2510  
    145145[] 
    146146 
    147 # You can clear the whole lot: 
    148 # (put some back first) 
    149 >>> p2.article_set.add(a4, a5) 
    150 >>> a4.publications.add(p3) 
    151 >>> a4.publications.all() 
    152 [Science News, Science Weekly] 
     147# Relation sets can be assigned. Assignment clears any existing set members 
     148>>> p2.article_set = [a4, a5] 
     149>>> p2.article_set.all() 
     150[NASA finds intelligent life on Earth, Oxygen-free diet works wonders] 
     151>>> a4.publications.all() 
     152[Science News] 
     153>>> a4.publications = [p3] 
     154>>> p2.article_set.all() 
     155[Oxygen-free diet works wonders] 
     156>>> a4.publications.all() 
     157[Science Weekly] 
     158 
     159# Relation sets can be cleared: 
    153160>>> p2.article_set.clear() 
    154161>>> p2.article_set.all() 
     
    197204[NASA uses Python] 
    198205 
    199  
    200206""" 
  • django/branches/magic-removal/tests/modeltests/many_to_one/models.py

    r2435 r2510  
    8484[] 
    8585 
    86 # Set the article back again. 
    87 >>> new_article2.reporter = r2 
    88 >>> new_article2.save() 
     86# Set the article back again using set descriptor. 
     87>>> r2.article_set = [new_article, new_article2] 
     88>>> r.article_set.all() 
     89[This is a test] 
     90>>> r2.article_set.all() 
     91[John's second story, Paul's story] 
     92 
     93# Funny case - assignment notation can only go so far; because the  
     94# ForeignKey cannot be null, existing members of the set must remain 
     95>>> r.article_set = [new_article] 
     96>>> r.article_set.all() 
     97[This is a test, John's second story] 
     98>>> r2.article_set.all() 
     99[Paul's story] 
    89100 
    90101# Reporter cannot be null - there should not be a clear or remove method 
  • django/branches/magic-removal/tests/modeltests/many_to_one_null/models.py

    r2434 r2510  
    5050 
    5151# Reporter objects have access to their related Article objects. 
    52 >>> r.article_set.order_by('headline'
     52>>> r.article_set.all(
    5353[First, Second] 
    5454>>> r.article_set.filter(headline__startswith='Fir') 
     
    112112[Fourth] 
    113113 
     114# Use descriptor assignment to allocate ForeignKey. Null is legal, so 
     115# existing members of set that are not in the assignment set are set null 
     116>>> r2.article_set = [a2, a3] 
     117>>> r2.article_set.all() 
     118[Second, Third] 
     119 
    114120# Clear the rest of the set 
    115121>>> r.article_set.clear() 
     
    117123[] 
    118124>>> Article.objects.filter(reporter__isnull=True) 
    119 [First, Second, Third
     125[First, Fourth
    120126 
    121127"""