FAQ Database Discussion Community


Tastypie DB level Field Selection

python,django,api,orm,tastypie
Given PostgreSQL 9.2.10, Django 1.8, python 2.7.5 and django-tastypie 0.12.1: I am trying to figure out how to do a dynamic queryset for tastypie based off of a custom param (field selection) a end user may filter for via something like this: https://example.com/api/v1/resource/?fields=field1 (list view) https://example.com/api/v1/resource/1/?fields=field1 (detail view) def dehydrate()...

Tastypie Accessing Query Before Data Gets Replaced by Request

python,django,tastypie
I am using a PATCH request in tastypie to update a field in my model but when this PATCH goes through I am trying to append the data that is already in the database rather than replacing the value entirely. Example: In a blog post, a user flags a post...

Django tastypie: how to retrieve a list of child objects and attach that list to the 'choices' field atrribute

django,django-models,tastypie
I have a model class named Blueprint: class Blueprint(models.Model): name = models.CharField(max_length=120) description = models.TextField() workloads = models.CharField(choices=WORKLOAD_CHOICES) class Meta: ordering = ["name", ] This model has children named workloads. Being totally new to both, django and tastypie, I have this question: 1) Where do I execute the logic, which...

django-tastypie how to assign default value in id

python,django,tastypie
tastypie i want to assign id with calling any other function in api.py class UserResource(ModelResource): class UserResource(ModelResource): class Meta: collection_name="data" queryset = User.objects.all() resource_name = 'user' authorization = Authorization() def dehydrate(self, bundle): bundle.data['user_id'] = "1" # want to call one function here return bundle but that is not working and...

Django Tastypie: Filtering by ForeignKey

python,django,tastypie
I would like Django Tastypie to return a query filtered by a ForeignKey. Here are my two models: class Origin(models.Model): country = models.CharField(max_length=1024) class Fruits(models.Model): origin = models.ForeignKey(Origin) fruit_name = models.CharField(max_length=1024) is_sweet = models.BooleanField() quantity = models.IntegerField() Based on the documentation here, I'm using the following resources.py: class FruitResource(ModelResource): class...

TastyPie apply authentication only to PUT/POST

tastypie
How do apply authentication & authorization to PUT and POST methods in TastyPie? I know we can define authentication and authorization in Meta inner class on the resource but how do i let these two methods ONLY to pass thru authentication and authorization layer. I interested in SessionAuthentication and DjangoAuthrization...

Django tastpie, how do i add/save-to two models with just once create post call

django,django-models,tastypie
I am trying to provide a restful api from django backend to an android app using tastypie. My sample db model is: class Device(models.Model): userprofile = models.ForeignKey(UserProfile, null=True) device_id = models.CharField(max_length=512) os = models.CharField(max_length=128, null=True) manufacturer = models.CharField(max_length=128, null=True) registered_on = models.DateTimeField(default=datetime.now) class Meta: ordering = ['registered_on'] verbose_name_plural = 'Devices'...

Processing data before saving to database Django Tastypie

django,timestamp,tastypie,data-type-conversion
I send a string to the server and I want to convert it to a Timestamp before saving in the database. I want to do this function before saving the data. date = time.strptime(date, "%Y-%m-%d %H:%M:%S") date is sent as a string and I want to convert it to a...

django tastypie caching is not working for list data

django,python-2.7,caching,tastypie
i am using python 2.7,django=1.5 and tastypie v0.12.1 when i am hit 1 recourse data is then caching is happing class BranchResource(ModelResource): class Meta: collection_name="data" queryset = Branch.objects.all() resource_name = 'branch' authorization = Authorization() limit = 0 #(unlimted) filtering = { "branch_flag": ALL, "branch_name":('exact', 'startswith','istartswith',), "cid":ALL, } cache = SimpleCache(timeout=1000)...

Backbone model save() with Django Tastypie

