我正在尝试从 REST API Django-piston 在我的数据库中插入一个新值。
我有以下型号
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" % (self.id, self.email)
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)
和 handlers.py
def create(self, request):
"""
Creates a new Mobile_User.
"""
if not hasattr(request, "data"):
request.data = request.POST
attrs = self.flatten_dict(request.data)
if self.exists(**attrs):
msj = "The user mobile already exist"
return msj
else:
mobile_user_business = Mobile_User_Business(mobile_user_id=attrs['mobile_user_id'],
business_id=attrs['business_id'],)
mobile_user_business.save()
return mobile_user_business
发送信息:
curl -u myuser:12345 -X POST -d mobile_user_id="21221212" -d business_id="1" http://mydomain.com/api/newuser/
并返回:
回溯(最近一次通话最后):
文件“/dir/www/djangotest/apps/api/handlers.py”,第 122 行,在 create business_id=attrs['business_id'],)
文件“/usr/local/lib/python2.7/dist-packages/django/db/models/base.py”,第 352 行,在init setattr(self, field.name, rel_obj)
文件“/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py”,第 331 行,在set self.field.name, self.field.rel.to._meta 中.object_name))
ValueError:无法分配“u'21221212'”:“Mobile_User_Business.mobile_user_id”必须是“Mobile_User”实例。
任何的想法??
谢谢!