Search code examples
djangoapitastypie

Getting Primary Key from Tastypie Resource URI


I have a Django - tastypie Resource as follows. It has multiple fields that has Many to Many Relations.

I am trying to get the fields "workflow_initiators", "workflow_submitters" and "workflow_approvers" and add the user to a respective group namely initiators, submitters and approvers.

My JSON Request as follows :

{

"workflow_approvers": [
"/client/api/v1/user/44/",
"/client/api/v1/user/6/"
],
"workflow_dept": [
"/client/api/v1/departments/1/",
"/client/api/v1/departments/2/"
],
"workflow_initators": [
"/client/api/v1/user/44/",
"/client/api/v1/user/6/"
],
"workflow_name": "Hello Workflow",
"workflow_submitters": [
"/client/api/v1/user/43/",
"/client/api/v1/user/6/"
],

}

I want to get the primary key from resource uri of tastypie in a hydrate() or a obj_create() method. In order to get the pk i used a function get_pk_from_uri(). But it throws an error of the following

error : global name 'get_pk_from_uri' is not defined

My Resource as follows :


class WorkflowResource(ModelResource):

    workflow_dept = fields.ToManyField(DepartmentsResource, 'workflow_dept', related_name='departments', full=True)
    workflow_initators = fields.ToManyField(UserResource, 'workflow_initators', related_name='user')
    workflow_submitters = fields.ToManyField(UserResource, 'workflow_submitters', related_name='user')
    workflow_approvers = fields.ToManyField(UserResource, 'workflow_approvers', related_name='user')

    def obj_create(self, bundle, **kwargs):
        submitters = bundle.data.get('workflow_submitters', [])
        for submitter in submitters:
            print(get_pk_from_uri(submitter)) # Throws Error

            #Adding User to Group Logic
            # g = Group.objects.get(name='submitters') 
            # g.user_set.add(your_user)

    class Meta:
        queryset = WorkflowIndex.objects.filter(is_active=True)
        resource_name = 'workflows'
        list_allowed_methods = ['get', 'post']
        detail_allowed_methods = ['get', 'post', 'put', 'delete', 'patch']
        serializer = Serializer()
        default_format = 'application/json'
        authentication = Authentication()
        authorization = DjangoAuthorization()
        always_return_data = True


Is there any other method to get the primary key and other fields from resource uri ? I did see get_via_uri() method but was unsure on how to implement the same.

Kindly guide me in resolving this issue.

References :

  1. Stackoverflow - Get model object from tastypie uri
  2. Tastypie Documents - get_via_uri()

Solution

  • You should go back to this post: Get model object from tastypie uri?

    The get_pk_from_uri(uri) method that you can see in this answer is not part of the source code of Tastypie, as you can check here.

    I suppose the guy wrote it by himself, and you should do the same sothat you won't get the error : global name 'get_pk_from_uri' is not defined error. I didn't tested his method thought.