views:

55

answers:

2

So I can create all the necessary db fields in my models.

I can create forms to access these models.

And if I create models from forms:

http://docs.djangoproject.com/en/dev/topics/forms/modelforms/

I cant understand why I have to redefine the Fields like these:

class AuthorForm(forms.Form):
    name = forms.CharField(max_length=100)
    title = forms.CharField(max_length=3,
                widget=forms.Select(choices=TITLE_CHOICES))
    birth_date = forms.DateField(required=False)

Why do i have to define CharField here? Didnt I do that in the models.py already?

Thanks a lot

A: 

You are using forms.Form rather than forms.ModelForm and declare, which model you are using (in class Meta). Maybe that's the problem. Could you describe, for which model of your you are having the problem?

gruszczy
+1  A: 

Building on what gruszczy said if you have a model named Author you can declare a ModelForm for this model thus:

class AuthorForm(forms.ModelForm): # Note the different superclass. 
    class Meta:
        model = Author

If you want a form that includes all attributes of Author then that is all you need to do. If however you need only a subset of attributes of Author (say name and title) then you can modify the model form thus:

class AuthorForm(forms.ModelForm): # Note the different superclass. 
    class Meta:
        model = Author
        fields = ('name', 'title',)

Note that you didn't have to use CharField here.

If on the other hand you want everything but name and title, then you can define your form like this:

class AuthorForm(forms.ModelForm): # Note the different superclass. 
    class Meta:
        model = Author
        exclude = ('name', 'title',)

Update

(After reading comment) You can treat AuthorForm like any other regular form. In your view you can instantiate this form with or without data based on the request.method. Something like this:

def add_author(request, *args, **kwargs):
    if request.method == 'GET':
        form = AuthorForm()

    elif request.method == 'POST':
        form = AuthorForm(request.POST.copy())
        if form.is_valid():
             author = form.save()
             # Redirect to listing etc.

    context = dict(form = form)
    return render_to_response(template_name, context) # etc.

And in your template:

{{ form.as_p }}

There are different ways to render a form; as_p is but one of them.

Manoj Govindan
+1 for clear and short explanation!
KillianDS
Thanks!. And how would I insert this "Author" form into the html code? Thanks again, evry insightful!
MacPython
@MacPython: this form can be treated like any other `Form`. You can instantiate this form from the view (with or without data based on request method) and pass it along to the template. In your template you can display the form using one of the utility methods. I've updated my post to demonstrate this.
Manoj Govindan
Master! Will try that! Thanks a million! So where would form.as_p go into the form html code? What does as_p stand for? Thanks again!!
MacPython
@MacPython: (1) `form.as_p` will go into whichever place in the output HTML where you wish to display the form to the user (2)`as_p` will render the form as HTML using `<p></p>` tags. There are other variants to render the form as a list (`<ul></ul>`), table (`<table></table>`) etc. See the documentation for more info on those.
Manoj Govindan
Great! Thanks so much!
MacPython