Friday, January 30, 2015

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

Hi Eugene,

Would it work to something like this in your obj_create?'blueprint', None)


On Tuesday, January 27, 2015 at 11:01:56 AM UTC-5, Eugene Goldberg wrote:
  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 ="description") ="name")          workload.image ="image")          workload.flavor ="flavor")            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?


You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
To view this discussion on the web visit
For more options, visit

No comments:

Post a Comment