tastypie

How to make tastypie give the url of a file instead of its path?

蓝咒 提交于 2019-12-11 05:09:10
问题 I'm using Django and Tastypie to create a RESTful web app. I have a model like this class Picture(models.Model): # some other fields image = models.ImageField('Image') def image_url(self): return self.image.url Tastypie will give the image's path but actually I need its url. How to write a correct resource api to achive this? 回答1: By default TastyPie is supposed to return URL of ImageField and FileField. See here: https://github.com/toastdriven/django-tastypie/blob/master/tastypie/fields.py

passing request variables in django/tastypie resources

巧了我就是萌 提交于 2019-12-11 03:44:18
问题 I need to execute filter query inside tastypie resources. the input should be the headers of url for example new Ext.data.Store({ proxy: { url :'api/users/' type: "ajax", headers: { "Authorization": "1" } } }) I tried below from tastypie.authorization import Authorization from django.contrib.auth.models import User from tastypie.authentication import BasicAuthentication from tastypie import fields from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS from tastypie.validation

Django Tastypie Record-Level Authorization

只谈情不闲聊 提交于 2019-12-11 03:13:44
问题 I'm having trouble implementing record-level authorization with tastypie 0.9.12+. My setup looks something like this: Model class UserProfile(models.Model): def __unicode__(self): return self.user.get_full_name() user = models.OneToOneField(User) class Sample(models.Model): def __unicode__(self): return '%s' % self.id OPEN = 0 CLAIMED = 1 CLOSED = 2 MANUAL = 3 MODIFIED = 4 DELETED = 5 ERROR = 6 RESERVED = 7 STATUS_CHOICES = ( (OPEN, 'Open'), (CLAIMED, 'Claimed'), (CLOSED, 'Closed'), (MANUAL,

Tastypie & raw sql

独自空忆成欢 提交于 2019-12-11 03:09:02
问题 How do I get Tastypie to fetch a raw sql queryset? queryset = Foo.objects.raw(sql) does not seem to be working. Is it not possible? 回答1: queryset = super(class_name, self).get_query_set() return queryset.whatever() 回答2: This seems to work: class BarResource(ModelResource): class Meta: queryset = Bar.objects.all() def dehydrate(self, bundle): qs = Bar.objects.raw('SELECT * FROM foo_bar') return [row for row in qs] 回答3: You can try to override the dehydrate function to include raw sql syntax

Tastypie returns string representation rather than valid array for reverse foreignkey lookup

此生再无相见时 提交于 2019-12-11 02:19:54
问题 UPDATE: Is the dehydrate within the LocationResource to blame? I have an usual situation where a reverse lookup via a foreignkey is returning a nested string representation of an array, rather than the array itself. [I discovered this while using tastypie with backbone.js and backbone.marionette.][1] The strange thing is that this does NOT happen when doing a "forward" or normal lookup, even if there are nested arrays returned from fields.ForeignKey resources For example: Forward lookups: [{

Filtering on reverse relations

淺唱寂寞╮ 提交于 2019-12-11 01:38:30
问题 I have tastypie and Django set up, and they work well, I can filter, and patch objects via HTTP. Now I want to try filtering my results on a reverse relationship and I am having trouble getting it working. So my Django models are like so, each library object has a multiplex index, and each multiplex index may have multiple libraries it is used with : class MultiplexIndex(models.Model): multiplex_index_name = models.CharField(max_length = 100, unique=True ) multiplex_index_seq = models

Best Practices in Retrieving Related Data in a REST API

点点圈 提交于 2019-12-10 18:46:43
问题 So I have a REST API in which I Have a resource in which other resources are linked to (related models, in programming point of view). So how I am doing it right now is that whenever I request for a resource, related resources are referenced via URLs ('/related_data/related_data_id/'). However, I worry that, let's say there are 5 related resources to the one I'm retrieving, is that I would perform 5 GET requests. I am writing an iPhone client and I'm wondering if this is how to properly do it

Django model not saving when calling save()

99封情书 提交于 2019-12-10 17:56:48
问题 so i am trying to save a django model, and for some reason i am only getting a 500 internal server error. the thing is, if i comment the social_auth.save() it works and i can manipulate the object, but not save it why is this happening? i am using django tastypie and i am trying to save a django-social-auth instance. def obj_create(self, bundle, request=None, **kwargs): try: #this is not supposed to upgrade password bundle = super(UserResource, self).obj_create(bundle) bundle.obj.save() if

Django: A more DRY way to prevent edit/delete of objects?

瘦欲@ 提交于 2019-12-10 16:25:39
问题 After reading the permission Django documentation, I'm still confused. I'd like to prevent access for user to edit or delete objects they didn't own. I dit it this way and it works: In views.py: def deleteReward(request, reward_id): reward = get_object_or_404(Reward, pk=reward_id) if reward.owner.user != request.user: # if the user linked to the reward is not the current one raise Exception("This reward is not yours, you can't delete it !") #... But I think this isn't clean and DRY for two

django tastypie manytomany field POST json error

本小妞迷上赌 提交于 2019-12-10 12:08:59
问题 Here are my resources: class CourseResource(ModelResource): subjects = fields.ToManyField('core.api.SubjectResource', 'subjects', full=True) class Meta: queryset = Course.objects.all() resource_name = 'course' authorization = Authorization() validation = FormValidation(form_class=CourseForm) class SubjectResource(ModelResource): class Meta: queryset = Subject.objects.all() resource_name = 'subject' authorization = Authorization() I am trying to post using curl on a django-tastypie system.