tastypie

Django-tastypie One-To-Many relationship

浪尽此生 提交于 2019-12-03 13:00:36
I'm trying to create a resource (Observation) that has 0 to unlimited comments. I'm stuck at the following error: "error": "The model '<Observation: Observation object>' has an empty attribute 'comments' and doesn't allow a null value." Also, adding null=True to comments = (...) will result in empty comment objects even though there should be comments for observations in question. I've also tried messing around with CommentResource2 path by changing it to full path. I've been following the reverse relationship guide from Tastypie's documentation: Reverse “Relationships” Here are my models:

Put a custom http header in backbone

我的未来我决定 提交于 2019-12-03 12:53:13
问题 I am creating an API with Tastypie and I want to access to the API from Backbone. To send credentials I use an user_id and a api_key. I do this in android and with curl and this work great, but I can set the http header from backbone. In curl I use: curl --dump-header - -H "Accept: application/json" -H "Content-Type: application/json" -H "user_id: 32" -H "api_key: 69950" -X DELETE "http://127.0.0.1:8000/api/v1/deletenote/66/?format=json" and in android java I use: HttpDelete requestDELETE =

Uploading files to tastypie with Backbone?

烂漫一生 提交于 2019-12-03 08:59:11
Checked some other questions and I think my tastypie resource should look something like this: class MultipartResource(object): def deserialize(self, request, data, format=None): if not format: format = request.META.get('CONTENT_TYPE', 'application/json') if format == 'application/x-www-form-urlencoded': return request.POST if format.startswith('multipart'): data = request.POST.copy() data.update(request.FILES) return data return super(MultipartResource, self).deserialize(request, data, format) class ImageResource(MultipartResource, ModelResource): image = fields.FileField(attribute="image")

Tastypie, filtering many to many relationships

一个人想着一个人 提交于 2019-12-03 07:42:16
I have two models that are linked by another model through a many to many relationship. Here's the models themselves class Posts(models.Model): id = models.CharField(max_length=108, primary_key=True) tags = models.ManyToManyField('Tags', through='PostTags') class Tags(models.Model): id = models.CharField(max_length=108, primary_key=True) posts = models.ManyToManyField('Posts', through='PostTags') class PostTags(models.Model): id = models.CharField(max_length=108, primary_key=True) deleted = models.IntegerField() post_id = models.ForeignKey('Posts', db_column='post_field') tag_id = models

How to load the foreign keys elements in Tastypie

一曲冷凌霜 提交于 2019-12-03 07:33:38
In my Django model, I have 10 fields and there are 3 fields which are foreign keys. In my JSON data which is received from a GET request, I am getting all the fields but not the foreign keys. I have also done this, but I am still not getting those fields in the JSON data: DataFields = MyData._meta.get_all_field_names() class MyResource(ModelResource): class Meta: queryset = MyData.objects.all() resource_name = 'Myres' serializer = Serializer(formats=['json']) filtering = dict(zip(DataFields, [ALL_WITH_RELATIONS for f in DataFields])) For example, I have the field in model like city , but that

Django : DRF Token based Authentication VS JSON Web Token

和自甴很熟 提交于 2019-12-03 06:38:58
问题 I am building a real world application where users will access the app primarily from Android, iOS devices as well as Desktops. From my elementary research, I have realized that token based authentication mechanism is more better and elegant for client-server models as compared to session based authentication. In Django, I have found two popular ways to do this - http://www.django-rest-framework.org/api-guide/authentication/#tokenauthentication http://getblimp.github.io/django-rest-framework

Tastypie Negation Filter

笑着哭i 提交于 2019-12-03 04:55:21
Is there a negation filter available by default. The idea is that you can do the following in the django ORM: model.objects.filter(field!=value) How can I do that in tastypie if that is even possible. I tried: someapi.com/resource/pk/?field__not=value someapi.com/resource/pk/?field__!=value someapi.com/resource/pk/?field!=value And all of them given me errors. kgr Unfortunately there's not. The problem is that Tastypie's ModelResource class uses the filter() method of the QuerySet only, i.e. it does not use exclude() which should be used for negative filters. There is no filter() field lookup

How to change status of JsonResponse in Django

此生再无相见时 提交于 2019-12-03 04:11:34
My API is returning a JSON object on error but the status code is HTTP 200 : response = JsonResponse({'status': 'false', 'message': message}) return response How can I change the response code to indicate an error? JsonResponse normally returns HTTP 200 , which is the status code for 'OK' . In order to indicate an error, you can add an HTTP status code to JsonResponse as it is a subclass of HttpResponse : response = JsonResponse({'status':'false','message':message}, status=500) Return an actual status JsonResponse(status=404, data={'status':'false','message':message}) To change status code in

Put a custom http header in backbone

孤人 提交于 2019-12-03 03:16:05
I am creating an API with Tastypie and I want to access to the API from Backbone. To send credentials I use an user_id and a api_key. I do this in android and with curl and this work great, but I can set the http header from backbone. In curl I use: curl --dump-header - -H "Accept: application/json" -H "Content-Type: application/json" -H "user_id: 32" -H "api_key: 69950" -X DELETE "http://127.0.0.1:8000/api/v1/deletenote/66/?format=json" and in android java I use: HttpDelete requestDELETE = new HttpDelete(); requestDELETE.setHeader("Content-type", "application/json"); requestDELETE.setHeader(

How to expose a property (virtual field) on a Django Model as a field in a TastyPie ModelResource

爷,独闯天下 提交于 2019-12-03 03:12:02
问题 I have a property in a Django Model that I'd like to expose via a TastyPie ModelResource. My Model is class UserProfile(models.Model): _genderChoices = ((u"M", u"Male"), (u"F", u"Female")) user = Models.OneToOneField(User, editable=False) gender = models.CharField(max_length=2, choices = _genderChoices) def _get_full_name(self): return "%s %s" % (self.user.first_name, self.user.last_name) fullName = property(_get_full_name) My ModelResource is class UserProfileResource(ModelResource): class