Re: Making a copy of an object

2007-05-16 Thread Forest Bond
On Wed, May 16, 2007 at 08:38:01PM +0300, Matti Haavikko wrote:
> I have the following scenario:
> - I have retrieved an object from the database
> - I want to create another object based on this one, with minor modifications.

This seems like a pretty common use case, doesn't it?

Maybe something like my_other_instance = my_instance.copy() would be a good
addition to the django core?  It would be simple enough to implement.

-Forest


signature.asc
Description: Digital signature


Re: Making a copy of an object

2007-05-16 Thread Malcolm Tredinnick

On Wed, 2007-05-16 at 20:38 +0300, Matti Haavikko wrote:
> Hi,
> 
> I have the following scenario:
> - I have retrieved an object from the database
> - I want to create another object based on this one, with minor modifications.
> 
> What I'm doing is:
> - Get the existing object
> - Set its "id" to None
> - Modify any other fields and relations I need to
> - Save the object
> 
> My question is, is it advisable to copy objects this way? Would there
> be problems with e.g. foreign keys and generic relations?

Experiment and see what happens is the usual answer for simple cases
like this.

Off the top of my head, one thing is that many-to-many fields won't
work, since there won't be entries in the intermediate table
corresponding to the new id you will get. So you'll have to save your
model and then update the many-to-many entries as well.

Malcolm



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Making a copy of an object

2007-05-16 Thread Matti Haavikko

Hi,

I have the following scenario:
- I have retrieved an object from the database
- I want to create another object based on this one, with minor modifications.

What I'm doing is:
- Get the existing object
- Set its "id" to None
- Modify any other fields and relations I need to
- Save the object

My question is, is it advisable to copy objects this way? Would there
be problems with e.g. foreign keys and generic relations?

Thanks,

- Matti Haavikko

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---