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 6864711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:56:46+00:00 2026-05-27T02:56:46+00:00

I have encountered a problem when I was trying to add/post a data to

  • 0

I have encountered a problem when I was trying to add/post a data to my models.
this is what i have done in my python manage.py shell:

>>> from booking.models import *
>>> qa = Product.objects.get(id=5)
>>> sd = Booking.objects.create(
... date_select = '2011-11-29',
... product_name = qa.name,
... quantity = 1,
... price = qa.price,
... totalcost = 20,
... first_name = 'lalala',
... last_name = 'sadsd',
... contact = '2321321',
... product = qa.id)
Traceback (most recent call last):
  File "<console>", line 10, in <module>
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 138, in create
    return self.get_query_set().create(**kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 358, in create
    obj = self.model(**kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 352, in __init__
    setattr(self, field.name, rel_obj)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 331, in __set__
    self.field.name, self.field.rel.to._meta.object_name))
ValueError: Cannot assign "5": "Booking.product" must be a "Product" instance.

i don’t have any idea why is that happening…
it is product = qa.id not equal to the Booking.product?

here is my model.py

from django.db import models

# Create your models here.
class Product(models.Model):
    name = models.CharField(max_length=50, null=True, unique=False)
    quantity = models.IntegerField(max_length=10, null=True)
    price = models.FloatField()
    def __unicode__(self):
        return self.name

class Booking(models.Model):
    date_select = models.DateField(auto_now=False, auto_now_add=False)
    product_name = models.CharField(max_length=50, blank=True, null=True)
    quantity = models.IntegerField(max_length=10, blank=True, null=True)
    price = models.FloatField()
    totalcost = models.FloatField()
    first_name = models.CharField(max_length=50, null=True, blank=True, unique=False)
    last_name = models.CharField(max_length=50, null=True, blank=True, unique=False)
    contact = models.CharField(max_length=50, null=True, blank=True, unique=False)
    product = models.ForeignKey(Product)
    def __unicode__(self):
        return self.first_name

and my handlers.py

from django.utils.xmlutils import SimplerXMLGenerator
from piston.handler import BaseHandler
from booking.models import *
from piston.utils import rc, require_mime, require_extended, validate
class BookingHandler(BaseHandler):
    allowed_method = ('GET', 'POST', 'PUT', 'DELETE')
    fields = ('id', 'date_select', 'product_name', 'quantity', 'price','totalcost', 'first_name', 'last_name', 'contact', 'product')
    model = Booking

    def read(self, request, id):
        if not self.has_model():
            return rc.NOT_IMPLEMENTED
        try:
            inst = self.model.objects.get(id=id)
            return inst
        except self.model.MultipleObjectsReturned:
            return rc.DUPLICATE_ENTRY
        except self.model.DoesNotExist:
            return rc.NOT_HERE

    def create(self, request, *args, **kwargs):
        if not self.has_model():
            return rc.NOT_IMPLEMENTED

        attrs = self.flatten_dict(request.POST)
        if attrs.has_key('data'):
            ext_posted_data = SimplerXMLGenerator(request.POST.get('data'))
            attrs = self.flatten_dict(ext_posted_data)
        try:
            inst = self.model.objects.get(**attrs)
            return rc.DUPLICATE_ENTRY
        except self.model.DoesNotExist:
            inst = self.model(**attrs)#error??
            inst.save()
            return inst
        except self.model.MultipleObjectsReturned:
            return rc.DUPLICATE_ENTR

does anyone can give me a hand about my situation?

thanks in advance…

  • 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-05-27T02:56:46+00:00Added an answer on May 27, 2026 at 2:56 am

    You assign the product as

    product = qa.id
    

    While you should do

    product = qa
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have encountered a problem when trying to select data from a table in
I have encountered a problem of placing data into the Table View. Here is
I've encountered a problem while trying to use the answer from a NDSolve in
I'm diving into EMF now, and have encountered the following problem: I'm trying to
I'm trying to solve the following real-life problem you might have encountered yourselves: You
While trying to use LINQ to SQL I encountered several problems. I have table
I have encountered a problem that I have not come accross yet when setting
I have encountered a problem twice now whereby a producer thread produces N work
I have encountered a problem in my application. I have two forms, one that
I'm migrating a TSQL stored procedure to PL/SQL and have encountered a problem -

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.