django / tastypie - how to exclude a resource attribute from obj_create, but keep it for listing

129 views
Skip to first unread message

Eugene Goldberg

unread,
Jan 27, 2015, 11:01:56 AM1/27/15
to django...@googlegroups.com
  I have the following tastypie resource:

 class WorkloadResource(ModelResource):
    # blueprints = ManyToManyField(Blueprint, 'blueprints')
    blueprint = fields.OneToManyField('catalog.api.BlueprintResource', attribute='blueprint',
                                         related_name='workloads', full=True, null=True)

    def obj_create(self, bundle, request=None, **kwargs):
        # return super(WorkloadResource, self).obj_create(bundle, request, **kwargs)
        return super(WorkloadResource, self).obj_create(bundle, request=request, **kwargs)

    def obj_update(self, bundle, request=None, **kwargs):

        workload = Workload.objects.get(id=kwargs.get("pk"))
        workload.description = bundle.data.get("description")
        workload.name = bundle.data.get("name")
        workload.image = bundle.data.get("image")
        workload.flavor = bundle.data.get("flavor")
        workload.save()

        def obj_delete(self, bundle, **kwargs):

            return super(WorkloadResource, self).obj_delete(bundle)

        def determine_format(self, request):
            return 'application/json'

    class Meta:
        queryset = Workload.objects.all()
        resource_name = 'workload'
        authorization=Authorization()
        filtering = {
            "blueprint": ('exact', ),
        }


When I use curl to POST:

curl -i -H "Content-Type: application/json" -X POST -d
@wkl.json http://localhost:8000/api/workload/

where wkl.json is:

{ "name":"w 5", "description":"w 5 desc" }


I get this error:

AttributeError: 'Workload' object has no attribute 'blueprint'

I do need to have this attribute, so I can list all child workloads for a given blueprint
like this: GET /api/workload/?blueprint=1

 but I need to exclude this 'blueprint' attribute from participating in the obj_create

 What is the proper place and syntax to do this?


-Eugene



Collin Anderson

unread,
Jan 30, 2015, 8:28:52 AM1/30/15
to django...@googlegroups.com
Hi Eugene,

Would it work to something like this in your obj_create?
bundle.data.pop('blueprint', None)

Collin
Reply all
Reply to author
Forward
0 new messages