Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set form field value before is_valid()

I'm having a bit of trouble grasping how to do this. I've put my best effort into searching Google without any luck.

I'll start with a bit of code and explain what I'm trying to do as 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, there is a dropdown field for owner. I have an icon that allows the user to enter a new username in a text field instead for owner. I check to see if owner_new was submitted and if so, create that user. I then need to set the owner field to that value so that form.is_valid() will be 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})
like image 667
Jared Avatar asked Jun 25 '13 17:06

Jared


People also ask

How do you exclude a specific field from a ModelForm?

Set the exclude attribute of the ModelForm 's inner Meta class to a list of fields to be excluded from the form.

What is form Is_valid ()?

Form. is_valid () The primary task of a Form object is to validate data. With a bound Form instance, call the is_valid() method to run validation and return a boolean designating whether the data was valid: >>> data = {'subject': 'hello', ... '

How to validate fields in Django?

Django provides built-in methods to validate form data automatically. Django forms submit only if it contains CSRF tokens. It uses uses a clean and easy approach to validate data. The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class.

How to initialize form in Django?

Method 1 – Adding initial form data in views.py This first and most commonly used method to add initial data through a dictionary is in view.py during the initialization of a form. Here is the code of views.py with some added data. Now open http://127.0.0.1:8000/.


2 Answers

You can try this:

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 of doing this is:

add required=False to the owner field. Now,

if form.is_valid(): # THIS DOES NOT FAIL EVEN IF 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')
like image 93
karthikr Avatar answered Oct 08 '22 12:10

karthikr


I came into a similar situation and couldn't figure out how to do it the way I wanted. What I ended up with was putting a link to a UserForm which allows a user to create a new owner, and then redirect back to the ActionForm with the argument initial={owner: new_owner} included when instantiating the form.

like image 30
Daniel Rosenthal Avatar answered Oct 08 '22 12:10

Daniel Rosenthal