0

Model

class Publisher(models.Model):
    name = models.CharField(max_length=30)
    address = models.CharField(max_length=50)
    city = models.CharField(max_length=60)
    state_province = models.CharField(max_length=30)
    country = models.CharField(max_length=50)
    website = models.URLField()

Form

from django import forms
class ContactForm(forms.Form):
    name = forms.CharField()
    address = forms.CharField()
    city = forms.CharField()
    state_province = forms.CharField()
    country = forms.CharField()
    website = forms.URLField() 

And view

def test(request):
    if request.method=='POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            print  form.cleaned_data
            p=Publisher()
            p.name=form.cleaned_data.get('name')
            p.address=form.cleaned_data.get('address')
            p.city=form.cleaned_data.get('city')
            p.state_province=form.cleaned_data.get('state_province')
            p.country=form.cleaned_data.get('country')
            p.website=form.cleaned_data.get('website')
            p.save()
            return HttpResponse("Done")
else:
        form = ContactForm()
    return render(request, 'contact_form.html', {'form': form})

I am saving the data to the database when the submitted form is valid. But what I did is extracting each field from dictionary form.cleaned_data and then assigned it to instance of Publisher object manually for e.g p.name=form.cleaned_data.get('name')

My question is that is there is any way to assign the form.cleaned_data dictionary to Publisher object. In short can i do it like ,

p=Publisher()
p=form.cleaned_data
p.save()
1

2 Answers 2

1

A perfect candidate for ModelForms

from django import forms
class ContactForm(forms.Form):
    class Meta:
        model = Publisher

and views:

def test(request):
    form = ContactForm()
    if request.method=='POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            publisher = form.save()
            return HttpResponse("Done")
    return render(request, 'contact_form.html', {'form': form})
Sign up to request clarification or add additional context in comments.

Comments

1

You should use ModelForms, it fits your case perfectly..

Full docs are available here

But I will try and explain how to use your case with a ModelForm (although I'd suggest you go through the docs themselves and try it yourself)

Considering your existing model, create a forms.py file in which you need to create a ModelForm class as follows:

#Import your model here
from .models import Publisher
from django.forms import ModelForm

class PublisherForm(ModelForm):
    class Meta:
        model = Publisher
        fields = '_all_'

Note: You can use the 'fields' option to tell Django to restrict the fields it will be showing in the form. By default 'all' is used to show and use all fields from your defined Publisher model. After creating your ModelForm, you can populate it using a request.POST(or any other way)

f = PublisherForm(request.POST)

To save the data directly to the corresponding model, simply call the save() method. Remember, you are using a ModelForm, not a regular forms.Form, which means Django now knows which model to save to, so you can simply call .save() on your ModelForm instance.

f.save()

If you wish to perform cleanup operations on the populated form before saving, that can also be done. Check the docs for detailed explanations regarding that.

Comments

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.