mirror of
https://github.com/django/django.git
synced 2025-07-04 09:49:12 +00:00
newforms-admin: Merged to [4449]
git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@4450 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
e6cd7fcacc
commit
5b47d3b42a
@ -316,18 +316,20 @@ def create_many_related_manager(superclass):
|
|||||||
# join_table: name of the m2m link table
|
# join_table: name of the m2m link table
|
||||||
# source_col_name: the PK colname in join_table for the source object
|
# source_col_name: the PK colname in join_table for the source object
|
||||||
# target_col_name: the PK colname in join_table for the target object
|
# target_col_name: the PK colname in join_table for the target object
|
||||||
# *objs - objects to add
|
# *objs - objects to add. Either object instances, or primary keys of object instances.
|
||||||
from django.db import connection
|
from django.db import connection
|
||||||
|
|
||||||
# If there aren't any objects, there is nothing to do.
|
# If there aren't any objects, there is nothing to do.
|
||||||
if objs:
|
if objs:
|
||||||
# Check that all the objects are of the right type
|
# Check that all the objects are of the right type
|
||||||
|
new_ids = set()
|
||||||
for obj in objs:
|
for obj in objs:
|
||||||
if not isinstance(obj, self.model):
|
if isinstance(obj, self.model):
|
||||||
raise ValueError, "objects to add() must be %s instances" % self.model._meta.object_name
|
new_ids.add(obj._get_pk_val())
|
||||||
|
else:
|
||||||
|
new_ids.add(obj)
|
||||||
# Add the newly created or already existing objects to the join table.
|
# Add the newly created or already existing objects to the join table.
|
||||||
# First find out which items are already added, to avoid adding them twice
|
# First find out which items are already added, to avoid adding them twice
|
||||||
new_ids = set([obj._get_pk_val() for obj in objs])
|
|
||||||
cursor = connection.cursor()
|
cursor = connection.cursor()
|
||||||
cursor.execute("SELECT %s FROM %s WHERE %s = %%s AND %s IN (%s)" % \
|
cursor.execute("SELECT %s FROM %s WHERE %s = %%s AND %s IN (%s)" % \
|
||||||
(target_col_name, self.join_table, source_col_name,
|
(target_col_name, self.join_table, source_col_name,
|
||||||
@ -354,11 +356,13 @@ def create_many_related_manager(superclass):
|
|||||||
# If there aren't any objects, there is nothing to do.
|
# If there aren't any objects, there is nothing to do.
|
||||||
if objs:
|
if objs:
|
||||||
# Check that all the objects are of the right type
|
# Check that all the objects are of the right type
|
||||||
|
old_ids = set()
|
||||||
for obj in objs:
|
for obj in objs:
|
||||||
if not isinstance(obj, self.model):
|
if isinstance(obj, self.model):
|
||||||
raise ValueError, "objects to remove() must be %s instances" % self.model._meta.object_name
|
old_ids.add(obj._get_pk_val())
|
||||||
|
else:
|
||||||
|
old_ids.add(obj)
|
||||||
# Remove the specified objects from the join table
|
# Remove the specified objects from the join table
|
||||||
old_ids = set([obj._get_pk_val() for obj in objs])
|
|
||||||
cursor = connection.cursor()
|
cursor = connection.cursor()
|
||||||
cursor.execute("DELETE FROM %s WHERE %s = %%s AND %s IN (%s)" % \
|
cursor.execute("DELETE FROM %s WHERE %s = %%s AND %s IN (%s)" % \
|
||||||
(self.join_table, source_col_name,
|
(self.join_table, source_col_name,
|
||||||
|
@ -37,7 +37,7 @@ def save_instance(form, instance, commit=True):
|
|||||||
if commit:
|
if commit:
|
||||||
instance.save()
|
instance.save()
|
||||||
for f in opts.many_to_many:
|
for f in opts.many_to_many:
|
||||||
setattr(instance, f.attname, getattr(instance, f.attname).model.objects.filter(pk__in = clean_data[f.name]))
|
setattr(instance, f.attname, clean_data[f.name])
|
||||||
# GOTCHA: If many-to-many data is given and commit=False, the many-to-many
|
# GOTCHA: If many-to-many data is given and commit=False, the many-to-many
|
||||||
# data will be lost. This happens because a many-to-many options cannot be
|
# data will be lost. This happens because a many-to-many options cannot be
|
||||||
# set on an object until after it's saved. Maybe we should raise an
|
# set on an object until after it's saved. Maybe we should raise an
|
||||||
|
@ -186,7 +186,7 @@ code.
|
|||||||
|
|
||||||
This is the philosophy behind `template inheritance`_.
|
This is the philosophy behind `template inheritance`_.
|
||||||
|
|
||||||
.. _template inheritance: ./templates/#template-inheritance
|
.. _template inheritance: ../templates/#template-inheritance
|
||||||
|
|
||||||
Be decoupled from HTML
|
Be decoupled from HTML
|
||||||
----------------------
|
----------------------
|
||||||
|
@ -203,7 +203,19 @@ __test__ = {'API_TESTS':"""
|
|||||||
>>> p2.article_set.all()
|
>>> p2.article_set.all()
|
||||||
[<Article: Oxygen-free diet works wonders>]
|
[<Article: Oxygen-free diet works wonders>]
|
||||||
|
|
||||||
# Recreate the article and Publication we just deleted.
|
# Relation sets can also be set using primary key values
|
||||||
|
>>> p2.article_set = [a4.id, a5.id]
|
||||||
|
>>> p2.article_set.all()
|
||||||
|
[<Article: NASA finds intelligent life on Earth>, <Article: Oxygen-free diet works wonders>]
|
||||||
|
>>> a4.publications.all()
|
||||||
|
[<Publication: Science News>]
|
||||||
|
>>> a4.publications = [p3.id]
|
||||||
|
>>> p2.article_set.all()
|
||||||
|
[<Article: Oxygen-free diet works wonders>]
|
||||||
|
>>> a4.publications.all()
|
||||||
|
[<Publication: Science Weekly>]
|
||||||
|
|
||||||
|
# Recreate the article and Publication we have deleted.
|
||||||
>>> p1 = Publication(id=None, title='The Python Journal')
|
>>> p1 = Publication(id=None, title='The Python Journal')
|
||||||
>>> p1.save()
|
>>> p1.save()
|
||||||
>>> a2 = Article(id=None, headline='NASA uses Python')
|
>>> a2 = Article(id=None, headline='NASA uses Python')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user