Django Tastypie中的外键管理

时间:2016-02-02 06:00:33

标签: django tastypie

我正在尝试使用API​​插入数据,并希望管理将数据传递给外键的方式。但是我继续得到空值,即使它们在包中不是空的

MODELS.PY

class LCUser(models.Model):
        email = models.CharField(max_length=100,unique=True)
        password = models.CharField(max_length=100)
        first_name = models.CharField(max_length=100)
        last_name=models.CharField(max_length=100)

        class Meta:
            db_table = "user"

    class UserProfile(models.Model):
        user = models.ForeignKey(LCUser, on_delete=models.CASCADE,primary_key=True)
        mobile_phone = models.IntegerField()
        city=models.CharField(max_length=50)

        class Meta:
            db_table = "profile"

API.PY

class MultipartResource(object):

    def deserialize(self, request, data, format=None):

        if not format:
            format = request.META.get('CONTENT_TYPE', 'application/json')

        if format == 'application/x-www-form-urlencoded':
            return request.POST

        if format.startswith('multipart/form-data'):
            multipart_data = request.POST.copy()
            multipart_data.update(request.FILES)
            return multipart_data

        return super(MultipartResource, self).deserialize(request, data, format)

    def put_detail(self, request, **kwargs):
        if request.META.get('CONTENT_TYPE', '').startswith('multipart/form-data') and not hasattr(request, '_body'):
            request._body = ''
        return super(MultipartResource, self).put_detail(request, **kwargs)

    def patch_detail(self, request, **kwargs):
        if request.META.get('CONTENT_TYPE', '').startswith('multipart/form-data') and not hasattr(request, '_body'):
            request._body = ''
        return super(MultipartResource, self).patch_detail(request, **kwargs)    

class ProfileResource(MultipartResource,ModelResource):
#Get the id from the request and obtain the correcct user object
    user_id = LCUser.objects.get(id=1)  #For testing
    class Meta:
        queryset = UserProfile.objects.all()
        resource_name = 'profile'
        authorization = Authorization()

    def obj_create(self,bundle,request=None,**kwargs):
        try:
            print repr(bundle)
            bundle = super(ProfileResource, self).obj_create(bundle, request=request, **kwargs)
            bundle.obj.save()
        except:
            traceback.print_exc()
            raise BadRequest('Error')
        return bundle

REQUEST

POST /api/profile/? HTTP/1.1
Host: 127.0.0.1:8000
Cache-Control: no-cache
Postman-Token: c3cddfd3-a2ec-06fc-e916-ed5ff8f21077
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="user"

1
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="mobile_phone"

123
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="city"

123

当我尝试插入时,我收到了完整性错误, IntegrityError:列中的空值" user_id"违反非空约束 失败的行包含(null,123,123)

<Bundle for obj: 'UserProfile object' and with data: '{'mobile_phone': u'123', 'user': u'1', 'city': u'123'}'>

2 个答案:

答案 0 :(得分:1)

您需要在资源上手动指定相关字段:http://django-tastypie.readthedocs.org/en/latest/fields.html?highlight=meta#toonefield

class UserResource(MultipartResource, ModelResource):
    class Meta:
        queryset = User.objects.all()
        resource_name = 'user'

class ProfileResource(MultipartResource, ModelResource):
    user = fields.ToOneField(UserResource)

    class Meta:
        queryset = UserProfile.objects.all()
        resource_name = 'profile'
        authorization = Authorization()

    def obj_create(self, bundle, request=None, **kwargs):
        try:
            print repr(bundle)
            bundle = super(ProfileResource, self).obj_create(bundle, request=request, **kwargs)
            bundle.obj.save()
        except:
            traceback.print_exc()
            raise BadRequest('Error')
        return bundle

另外,请不要忘记配置您的授权和身份验证。

答案 1 :(得分:0)

我设法通过覆盖obj_create方法来修复它。

def obj_create(self, bundle, request=None, **kwargs):
    try:
        bundle.obj = UserProfile(user_id=bundle.data['user'], mobile_phone=bundle.data['mobile_phone'],city=bundle.data['city'])
        bundle.obj.save()
    except:
        traceback.print_exc()
        raise #On error raise it back to wrap_view()
    return bundle