mirror of
https://github.com/django/django.git
synced 2025-10-25 14:46:09 +00:00
[1.7.x] Fixed #23611 -- update_or_create failing from a related manager
Added update_or_create to RelatedManager, ManyRelatedManager and
GenericRelatedObjectManager.
Added missing get_or_create to GenericRelatedObjectManager.
Conflicts:
tests/generic_relations/tests.py
tests/get_or_create/tests.py
Backport of ed37f7e979 from master
This commit is contained in:
committed by
Loic Bistuer
parent
dbd52f339c
commit
fa4b6482df
@@ -8,7 +8,8 @@ from django.db import IntegrityError, DatabaseError
|
||||
from django.utils.encoding import DjangoUnicodeDecodeError
|
||||
from django.test import TestCase, TransactionTestCase
|
||||
|
||||
from .models import DefaultPerson, Person, ManualPrimaryKeyTest, Profile, Tag, Thing
|
||||
from .models import (DefaultPerson, Person, ManualPrimaryKeyTest, Profile,
|
||||
Tag, Thing, Publisher, Author, Book)
|
||||
|
||||
|
||||
class GetOrCreateTests(TestCase):
|
||||
@@ -199,3 +200,54 @@ class UpdateOrCreateTests(TestCase):
|
||||
except IntegrityError:
|
||||
formatted_traceback = traceback.format_exc()
|
||||
self.assertIn('obj.save', formatted_traceback)
|
||||
|
||||
def test_create_with_related_manager(self):
|
||||
"""
|
||||
Should be able to use update_or_create from the related manager to
|
||||
create a book. Refs #23611.
|
||||
"""
|
||||
p = Publisher.objects.create(name="Acme Publishing")
|
||||
book, created = p.books.update_or_create(name="The Book of Ed & Fred")
|
||||
self.assertTrue(created)
|
||||
self.assertEqual(p.books.count(), 1)
|
||||
|
||||
def test_update_with_related_manager(self):
|
||||
"""
|
||||
Should be able to use update_or_create from the related manager to
|
||||
update a book. Refs #23611.
|
||||
"""
|
||||
p = Publisher.objects.create(name="Acme Publishing")
|
||||
book = Book.objects.create(name="The Book of Ed & Fred", publisher=p)
|
||||
self.assertEqual(p.books.count(), 1)
|
||||
name = "The Book of Django"
|
||||
book, created = p.books.update_or_create(defaults={'name': name}, id=book.id)
|
||||
self.assertFalse(created)
|
||||
self.assertEqual(book.name, name)
|
||||
self.assertEqual(p.books.count(), 1)
|
||||
|
||||
def test_create_with_many(self):
|
||||
"""
|
||||
Should be able to use update_or_create from the m2m related manager to
|
||||
create a book. Refs #23611.
|
||||
"""
|
||||
p = Publisher.objects.create(name="Acme Publishing")
|
||||
author = Author.objects.create(name="Ted")
|
||||
book, created = author.books.update_or_create(name="The Book of Ed & Fred", publisher=p)
|
||||
self.assertTrue(created)
|
||||
self.assertEqual(author.books.count(), 1)
|
||||
|
||||
def test_update_with_many(self):
|
||||
"""
|
||||
Should be able to use update_or_create from the m2m related manager to
|
||||
update a book. Refs #23611.
|
||||
"""
|
||||
p = Publisher.objects.create(name="Acme Publishing")
|
||||
author = Author.objects.create(name="Ted")
|
||||
book = Book.objects.create(name="The Book of Ed & Fred", publisher=p)
|
||||
book.authors.add(author)
|
||||
self.assertEqual(author.books.count(), 1)
|
||||
name = "The Book of Django"
|
||||
book, created = author.books.update_or_create(defaults={'name': name}, id=book.id)
|
||||
self.assertFalse(created)
|
||||
self.assertEqual(book.name, name)
|
||||
self.assertEqual(author.books.count(), 1)
|
||||
|
||||
Reference in New Issue
Block a user