Set the value of the form field to is_valid ()

I have problems understanding how to do this. I did my best to search Google without much success.

I will start with the code and explain what I'm trying to do when I go:

models.py

class Action(models.Model): title = models.CharField(max_length=200) owner = models.ForeignKey(User, related_name='actions') created_by = models.ForeignKey(User, related_name='+', editable=False) modified_by = models.ForeignKey(User, related_name='+', editable=False) class ActionForm(ModelForm): class Meta: model = Action 

views.py

By default, the owner has a drop-down field. I have an icon that allows the user to enter a new username in the text box instead for the owner. I check if owner_new sent, and if so, create this user. Then I need to set the owner field to this so that form.is_valid() true.

 def action_create(request): if request.method == 'POST': form = ActionForm(request.POST) # check if new user should be created if 'owner_new' in request.POST: # check if user already exists user = User.objects.get(username=request.POST.get('owner_new')) if not user: user = User.objects.create_user(request.POST.get('owner_new')) # HERE IS WHERE I'M STUMPED form.owner = user.id if form.is_valid(): # THIS FAILS BECAUSE form.owner ISN'T SET action = form.save(commit=False) action.created_by = request.user action.modified_by = request.user action.save() return redirect('action_register:index') else: form = ActionForm() return render(request, 'actions/create.html', {'form': form}) 
+4
source share
2 answers

You can try the following:

 def action_create(request): if request.method == 'POST': form = ActionForm(request.POST) # check if new user should be created if 'owner_new' in request.POST: # check if user already exists user, _ = User.objects.get_or_create(username=request.POST.get('owner_new')) updated_data = request.POST.copy() updated_data.update({'owner': user}) form = MyForm(data=updated_data) if form.is_valid(): # THIS FAILS BECAUSE form.owner ISN'T SET action = form.save(commit=False) action.created_by = request.user action.modified_by = request.user action.save() return redirect('action_register:index') else: form = ActionForm() return render(request, 'actions/create.html', {'form': form}) 

A cleaner way to do this:

add required=False to the owner field. Now,

 if form.is_valid(): # THIS FAILS BECAUSE form.owner ISN'T SET action = form.save(commit=False) if 'owner_new' in request.POST: user, _ = User.objects.get_or_create(username=request.POST.get('owner_new')) action.owner = user action.created_by = request.user action.modified_by = request.user action.save() return redirect('action_register:index') 
+5
source

I came to a similar situation and could not understand how to do it the way I wanted. As a result, I added a link to UserForm that allows the user to create a new owner , and then redirect back to ActionForm with the argument initial={owner: new_owner} included when creating the form instance.

+1
source

Source: https://habr.com/ru/post/1488138/


All Articles