Thursday, August 21, 2014

Re: Problem 'duplicating' object instance

I think you can use deepcopy.copy set the id none then save

On 22/08/2014 10:58 am, "luciusoso" <kruchanem@gmail.com> wrote:
Hi everyone, as the title says i have a problem when trying to copy a model instance.

Basically when i try in my tests to duplicate an object by setting its pk and id to None, i get nothing in return.

class MyTest(TestCase):
    def setUp(self):
        u1 = User(username='user1', email='test1@test.com')
        u1.save()
        a1 = Account(full_name='Test1', user=u1)
        a1.save()

    def test_copy(self):
        account1 = Account.objects.all()[0]
        account1.pk = None
        account1.id = None
        account1.save()
        print Account.objects.all()

>>>... <Account: user1>

Am i wrong in thinking i should be getting 2 objects after setting the pk to none?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c7b353bc-f4aa-4c4d-920d-408650c07d02%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHqTbjnn-piRAzbcgODQ7ShZe%2BP1yrKhP4qEREe2DmbQa5ZXyA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment