[Answered ]-Tastypie: how to set default value for a m2m field?

2👍

There are two possible way to handle the m2m relation at django-tastypie side.

One to to override the obj_create function. Here is for more help.

class QuestionResource(ModelResource):
   options = fields.OneToManyField('qa.api.OptionResource', 'options', full=True, blank=True)
   class Meta:
      queryset = Question.objects.all()

   def obj_create(self, bundle, request, **kwargs):
       print "hey we're in object create"
       # do something with bundle.data,
       return super(QuestionResource, self).obj_create(bundle, request, **kwargs)

And second way is to do it through curl request.

curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"body":"test", "options": ["/api/v1/option/1/"]}' http://localhost:8000/api/0.1/question/
👤Ahsan

Leave a comment