django,backbone.js,tastypie,backbone-relational
I'm working on some code that relies on Backbone, Backbone-relational and Tastypie working together. (If it matters, I've included the backbone-tastypie library). I have a Basket model that stores some properties about itself, and can HaveMany Fruit models: var Fruit = Backbone.RelationalModel.extend({ urlRoot: '/api/v1/fruit/', }); var Basket = Backbone.RelationalModel.extend({ urlRoot:...

Create timestamp field on getting a POST request when using Django-tastypie

django,tastypie
I have a timestamp field in one of my Django models (let's say Comment). I have been using Django-tastypie for REST API. Now when a POST request is submitted to /api/v1/comments/, a new comment object is created but how do I populated the timestamp field in this comment. I want...

Get logged in user and value of another model - Tastypie

python,django,tastypie
Models: class Applicant_Skill(models.Model): user = models.ForeignKey(User) #applicant = models.ForeignKey(Applicant) skill = models.ForeignKey('skills_layer.Skill') active = models.BooleanField(default=True) class Job_Posting(models.Model): company = models.ForeignKey('companies_layer.Company', default=-1) job_posted_by = models.ForeignKey(User, default=-1) job_title = models.CharField(max_length=100) job_summary = HTMLField(blank=True) job_details = HTMLField(blank=True) no_of_openings = models.IntegerField(default=0) tags =...

How to write helper method for PATCH request using django-tastypie?

python,django,rest,tastypie
I have a Comment object which has a text field. For editing it's text field, we can send a PATCH request (based on REST principles). I am using django-tastypie for REST API. Now I want to keep history of this text field, so that original text is not deleted but...

Getting Attrbute error _exit_ when use “with” keyword

python,django,tastypie
I had passed .csv file to post request in api, input_file = data.get('file', None) with input_file as datasheet: header = datasheet.readline() ............... I got error on second line. my file type is Unicode so it again giving error on third line for readline()...

Trying to Import TastyPie API but I get Page Not Found Message

python,django,tastypie
I am trying to create a Django website and more importantly, import the TastyPie API. But every time I run /articles/api/article on my localhost, I get this error message: Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/articles/api/article Using the URLconf defined in django_test.urls, Django tried these URL patterns,...

tastypie obj_create and authorization

python,django,authorization,tastypie
I use tastypie 0.12.2-dev to create API for my django site. I wrote a class authorization (ApprovedLaptopsAuthorization) and used it in my ModelResource (RecordResource) class. The endpoint of RecordResource is http://myserver/book/api/record. HTTP GET Request to that endpoint is working correctly. (permissions are checked in read_list() method of ApprovedLaptopsAuthorization class). Now...

How to deal with many-to-many in django / tastypie

django,django-models,tastypie
I have the following two models, that should have many-to-many between them: class Blueprint(models.Model): name = models.CharField(max_length=120) description = models.TextField() class Meta: ordering = ["name", ] class Workload(models.Model): blueprints = models.ManyToManyField('Blueprint', db_constraint=False) name = models.CharField(max_length=120) description = models.TextField() image = models.CharField(max_length=120) flavor = models.CharField(max_length=120) class Meta: ordering = ["name", ]...

Django tastypie reverse relation not working

python,django,tastypie
I have following CustomerProfile models that OneToOne maps to the User table : class CustomerProfile(models.Model): '''Profile details of the customers. All extra details are mentioned here.''' user = models.OneToOneField(User, related_name='profile') phone = models.CharField(max_length=200, null=True, blank=True) class Meta: app_label = 'testapp' def __unicode__(self): return unicode(self.user) I have created REST API using...

How to get TastyPie fields.foreignkey to only return certain fields as apposed to resource uri or full object

django,tastypie
I have a TastyPie resource setup to return all Quotes from the database. Each quote has a foreign key relationship to a Django user. class QuoteResource(ModelResource): user = fields.ForeignKey(UserResource, 'user') class Meta: queryset = Quote.objects.all().order_by('-created') limit = 0 max_limit = 0 resource_name = 'quotesreport' allowed_methods = ['get'] authentication = BasicAuthentication()...