mirror of
https://github.com/django/django.git
synced 2025-08-03 10:34:04 +00:00
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.
This commit is contained in:
parent
c1ef234e31
commit
ed37f7e979
5 changed files with 134 additions and 5 deletions
|
@ -9,7 +9,7 @@ from django.utils.encoding import DjangoUnicodeDecodeError
|
|||
from django.test import TestCase, TransactionTestCase
|
||||
|
||||
from .models import (DefaultPerson, Person, ManualPrimaryKeyTest, Profile,
|
||||
Tag, Thing, Publisher, Author)
|
||||
Tag, Thing, Publisher, Author, Book)
|
||||
|
||||
|
||||
class GetOrCreateTests(TestCase):
|
||||
|
@ -239,6 +239,57 @@ class UpdateOrCreateTests(TestCase):
|
|||
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)
|
||||
|
||||
def test_related(self):
|
||||
p = Publisher.objects.create(name="Acme Publishing")
|
||||
# Create a book through the publisher.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue