0

There are a lot of questions on SO asking nearly this very same question, but it seems every one has a different issue at the heart of it. With that in mind, I have combed through the questions from the past several years without luck finding a solution to my own unique issue. I think I just need a pair of eyes more experienced than mine.

I'm trying to render a form on a profile information edit page. The form is supposed to draw from two models, the main auth.User and a profile model connected through OneToOne. The issue is that the form not only doesn't render in HTML, but also shows no sign of life in the manage.py shell prompt. First, I'll show you the form itself, then I'll show the output of the prompt. I'm not going to show my views unless asked for the sake of simplicity and because I'm ruling them out for the moment.

forms.py:

from django import forms
from django.contrib.auth.models import User
from .models import People, Pick # this is actually for a later forms, which don't work either

class ProfileForm(forms.Form):
    class Meta:
        model = User
        fields = ['people__usr_img', 'email', 'password', 'people__middle_name', 'people__gender', 'people__preference',]

And my shell output:

$ > python manage.py shell
...Python meta stuff...

>>> from apps.catch.forms import ProfileForm
>>> ProfileForm()
<ProfileForm bound=False, valid=Unknown, fields=()>
>>> form = ProfileForm()
>>> form.as_p()
''
>>> for field in form:
...     print("Field")
... 
>>> 

It seems to me that what's at the heart of this issue is the fields=() declaration. I have the fields defined explicitly in Meta, so why am I getting nothing here? It should be mentioned that I am having the same issue with my other forms as well.

3
  • In template we use {{ form.as_p }}, but in interactive shell form.as_p() Commented May 12, 2016 at 3:36
  • @slackmart Thanks. I've updated the shell code section of the question Commented May 12, 2016 at 3:48
  • Fine, I think you want to use forms.ModelForm instead forms.Form. The ModelForm class allows you to use a metaclass to override the target model (i.e. User). Also it forces you to specify a tuple with the desired fields. Please note that that fields must match the User fields. Commented May 12, 2016 at 4:06

1 Answer 1

2

Given that you are designing the form which is interacting User model of yours, if you user forms.ModelForm instead of forms.Form, it will work for you.

Sign up to request clarification or add additional context in comments.

1 Comment

This seems to be the answer I am looking for in this particular case. It still doesn't fix the issue with my other form, but I didn't expect you to fix something I wasn't asking about :) Thank you Rajesh.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.