tastypie

How to filter ToManyField of django-tastypie by request.user?

耗尽温柔 提交于 2019-11-30 22:03:18
I'm building an API with tastypie for a django app for data based on the user. The resources are like this: class PizzaResource(ModelResource): toppings = fields.ToManyField( 'project.app.api.ToppingResource', 'topping_set' ) class Meta: authentication = SessionAuthentication() queryset = Pizza.objects.all() def apply_authorization_limits(self, request, object_list): return object_list.filter(users=request.user) class ToppingResource(ModelResource): pizza = fields.ForeignKey(PizzaResource, 'pizza') class Meta: authentication = SessionAuthentication() queryset = Topping.objects.filter() The

django-tastypie: Posting to a Resource having ManytoMany field with through relationship

只谈情不闲聊 提交于 2019-11-30 21:04:45
I'm working on a API for a project and I have a relationship Order/Products through OrderProducts like this: In models.py class Product(models.Model): ... class Order(models.Model): products = models.ManyToManyField(Product, verbose_name='Products', through='OrderProducts') ... class OrderProducts(models.Model): order = models.ForeignKey(Order) product = models.ForeignKey(Product) ... Now, when I load an Order through the API I'd like to get the related Products as well, so I tried this (with django-tastypie): In order/api.py class OrderResource(ModelResource): products = fields.ToManyField(

django-tastypie and many to many “through” relationships

青春壹個敷衍的年華 提交于 2019-11-30 20:40:39
In Django and Tastypie I'm attempting to figure out how to properly deal with Many to Many "through" relationships, like those found here: https://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships Here are my sample models: class Ingredient(models.Model): name = models.CharField(max_length=100) description = models.TextField() class RecipeIngredients(models.Model): recipe = models.ForeignKey('Recipe') ingredient = models.ForeignKey('Ingredient') weight = models.IntegerField(null = True, blank = True) class Recipe(models.Model): title = models.CharField

Tastypie - Nested Resource field not found

心已入冬 提交于 2019-11-30 20:22:10
I have this code: #api model class VideoResource(ModelResource): class Meta: queryset = Video.objects.all() include_resource_uri = False resource_name = 'video' authorization = DjangoAuthorization() class QuestionResource(ModelResource): user = fields.ToOneField(UserResource,'user',full=True) video = fields.ForeignKey(VideoResource,'video',full=True) class Meta: queryset = Question.objects.all() resource_name = 'question' include_resource_uri = False authorization = DjangoAuthorization() def obj_create(self, bundle, request=None, **kwargs): import json temp = json.loads(request.body, object

Convert a queryset to json using tastypie resource

蹲街弑〆低调 提交于 2019-11-30 19:22:48
I have a tastypie resource for a model. I also have a view which comes up with a queryset which needs to be serialised and sent to client. I am looking for a way to let tastypie resource handle the serialisation and dehydration of the queryset. I see that I can pass a single object to [Resource.build_bundle(self, obj=None, data=None, request=None)][1] to create a bundle and then pass the bundle to [Resource.full_dehydrate(self, bundle)][2] and finally call [Resource.serialize(self, request, data, format, options=None)][3] on the dehydrated data. But I want to convert full queryset to json and

Django & TastyPie: request.POST is empty

不问归期 提交于 2019-11-30 18:43:13
I'm trying to do a POST using curl: curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"item_id": "1"}' http://www.mylocal.com:8000/api/1/bookmarks/ However, request.POST is always empty. Below is my ModelResource code: class BookmarkResource(ModelResource): class Meta: queryset = Bookmark.objects.all() resource_name = 'bookmarks' fields = ['id', 'tags'] allowed_methods = ['get', 'post', 'delete', 'put'] always_return_data = True authorization= Authorization() include_resource_uri = False def determine_format(self, request): return "application/json" def obj_create(self,

Using non-AMD compatible javascript module with require.js?

被刻印的时光 ゝ 提交于 2019-11-30 11:34:47
问题 I'm using require.js to help organize my Backbone.js based application. I'm trying to figure out the right way to use a 3rd party javascript library that is not AMD compatible with require.js The library in questions is backbone-tastypie.js. Basically what the library does is monkeypatch some of the prototype methods of Backbone to provide simpler support for the TastyPie Django REST framework. It does this by directly manipulating the Backbone object in the global namespace. However, since I

'Request header field Authorization is not allowed' error - Tastypie

主宰稳场 提交于 2019-11-30 10:54:44
I am getting the following error while using ApiKeyAuthentication for my Tastypie resources when I try to do an HTTP request using AJAX and Tastypie: XMLHttpRequest cannot load http://domain.com/api/v1/item/?format=json&username=popo&api_key=b83d21e2f8bd4952a53d0ce12a2314c0ffa031b1. Request header field Authorization is not allowed by Access-Control-Allow-Headers. Any ideas on how to solve this? Here are the request headers from Chrome: Request Headersview source Accept:*/* Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 Accept-Encoding:gzip,deflate,sdch Accept-Language:en-US,en;q=0.8 Access

Including child resources in a Django Tastypie API

≯℡__Kan透↙ 提交于 2019-11-30 09:23:15
I'm planning a site with Django and Tastypie for the REST API, and I'm having a tough time figuring out the "right" way to include child resources in a returned resource. As a sandbox, I made a small app with a Ticket model and a TicketComment model, where comments belong to a ticket. I looked at the Tastypie Cookbook recipe on nested resources (http://django-tastypie.readthedocs.org/en/latest/cookbook.html#nested-resources), but I'm having a hard time understanding why I should do that. The code below uses django.forms.models.model_to_dict() to get the comments into the ticket, but I'm

underscore.js - _.groupBy nested attribute

谁说胖子不能爱 提交于 2019-11-30 08:33:30
问题 Tastypie returns an array, including nested resources as follows: data = [ {"adult_price": "123", "child_price": "123", "currency": [{"abbrev": "USD", "id": "1", "name": "US Dollars", "resource_uri": "/api/v1/currency/1/", "symbol": "$"}], "day": [{"day_of_week": "TUE", "id": "2", "resource_uri": "/api/v1/days/2/"}], "description": "Please enter the tour description here", "id": "1", "important": "ex. Please contact us to negotiate a price if you want to book the Fiat for 1 person only.",