Django

Code

Changeset 4448

Show
Ignore:
Timestamp:
01/29/07 10:09:25 (2 years ago)
Author:
adrian
Message:

Fixed #3389 -- Many-to-many sets can now be assigned with primary key values

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/models/fields/related.py

    r4442 r4448  
    317317            # source_col_name: the PK colname in join_table for the source object 
    318318            # target_col_name: the PK colname in join_table for the target object 
    319             # *objs - objects to add 
     319            # *objs - objects to add. Either object instances, or primary keys of object instances. 
    320320            from django.db import connection 
    321321 
     
    323323            if objs: 
    324324                # Check that all the objects are of the right type 
     325                new_ids = set() 
    325326                for obj in objs: 
    326                     if not isinstance(obj, self.model): 
    327                         raise ValueError, "objects to add() must be %s instances" % self.model._meta.object_name 
     327                    if isinstance(obj, self.model): 
     328                        new_ids.add(obj._get_pk_val()) 
     329                    else: 
     330                        new_ids.add(obj) 
    328331                # Add the newly created or already existing objects to the join table. 
    329332                # First find out which items are already added, to avoid adding them twice 
    330                 new_ids = set([obj._get_pk_val() for obj in objs]) 
    331333                cursor = connection.cursor() 
    332334                cursor.execute("SELECT %s FROM %s WHERE %s = %%s AND %s IN (%s)" % \ 
     
    355357            if objs: 
    356358                # Check that all the objects are of the right type 
     359                old_ids = set() 
    357360                for obj in objs: 
    358                     if not isinstance(obj, self.model): 
    359                         raise ValueError, "objects to remove() must be %s instances" % self.model._meta.object_name 
     361                    if isinstance(obj, self.model): 
     362                        old_ids.add(obj._get_pk_val()) 
     363                    else: 
     364                        old_ids.add(obj) 
    360365                # Remove the specified objects from the join table 
    361                 old_ids = set([obj._get_pk_val() for obj in objs]) 
    362366                cursor = connection.cursor() 
    363367                cursor.execute("DELETE FROM %s WHERE %s = %%s AND %s IN (%s)" % \ 
  • django/trunk/tests/modeltests/many_to_many/models.py

    r4232 r4448  
    204204[<Article: Oxygen-free diet works wonders>] 
    205205 
    206 # Recreate the article and Publication we just deleted. 
     206# Relation sets can also be set using primary key values 
     207>>> p2.article_set = [a4.id, a5.id] 
     208>>> p2.article_set.all() 
     209[<Article: NASA finds intelligent life on Earth>, <Article: Oxygen-free diet works wonders>] 
     210>>> a4.publications.all() 
     211[<Publication: Science News>] 
     212>>> a4.publications = [p3.id] 
     213>>> p2.article_set.all() 
     214[<Article: Oxygen-free diet works wonders>] 
     215>>> a4.publications.all() 
     216[<Publication: Science Weekly>] 
     217 
     218# Recreate the article and Publication we have deleted. 
    207219>>> p1 = Publication(id=None, title='The Python Journal') 
    208220>>> p1.save()