Ticket #2316: 0001-Fixed-2316-Added-get_or_create-to-GenericRelatedObje.diff

File 0001-Fixed-2316-Added-get_or_create-to-GenericRelatedObje.diff, 2.7 KB (added by Henrique Bastos, 14 years ago)

Updated patch with tests

  • django/contrib/contenttypes/generic.py

    From 86a80a1db9640b9f4aa6ea6643426338fd50951d Mon Sep 17 00:00:00 2001
    From: Henrique Bastos <henrique@bastos.net>
    Date: Wed, 24 Nov 2010 08:36:24 -0200
    Subject: [PATCH] Fixed #2316 -- Added get_or_create to GenericRelatedObjectManager
    
    ---
     django/contrib/contenttypes/generic.py      |    7 +++++++
     tests/modeltests/generic_relations/tests.py |   16 ++++++++++++++++
     2 files changed, 23 insertions(+), 0 deletions(-)
    
    diff --git a/django/contrib/contenttypes/generic.py b/django/contrib/contenttypes/generic.py
    index fd05a30..793b495 100644
    a b def create_generic_related_manager(superclass):  
    295295            return super(GenericRelatedObjectManager, self).using(db).create(**kwargs)
    296296        create.alters_data = True
    297297
     298        def get_or_create(self, **kwargs):
     299            kwargs[self.content_type_field_name] = self.content_type
     300            kwargs[self.object_id_field_name] = self.pk_val
     301            db = router.db_for_write(self.model, instance=self.instance)
     302            return super(GenericRelatedObjectManager, self).using(db).get_or_create(**kwargs)
     303        get_or_create.alters_data = True
     304
    298305    return GenericRelatedObjectManager
    299306
    300307class GenericRel(ManyToManyRel):
  • tests/modeltests/generic_relations/tests.py

    diff --git a/tests/modeltests/generic_relations/tests.py b/tests/modeltests/generic_relations/tests.py
    index 3d25301..f30b5dc 100644
    a b class GenericRelationsTests(TestCase):  
    221221        formset = GenericFormSet(instance=lion, prefix='x')
    222222        self.assertEqual(u''.join(form.as_p() for form in formset.forms), u"""<p><label for="id_x-0-tag">Tag:</label> <input id="id_x-0-tag" type="text" name="x-0-tag" maxlength="50" /></p>
    223223<p><label for="id_x-0-DELETE">Delete:</label> <input type="checkbox" name="x-0-DELETE" id="id_x-0-DELETE" /><input type="hidden" name="x-0-id" id="id_x-0-id" /></p>""")
     224
     225    def test_generic_relations_get_or_create(self):
     226        # Setup our initial data
     227        bacon = Vegetable.objects.create(name="Bacon", is_yucky=False)
     228        fatty = TaggedItem.objects.create(content_object=bacon, tag="fatty")
     229
     230        # tag1 must be retrieved from the database
     231        tag1, created = bacon.tags.get_or_create(tag='fatty')
     232        self.assertFalse(created)
     233        self.assertEquals(tag1, fatty)
     234        self.assertEquals(tag1.content_object, bacon)
     235
     236        # tag2 should not exist and must be created
     237        tag2, created = bacon.tags.get_or_create(tag='salty')
     238        self.assertTrue(created)
     239        self.assertEquals(tag2.content_object, bacon)
Back to Top