Make copies of object: discussion for ticket #4027

162 views
Skip to first unread message

Marek Kubica

unread,
Apr 14, 2007, 8:36:22 AM4/14/07
to django-d...@googlegroups.com
Hi all,

some days ago I had a problem: I needed to copy some objects from the
database preserving their values (and changing only one field). In #django
someone told me that this can be archived easily by setting the primary
key to None and saving. It worked well. But some days later I realized
that ManyToManyFields were not copied, whereas the others did.
This was caused be how ManyToManyFields work, so I had first to save the
objects linked in the ManyToManyField, then to set the private key to
None, save, and add the linked objects again and save them for a second
time.
So, setting the primary key to None is not a very clean solution to copy
fields. What I'd need is an official way to copy objects.
Is there any reason there is no such .copy() method yet? To me it seems
quite reasonable to copy objects as I have to maintain a lots of objects
which differ only in one field and recreating the object from scratch
feels like some kind of strange limitation. Maybe I'm wrong in some way,
but systems have supported copy since a long time...

I would be happy to be able to do something like that:

>>> first = Object.objects.get(name='First')
>>> second = first.copy()
>>>
>>> second.name = 'Second'
>>> # or, that would be also quite clean
>>> second = first.copy(name='Second')
>>>
>>> second.save()

I'd be happy to discuss the pros and cons of such a copy()-function.

The ticket: http://code.djangoproject.com/ticket/4027

regards,
Marek

anders conbere

unread,
Apr 14, 2007, 12:42:26 PM4/14/07
to django-d...@googlegroups.com
On 4/14/07, Marek Kubica <python...@web.de> wrote:
>
> Hi all,
>
> some days ago I had a problem: I needed to copy some objects from the
> database preserving their values (and changing only one field). In #django
> someone told me that this can be archived easily by setting the primary
> key to None and saving. It worked well. But some days later I realized
> that ManyToManyFields were not copied, whereas the others did.
> This was caused be how ManyToManyFields work, so I had first to save the
> objects linked in the ManyToManyField, then to set the private key to
> None, save, and add the linked objects again and save them for a second
> time.
> So, setting the primary key to None is not a very clean solution to copy
> fields. What I'd need is an official way to copy objects.
> Is there any reason there is no such .copy() method yet? To me it seems
> quite reasonable to copy objects as I have to maintain a lots of objects
> which differ only in one field and recreating the object from scratch
> feels like some kind of strange limitation. Maybe I'm wrong in some way,
> but systems have supported copy since a long time...

If you have many objects that differ by only one field, have you
considered moving that one field into it's own model? That would
allow you to to make "copies" by simply setting up a m2m relation.

~ Anders

Marek Kubica

unread,
Apr 15, 2007, 5:09:04 AM4/15/07
to django-d...@googlegroups.com
Hi,

On Sat, 14 Apr 2007 09:42:26 -0700, anders conbere wrote:

> If you have many objects that differ by only one field, have you
> considered moving that one field into it's own model? That would
> allow you to to make "copies" by simply setting up a m2m relation.

Unfortunately, it's not that easy:
- I have many objects that differ by (at minimum) one field - the
name. But other fields can differ as well.
- As the admin has no copy support, I wrote a helper app, which copies
one object to a new name, and gives a link to it, so that the user can
modify the models further (means: change more fields from the original
model, without having to re-enter the whole model)

So it's a little bit more complicated. It can be worked around with helper
applications like mine, but well - it still feels like a workaround, not
like a real solution.

regards,
Marek

Reply all
Reply to author
Forward
0 new messages