Question

Just got introduced to django tastypie and having trouble sending data.I have a django model,

class OrderItem(SmartModel):
    shopping_id = models.CharField(max_length=255,db_index=True)
    quantity = models.IntegerField()
    item = models.ForeignKey(Item)
    option = models.ForeignKey(OptionalItem,null=True,blank=True)
    toppings_and_extras = models.ManyToManyField(ToppingsAndExtra,null=True, blank=True)

and the tastypie resource,

class OrderItemResource(ModelResource):
    item = fields.ToOneField(ItemResource,'item',null=False,blank=False,full=True)
    option = fields.ToOneField(OptionResource,'option',null=True,blank=True,full=True)
    toppings_and_extras = fields.ToManyField(ToppingResource,'toppings_and_extras',null=True,blank=True,full=True)
    class Meta:
        queryset = OrderItem.objects.all()
        resource_name = 'order'
        authorization = Authorization()
        always_return_data =True

I perform a test POST as illustrated in the tastypie docs,

curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"shopping_id":"(TBH5@Y4NQDD$PJWUCQ4WEG3%CL0RSDPUD%EDQ!EG$81^WXSV6^URBC0OO45OC(IV)QW7WC(W0GD9)N&5HXLA1)8M1IHGWQ4A&P1","quantity":"5","item":"/api/v1/menu_item/1/","option":"/api/v1/options/1/","created_by":"/api/v1/user/1/","modified_by_id":"/api/v1/user/1/","toppings_and_extra":"[]"}' http://localhost:8000/api/v1/order/

The request responds with an error "current transaction is aborted",looking at the logs, the error their is null value in column "created_by_id" violates not-null constraint. This is what i don't understand, because i thought by adding "created_by":"/api/v1/user/1/" takes care of the problem. What am i not doing right, my research is limited because i cannot seem to understand what is going wrong. Links to helpful information in docs is also appreciated.

Was it helpful?

Solution

To access any relation field you have to define it in ModelResource.

class OrderItemResource(ModelResource):
    created_by = fields.ToOneField(UserResource,'created_by')
    [...]

    class Meta:
        queryset = OrderItem.objects.all()
        resource_name = 'order'
        authorization = Authorization()
        always_return_data =True

The reason why you have to do it is that Tastypie has to know ModelResource definition of that model: resource name, query set and authorization access among others. Without it Tastypie will ignore it.

If you haven't defined UserResource yet, you should do it.

I see you are using base model for all models: "SmartModel". I think the best for you will be creation of base model resource to inherit. "SmartModelResource" and you will define needed fields there.

class SmartModelResource(ModelResource):
    created_by = fields.ToOneField(UserResource,'created_by')
    [...]


class OrderItemResource(SmartModelResource):
    [...]

    class Meta:
        queryset = OrderItem.objects.all()
        resource_name = 'order'
        authorization = Authorization()
        always_return_data =True
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top