0
votes

Sorry for the lengthy question. I have a complicated situation with django modelform validation. I have a model UserProject ready and created many objects. I also have another model Action_Inputs to accept multiple parameters, which is a onetoonefield relation with UserProject. I do need customed input argument for one field of Action_Inputs. But I cannot have the form valided.

models.py

class UserProject(models.Model):
    pid = models.CharField(max_length=10, null=False, unique=True)
    email = models.EmailField(max_length=254, null=False)
    directory = models.CharField(max_length=255)

class Action_Inputs(models.Model):
    userproject = models.OneToOneField(UserProject, null=False)
    method = models.CharField(max_length=255)
    file = models.FileField(upload_to='userdata')

Now I have the following ModelForm which takes a customed input argument jobid, catched from url, which is a string to get back to the previous UserProject pid:

class ActionInputsForm(ModelForm):

    def __init__(self, jobid, *args, **kwargs):
        super(ActionInputsForm, self).__init__(*args, **kwargs)
        self.fields['userproject'].initial = jobid

    class Meta:
        model = Action_Inputs
        fields = ['userproject', 'method', 'file'] # userproject will be hidden

    def clean_userproject(self):
        userproject = self.cleaned_data['userproject']
        if len(userproject) != 10:
            raise forms.ValidationError("---PID error.")
        return UserProject.objects.get(pid=userproject)

    def clean(self):

        return self.cleaned_data

In my views.py

def parameters_Inputs(request, jobid):

    if request.method == "POST":
        form1 = ActionInputsForm(request.POST, request.FILES, jobid)

        if form1.is_bound:
            form1.save()
            return render(request, 'goodlog.html', {'jobid': jobid})

    elif request.method == "GET":
        form1 = ActionInputsForm(jobid)

    return render(request, 'inputsform.html',
        {'form1': form1, 'jobid': jobid})

Now the request.POST['userproject'] is empty, which means the jobid has not been modified by init, the request.FILES looks correct but the validation is false. It says Unicode object has no attrite get, which is related to the uploaded file. Any idea about what is wrong? Thanks very much.

The following works:(thanks to Vladimir Danilov)

def __init__(self, jobid, *args, **kwargs):
    super(ActionInputsForm, self).__init__(*args, **kwargs)
    self.fields['userproject'].initial = UserProject.objects.get(pid=jobid)


def clean_userproject(self):
    userproject = self.cleaned_data['userproject']
    if not userproject:
        raise forms.ValidationError("---UserProject not found.")
    return userproject

def parameters_Inputs(request, jobid):

if request.method == "POST":
    form1 = ActionInputsForm(jobid, request.POST, request.FILES)

.......

1

1 Answers

0
votes

Not answer, but do you mean ActionInputsForm instead of Action_Inputs in these lines?

form1 = Action_Inputs(request.POST, request.FILES, jobid)
# ...
form1 = Action_inputs(jobid)

Also, you should write ActionInputsForm(jobid, request.POST, request.FILES). Because in your case jobid will be request.POST.