Tuesday, January 5, 2010

Re: Saving several copies of an object

2010/1/5 Tomasz Zieliński <tomasz.zielinski@pyconsultant.eu>:
>
>
> On 5 Sty, 18:16, pjmorse <flashesofpa...@gmail.com> wrote:
>>
>> This is done by looping over the list of languages and saving a
>> NewsTrans in each language. The source language is marked as already
>> translated, the other two are not (that is, they still need
>> translating).
>>
>> The problem is that this is done with ns.save() (where ns is a
>> NewsTransForm with the submitted data), and what's actually happening
>> is that one NewsTrans is created, then it is updated twice, ending in
>> the third language.
>>
>> I thought I could fix this with ns.save(force_insert=True) but that
>> throws errors instead: "save() got an unexpected keyword argument
>> 'force_insert'".
>>
>
> Take a look at this:
>
> http://docs.djangoproject.com/en/1.0/ref/models/instances/#how-django-knows-to-update-vs-insert
>
> - after first ns.save() 'ns' receives primary key (id), which in turn
> causes any subsequent
> ns.save() to UPDATE previously created row. I'm not sure why
> 'force_insert' is an unexpected
> argument, but it doesn't make sense anyway to use it.
> You can try ns.id = None; ns.save() instead.
>

This won't do it, because ns is a Form, not a Model object. Something
like this might work though:

obj = ns.save(commit=False)

for language in languages:
obj.id = None
obj.language = language
obj.save()


Matthias

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate