Dhanushka Amarakoon
Dhanushka Amarakoon

Reputation: 3762

Foreign key managment in Django Tastypie

I am trying to insert data using an API and want to manage the way in which it passes data to foreign keys. But I keep getting null values even though they are not null in the bundle

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

When I try to insert I get an Integrity error, IntegrityError: null value in column "user_id" violates not-null constraint Failing row contains (null, 123, 123)

bundle

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

Upvotes: 1

Views: 304

Answers (2)

Dhanushka Amarakoon
Dhanushka Amarakoon

Reputation: 3762

Well I managed to fix it by overwriting the obj_create method.

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

Upvotes: 0

Se&#225;n Hayes
Se&#225;n Hayes

Reputation: 4360

You need to manually specify related fields on your Resources: 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

Also, don't forget to configure your authorization and authentication.

Upvotes: 1

Related Questions