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

Converting Win16 C code to Win32

c windows winapi 16-bit

In general, what needs to be done to convert a 16 bit Windows program to Win32? I'm sure I'm not the only person to inherit a codebase and be stunned to find 16-bit code lurking in the corners.