Problem 'duplicating' object instance

15 views
Skip to first unread message

luciusoso

unread,
Aug 21, 2014, 8:28:56 PM8/21/14
to django...@googlegroups.com
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='te...@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?

Mario Gudelj

unread,
Aug 21, 2014, 9:19:29 PM8/21/14
to django...@googlegroups.com

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

--
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...@googlegroups.com.
To post to this group, send email to django...@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.
Reply all
Reply to author
Forward
0 new messages