Django model form is not getting saved in Postgresql database

269 views
Skip to first unread message

Ankhi Roy

unread,
Jun 3, 2019, 11:38:12 AM6/3/19
to Django users
Hi,

I am using Django admin - 2.2.1 and Postgresql as my database. So my problem is I have saved the user entered data in form.py on "POST" Request but it isn't saving in the postgresql database.

Model.py -

class modelForm(models.Model):
    CHOICES = [('Y', 'Yes'),
               ('N', 'No')]
    allow = models.CharField( max_length = 4 , choices = CHOICES)

    description = models.CharField( max_length=1000)

    usingtheproduct = models.CharField( max_length = 4 , choices = CHOICES)


 Forms.py file -



class productForm(forms.ModelForm):
    class Meta:
        model = modelForm

        fields = ["allow", "description", "usingtheproduct"]

        labels = {
            "allow": "Allow",
            "description": "Description",
            "usingtheproduct": "Using The Product"
        } 


first.html file -

 <form name = "form" action = "." method = "POST" >{% csrf_token %}
            {{form}}
         <input type = "submit", value = "submit">
    </form>


Views.py -



def first(request):
    template = loader.get_template('xyz/first.html')
    form = productForm(request.POST or None) # https://stackoverflow.com/questions/35748734/django-local-variable-form-referenced-before-assignment

    if request.method == 'POST':

        form = productForm(data = request.POST)

        if form.is_valid():

            allow = request.POST.get('Allow', '')
            decription = request.POST.get('Decription', '')
             usingtheproduct= request.POST.get('Using The Product', '')
            form.save() # This did not work as did not store user enetered data in postgresql database.
            return HttpResponse("It is getting saved!!!!") # I can see this message in browser after submitting the html form,
        else:
            print(form.errors)

    return HttpResponse(template.render({'form': form}, request))




setting.py file -

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'abc',
        'USER' : 'xyz',
        'PASSWORD' : '*********',
        'HOST': 'localhost', 
        'PORT':'',

    } # I have also created a database and a table with model name and have the exact columns as that of form input variables that is I have 3 column as allow, description, usingtheproduct in the postgresql database.
}


So not sure why the data is not getting saved in the postgresdatabase, am i missing something? Thanks for your time.

Thanks

Ankhi Roy

unread,
Jun 3, 2019, 6:16:52 PM6/3/19
to Django users
Hey, 

Nevermind I have solved the problem.
I was in an impression that I need to create a seperate database equivalent to django model form. Then realised that Django automatically creates a database table like databasename_modelclassname and have all the columns as per Django model form.

Anyway thanks for your time.

Thanks 



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9334a917-1539-4bfc-96f2-c2f617f2ff9d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Taylor Hughes-Scott

unread,
Jun 3, 2019, 7:39:03 PM6/3/19
to django...@googlegroups.com
With your form.save() call try passing commit=True so it looks like form.save(commit=True)

--

Taylor Hughes-Scott

unread,
Jun 3, 2019, 7:39:03 PM6/3/19
to django...@googlegroups.com
No problem. Glad you got it sorted. 

Reply all
Reply to author
Forward
0 new messages