Django formset set current user

前端 未结 5 1397
耶瑟儿~
耶瑟儿~ 2020-12-28 08:18

Related to this question, but expanding on it - How would I use this technique in a formset?

I\'d like to use the current logged in user in a form, but I\'m using th

5条回答
  •  一向
    一向 (楼主)
    2020-12-28 09:07

    Based on Paulo Cheque answer (which didn't really work for my case).

    I loved the idea of not writing a custom BaseFormSet inherited class.

    if formset.is_valid():
        new_instances = formset.save(commit=False)
        for new_instance in new_instances:
            new_instance.user = request.user
            new_instance.save()
    

提交回复
热议问题