On Jan 5, 1:11 pm, Matthias Kestenholz <matthias.kestenh...@gmail.com>
wrote:
> 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

Thanks, Matthias, that does work.

Noting Daniel's comment, here's what the original code looked like:

languages = Language.objects.all()

for language in languages:
    """add required fields"""
    nsForm.cleaned_data['news'] = news_obj
    nsForm.cleaned_data['language'] = language
    nsForm.cleaned_data['news_date'] = datetime.now()
    if language == user.language:
        nsForm.cleaned_data['translated'] = True
    else:
        nsForm.cleaned_data['translated'] = False
    nsForm.save()

Following Matthias' example and taking some redundant code out of the
loop, here's the code that worked:

"""add required fields"""
nsForm.cleaned_data['news'] = news_obj
nsForm.cleaned_data['news_date'] = datetime.now()
languages = Language.objects.all()
obj = nsForm.save(commit=False)

for language in languages:
    obj.id = None
    obj.language = language
    if language == user.race.language:
        obj.translated = True
    else:
        obj.translated = False
    obj.save()

Thanks again, everyone.

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


Reply via email to