Re: Problem with __setattr__ and __getattribute__

2007-08-06 Thread Matti Haavikko
Hi, I asked the same question earlier. When ID is set to None and object is saved, the M2M fields are not copied. They must be copied separately. Caveats: the code below uses django internals and is very lightly tested. def copy_m2m_fields(src, dst): for m2m_field in

Re: Problem with __setattr__ and __getattribute__

2007-08-01 Thread Jacob Kaplan-Moss
Hi Dave -- I'm not sure what's going on in your code example, but there's a *much* easier way of copying an object:: >>> o = MyModel.objects.get(pk=1) >>> o.id = None >>> o.save() By setting the ID (or whatever your primary key is called) to ``None`` and calling save() you'll force

Problem with __setattr__ and __getattribute__

2007-08-01 Thread ilDave
Hello. I have a view that should duplicate a model object and save it in the database. I tried with this code: obj = MyModel.objects.get(pk=1) obj2 = MyModel() for e in obj.__dict__: if e != 'id': obj2.__setattr__(e, obj.__getattribute__(e)) #error obj2.save() But I get this