Django

Code

Changeset 2434

Show
Ignore:
Timestamp:
02/28/06 04:12:45 (3 years ago)
Author:
russellm
Message:

magic-removal: Refs #1407 -- Added remove() and clear() methods for ForeignKeys? that allow nulls.

Files:

Legend:

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

    r2409 r2434  
    142142            create.alters_data = True 
    143143 
     144            # remove() and clear() are only provided if the ForeignKey can have a value of null. 
     145            if rel_field.null: 
     146                def remove(self, *objs): 
     147                    val = getattr(instance, rel_field.rel.get_related_field().attname) 
     148                    for obj in objs: 
     149                        if getattr(obj, rel_field.attname) == val: 
     150                            setattr(obj, rel_field.attname, None) 
     151                            obj.save() 
     152                        else: 
     153                            raise rel_field.rel.to.DoesNotExist, "'%s' is not related to '%s'." % (obj, instance) 
     154                add.alters_data = True 
     155                 
     156                def clear(self): 
     157                    for obj in self.all(): 
     158                        setattr(obj, rel_field.attname, None) 
     159                        obj.save() 
     160                add.alters_data = True 
     161                     
    144162        manager = RelatedManager() 
    145163        manager.core_filters = {'%s__pk' % rel_field.name: getattr(instance, rel_field.rel.get_related_field().attname)} 
  • django/branches/magic-removal/tests/modeltests/many_to_one/models.py

    r2409 r2434  
    7171>>> r2.article_set.all() 
    7272[Paul's story] 
     73 
     74# Reporter cannot be null - there should not be a clear or remove method 
     75>>> hasattr(r2.article_set, 'remove') 
     76False 
     77>>> hasattr(r2.article_set, 'clear') 
     78False 
    7379 
    7480# Reporter objects have access to their related Article objects. 
  • django/branches/magic-removal/tests/modeltests/many_to_one_null/models.py

    r2409 r2434  
    2121        return self.headline 
    2222 
     23    class Meta: 
     24        ordering = ('headline',) 
     25         
    2326API_TESTS = """ 
    2427# Create a Reporter. 
     
    8083>>> Article.objects.filter(reporter__isnull=True) 
    8184[Third] 
     85 
     86# Set the reporter for the Third article 
     87>>> r.article_set.add(a3) 
     88>>> r.article_set.all() 
     89[First, Second, Third] 
     90 
     91# Remove an article from the set, and check that it was removed. 
     92>>> r.article_set.remove(a3) 
     93>>> r.article_set.all() 
     94[First, Second] 
     95>>> Article.objects.filter(reporter__isnull=True) 
     96[Third] 
     97 
     98# Create another article and reporter 
     99>>> r2 = Reporter(name='Paul Jones') 
     100>>> r2.save() 
     101>>> a4 = r2.article_set.create(headline='Fourth') 
     102>>> r2.article_set.all() 
     103[Fourth] 
     104 
     105# Try to remove a4 from a set it does not belong to 
     106>>> r.article_set.remove(a4) 
     107Traceback (most recent call last): 
     108... 
     109DoesNotExist: 'Fourth' is not related to 'John Smith'. 
     110 
     111>>> r2.article_set.all() 
     112[Fourth] 
     113 
     114# Clear the rest of the set 
     115>>> r.article_set.clear() 
     116>>> r.article_set.all() 
     117[] 
     118>>> Article.objects.filter(reporter__isnull=True) 
     119[First, Second, Third] 
     120 
    82121"""