Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8610237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:04:55+00:00 2026-06-12T04:04:55+00:00

I created a form to add products, but I get an error like ValueError

  • 0

I created a form to add products, but I get an error like ValueError at /product/add_product/ Cannot assign “u’1′”: “Product.category” must be a “Category” instance.

I am assuming it has something to do with not sending the Foreign Key values right, when i use the print statement i can see the values that are been passed from the form,

Am i saving the data properly?

My model.py

from django.db import models

class Category(models.Model):
    name = models.CharField(max_length=250)

    def __unicode__(self):
        return self.name

class Product(models.Model):
    category = models.ForeignKey(Category)
    product = models.CharField(max_length=250)
    quantity = models.IntegerField(default=0)
    price = models.FloatField(default=0.0)

    def __unicode__(self):
        return self.product

My views.py

def add_product(request):
    post = request.POST.copy()

    category = post['category']
    product = post['product']
    quantity = post['quantity']
    price = post['price']

    new_product = Product(category = category, product = product, quantity = quantity, price = price )       
    return render_to_response('product/add_product.html')

EDIT: This is how my HTML page form looks like

<form method="post" action="add_product/">
        {% csrf_token %}
        <label for="category">Category</label>
        <select name="category" id="category">
        {% for category in category_list %}
          <option> {{ category.id }} </option>
        {% endfor %}
        </select>

        <label for="product">Product</label>
        <input type="text" name="product" id="product">

        <label for="quantity">Quantitiy</label>
        <input type="text" name="quantity" id="quantity">

        <label for="price">Price</label>
        <input type="text" name="price" id="price">

        <input type="submit" value="Add New product" id="create">
    </form>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:04:56+00:00Added an answer on June 12, 2026 at 4:04 am

    You are using category as category = post['category'] which will give post['category'] as unicode string with value of id field. Instead of that you can do

    category = Category.objects.get(id=post['category'])
    

    But I would suggest to use modelforms (if you haven’t done) to build for form and save the objects which will give you much more functionality for validation, error handling etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a textbox in a form that I created. I want to add
I am trying to using a jquery.validate.unotrusive.js plugin to dynamically created form fields like:
I am feeling like an idiot right now, but I cannot for the life
Im writing my first online shop in PHP. Now working on add product form
I have a strange one. Create a new form. Then add the following function
I created a form in CodeIgniter framework that successfully uploads an image using the
I created a form with a label, textbox and a button. In the form
I created a form that overlays on top of another form in an alpha
I created a form application with 1 button and 1 text box to see
I created a form using Swing in Java. In the form I have used

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.