Django

Code

Changeset 2435

Show
Ignore:
Timestamp:
02/28/06 05:36:20 (3 years ago)
Author:
russellm
Message:

magic-removal: Fixed #1407 -- Added a set method for the single object descriptor. This enables setting related objects using poll.choice = c, and ensures that the cache is kept accurate in the process.

Files:

Legend:

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

    r2434 r2435  
    105105            return rel_obj 
    106106 
     107    def __set__(self, instance, value): 
     108        # Set the value of the related field 
     109        if value: 
     110            val = getattr(value, self._field.rel.get_related_field().attname) 
     111        else: 
     112            val = None 
     113        setattr(instance, self._field.attname, val) 
     114             
     115        # Set the cache to point to the new object 
     116        setattr(instance, self._field.get_cache_name(), value) 
     117         
    107118class ForeignRelatedObjectsDescriptor(object): 
    108119    # This class provides the functionality that makes the related-object 
     
    131142            def add(self, *objs): 
    132143                for obj in objs: 
    133                     val = getattr(instance, rel_field.rel.get_related_field().attname) 
    134                     setattr(obj, rel_field.attname, val) 
     144                    setattr(obj, rel_field.name, instance) 
    135145                    obj.save() 
    136146            add.alters_data = True 
     
    147157                    val = getattr(instance, rel_field.rel.get_related_field().attname) 
    148158                    for obj in objs: 
     159                        # Is obj actually part of this descriptor set?  
    149160                        if getattr(obj, rel_field.attname) == val: 
    150                             setattr(obj, rel_field.attname, None) 
     161                            setattr(obj, rel_field.name, None) 
    151162                            obj.save() 
    152163                        else: 
     
    156167                def clear(self): 
    157168                    for obj in self.all(): 
    158                         setattr(obj, rel_field.attname, None) 
     169                        setattr(obj, rel_field.name, None) 
    159170                        obj.save() 
    160171                add.alters_data = True 
  • django/branches/magic-removal/tests/modeltests/many_to_one/models.py

    r2434 r2435  
    7171>>> r2.article_set.all() 
    7272[Paul's story] 
     73 
     74# Assign the article to the reporter directly using the descriptor 
     75>>> new_article2.reporter = r 
     76>>> new_article2.save() 
     77>>> new_article2.reporter 
     78John Smith 
     79>>> new_article2.reporter.id 
     801 
     81>>> r.article_set.all() 
     82[This is a test, John's second story, Paul's story] 
     83>>> r2.article_set.all() 
     84[] 
     85 
     86# Set the article back again. 
     87>>> new_article2.reporter = r2 
     88>>> new_article2.save() 
    7389 
    7490# Reporter cannot be null - there should not be a clear or remove method