Search code examples
pythondjangotastypie

Tastypie overriding obj_create


I have the following resource and I am attempting to override obj_create. If I don't override it, things work perfectly, but when I override it it gives a POST error. Any leads? Would really appreciate an answer though I believe tastypie is really not getting any answers on stack overflow these days.. which is pretty annoying. I am thinking of possibly switching my stack for the same reasons..

The code is as follows:

class OrderResource(BackBoneCompatibleResource):
  person = fields.ToOneField(PersonResource, 'person', full=True)
  restaurant = fields.ToOneField(RestaurantResource, 'restaurant', full=True)
  itemList = fields.ToManyField(OrderItemResource, 'itemList', full=True)

  class Meta:
    object_class = Order
    queryset = Order.objects.all().order_by("-time_updated")
    resource_name = 'order'
    allowed_methods = ['get','post','put','delete','patch']
    authorization = Authorization()
    serializer = Serializer(formats=['json', 'jsonp', 'xml', 'yaml', 'html', 'plist'])
    authentication = ClientAuthentication()
    authorization = OrderAuthorization()
    always_return_data = True
    filtering = {
        "restaurant" : ["exact"],
        "time_created" : ["gte"],
        "person" : ["exact"]
    }

  def obj_create(self, bundle, request=None, **kwargs):
    print "Entered Order Create"
    return super(OrderResource, self).obj_create(bundle, request, **kwargs)

And the order model is :

class Order(models.Model):
  restaurant = models.ForeignKey(Restaurant)
  person = models.ForeignKey(Person)
  tableNumber = models.CharField(max_length=2)
  PLACED = 'p'
  ACCEPTED = 'a'
  READY = 'r'
  ORDER_STATUS_CHOICES = (
    (PLACED, 'Placed'),
    (ACCEPTED, 'Accepted'),
    (READY, 'Ready'),
  ) 
  order_status = models.CharField(max_length=1, choices=ORDER_STATUS_CHOICES, default=PLACED)
  itemList = models.ManyToManyField(OrderItem, null=True)
  tax = models.FloatField()
  tip = models.FloatField()
  cost = models.FloatField()
  time_created = models.DateTimeField(auto_now_add=True)
  time_updated = models.DateTimeField(auto_now=True)

As I said, if I remove the obj_create() function from the resource, post happens properly. I am unable to understand what wrong I am doing by just writing the default function as specified in the docs? I might be missing something very obvious. Thanks for your time..

The specific error I get is the following, not sure if it helps much though..

<type 'exceptions.TypeError'>, TypeError('obj_create() takes exactly 2 arguments (3 given)',), <traceback object at 0x10d30fb90>

Solution

  • Change this line:

    return super(OrderResource, self).obj_create(bundle, request, **kwargs)
    

    To this:

    return super(OrderResource, self).obj_create(bundle, request=request, **kwargs)
    

    request must be passed as a keyword argument.