django-piston add value foreign key

I'm trying to insert from REST API Django-piston a new value on my database.

I have the follow models

class Mobile_User(models.Model):
id       = models.CharField(primary_key=True, max_length=255, unique=True)
email    = models.EmailField()
name     = models.CharField(max_length=300)

def __unicode__(self):
    return u"%s - %s" % (,

class Mobile_User_Business(models.Model):
mobile_user_id = models.ForeignKey(Mobile_User)
business_id = models.ForeignKey(Business)

def __unicode__(self):
    return u"%s - %s" % (self.business_id, self.mobile_user_id)


def create(self, request):
    Creates a new Mobile_User.
    if not hasattr(request, "data"): = request.POST
    attrs = self.flatten_dict(
    if self.exists(**attrs):
        msj = "The user mobile already exist"
        return msj
        mobile_user_business = Mobile_User_Business(mobile_user_id=attrs['mobile_user_id'], 
        return mobile_user_business

for send the information:

curl -u myuser:12345 -X POST -d mobile_user_id="21221212" -d business_id="1"

and that returns:

Traceback (most recent call last):

File "/dir/www/djangotest/apps/api/", line 122, in create business_id=attrs['business_id'],)

File "/usr/local/lib/python2.7/dist-packages/django/db/models/", line 352, in init setattr(self,, rel_obj)

File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/", line 331, in set,

ValueError: Cannot assign "u'21221212'": "Mobile_User_Business.mobile_user_id" must be a "Mobile_User" instance.

Any idea??



That's because you are trying to assign a unicode value to mobile_user_id, but django expects a Mobile_User instance instead.

You should change it to something like this:

    # First get the instances
    mobile_user = Mobile_User.objects.get(pk=attrs['mobile_user_id'])
    business = Business.objects.get(pk=attrs['business_id'])

    # Then use them in this model
    mobile_user_business = Mobile_User_Business(mobile_user_id=mobile_user, 
    return mobile_user_business

Need Your Help

jQuery .add method

jquery jquery-animate add

I'm writing a simple jQuery plugin that displays images with a specific class in the center of the screen (when clicked, full-sized) with a gray overlay around the image. When we click on the overl...

How to get access code along from Facebook which manually building a Login Flow

php facebook facebook-graph-api facebook-login facebook-access-token

I am trying to manually build a login flow, so that the our users are able to login to our website with their Facebook accout.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.