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?
bundle.data.pop('blueprint', None)

Collin

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 = 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



--
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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7940ec09-b130-43ab-ab43-c25a26466284%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment