def create(request): participants = Participant.objects.all() if request.method == 'POST': query = request.POST.copy() form = EditForm(query) if form.is_valid(): try: with transaction.atomic(): d = form.cleaned_data participant_group = ParticipantGroup.objects.create( group_name=d['group_name'], info=d['info']) for participant_id in d['participant']: participant_group.participants.add( Participant.objects.get(pk=participant_id)) participant_group.save() except Exception: logger.exception(None) raise return render(request, 'participantgroup/complete.html', {'message': u'端末グループを更新しました。'}) else: form = EditForm() return render( request, 'participantgroup/edit.html', {'edit_type': 'create', 'form': form, 'participants': participants})
def update(request, participant_group_id=None): try: participant_group = \ ParticipantGroup.objects.get(pk=participant_group_id) except ParticipantGroup.DoesNotExist: raise Http404(u'端末グループが存在しません。') participants = Participant.objects.all() if request.method == 'POST': query = request.POST.copy() form = EditForm(query) if form.is_valid(): try: with transaction.atomic(): d = form.cleaned_data participant_group.group_name=d['group_name'] participant_group.info=d['info'] for participant_id in d['participant']: participant_group.participants.add( Participant.objects.get(pk=participant_id)) participant_group.save() except Exception: logger.exception(None) raise return render(request, 'participantgroup/complete.html', {'message': u'端末グループを更新しました。'}) else: form = EditForm(initial={ 'group_name': participant_group.group_name, 'info': participant_group.info, 'participant': [x['id'] for x in participant_group.participants.values('id')] }) return render( request, 'participantgroup/edit.html', {'edit_type': 'update', 'form': form, 'participant_group': participant_group, 'participants': participants})