Nest model serializers in Django without using rest framework - django

I would like to know if it's possible to nest serializers without using the rest framework.
I was reading this question and I would like to do something similar but I am not allowed to use the rest framework.
Is it possible to serialize models with foreign keys using a similar approach (nesting) without using the rest framework?
At the moment I am serializing json like this:
data = serialize("json", myModelObject, fields=('id', 'foreignKeyField'), cls=DatetimeJSONEncoder)
And I would like to do something like this:
data = serialize("json", myModelObject, fields=('id', 'foreignKeyField.some_value'), cls=DatetimeJSONEncoder)

first make sure your view inherit from JSONResponseMixin
class JSONResponseMixin(object):
def render_to_json_response(self, context, **response_kwargs):
return JsonResponse(self.get_data(context), **response_kwargs)
def get_data(self, context):
return context
how about this :
Broaden your query values to include foreignKeyField.some_value
convert the queryset to list
return the json list as response for your ajax request
access your response as json array in ajax success
results = MyModel.objects.filte(my_filter).values("field_1",
"field_2",
"field_n",
"foreignKeyfield__field_11",
"foreignKeyfield__field_22")
results_list = json.dumps(results)
return JsonResponse(results_list)
it depends on your frontend technologie , try to output the reponse and parse with joy
let me know in the comments section if you need more details

Related

How to create a custom GET method that matches a certain URL pattern?

I am using a DefaultRouter and have a custom method in it. Right now I am passing values as POST but I want to pass as GET with the pattern like example.com/wallets/balance/<customerID>/. I am using ViewSet.
My current urls.py looks like:
router = routers.DefaultRouter()
router.register('wallets', views.WalletView, basename='WalletModel')
router.register('wallets/balance/1/', views.custom_balance,basename='CustomBalanceModel') # This crashes
and models.py
def custom_balance(id):
return Response({'status': 'OK', 'data': 'success'}, status=status.HTTP_200_OK)
class WalletView(viewsets.ModelViewSet):
.....
When I did this I used a .js file to do the get and post
Something like this to get all data. Then I passed it to the template using AngularJS.
You could do this in an AJAX .get call in the tag inside the template as well if you're not using AngularJS or VueJS for example.
function HeatTreatController($scope, $http) {
$scope.Foodata =[];
var q
$http.get('/wallets/balance/').then(function(response){
$scope.Foodata = response.data;
$scope.Fooworkorder = response.data.workOrder;
});
}
Foodata would then be everything sent by your serializer through the viewset.
workorder is just the workOrder value (models.py workOrder field) accessed by the dot operator. Just like when you have a QuerySet and you want to get sub fields of your model.
wallets/balance has to be an accesible API URL.
In your Django app you should be able to go to this URL and see the data.
Now if you want to get 1 specific piece of data from a GET
function FooController($scope, $http) {
$scope.Foodata =[];
var q
$http.get('/wallets/balance/'+customer.id +'/').then(function(response){
$scope.Foodata = response.data;
$scope.workorder = response.data.workOrder;
});
}
If I am understanding your question correctly it would not be strictly necessary to separate URL like router.register('wallets/balance/1/', views.custom_balance,basename='CustomBalanceModel').
You can GET and POST through router.register('wallets', views.WalletView, basename='WalletModel')
Here is some more Django documentation on ViewSets
So in this cast if you're already doing a POST request somehow in your code you can instead do a GET request in the same fashion.
If you have the time to learn, I learned all this from Pluralsight:
https://www.pluralsight.com/courses/django-angularjs-web-development
If you have a day or two you can learn it all.
I also recommend this Max Goodridge video. They're all pretty good

Django Rest Framework: Without Model

I'm working on creating a RESTAPI using DRF(Django Rest Framework). API just receives the users twitter handle and returns his twitter data.
Here, I'm not using model here because it's not required.
Should I use a serializer here? If so why? Now I'm able to return the data without using a serializer.
Moreover, My API is not web-browsable. How should I make it web-browsable: which is one of the best features of DRF.
Edit:1
I'm using Functions in Views.
#api_view(['GET'])
#csrf_exempt
def getdetails(request):
call the twitter api
receive the data
return HttpResponse(JsonResponse( {'data': {'twitter_id':id,'tweets':count,'Followers':followers,'following':count_follow}}))
In the browser I'm just seeing JSON data like this.
{"data": {"twitter_id": 352525, "tweets": 121, "Followers": 1008, "following": 281}}
You can use Serializer for the result
class SampleSerializer(serializers.Serializer):
field1 = serializers.CharField()
field2 = serializers.IntegerField()
# ... other fields
Usage
my_data = {
"field1": "my sample",
"field2": 123456
}
my_serializer = SampleSerializer(data=my_data)
my_serializer.is_valid(True)
data = my_serializer.data
You'll get serialized data in data variable (you can use my_serializer.data directly)
Should I use a serializer here?
It's up to you, because if you wish to show the response JSON without any modification from the Twitter API, you can go without DRF serializer. And If you wish to do some formatting on the JSON, my answer will help you
My API is not web-browsable. How should I make it web-browsable?
Maybe you followed wrong procedure. Anyway we can't say more on this thing without seeing your code snippets
Update-1
from rest_framework.response import Response
#api_view(['GET'])
#csrf_exempt
def getdetails(request):
call the twitter api
twitter_api = get_response_from_twitter() # Json response
return Response(data=twitter_api)

Using Django Rest Framework, is it possible to get results internally on server within Django?

I have a complex DRF ViewSet that supports paging, filtering, sorting, etc. that backends a grid view. To build an "export" capability, I need to be able to take the same querystring that my endpoint uses, such as:
?obj_id=129&ordering=latitude&city__icontains=nap
And be able to, in Django, send that string into DRF somehow and get the fully-modified queryset after all the view's filters, sorts, etc are applied (the same way as the GET did). I could use the fully-rendered json response or some interim filter-applied queryset. Is it possible to use DRF in this way?
You should write a CSV renderer for your viewset and get that content-type to export the CSV.
There's even one already available.
Yes you could, if you already have a request object i.e. If you want to use this DRF viewset into another view which has the request object:
def another_view(request):
# make a copy of the `GET` attribute of request object
request.GET = request.GET.copy()
# now you can set the query params on this GET object
# ?obj_id=129&ordering=latitude&city__icontains=nap
request.GET['obj_id'] = 129
request.GET['ordering'] = 'latitude'
request.GET['city__icontains'] = 'nap'
# you can also set paging options in similar way
# now instantiate the viewset
vs = DRFViewset.as_view()
# call the view for response
# set kwargs as you need
response = vs(request, *args, **kwargs)
# response.data will have what you want here

Post data as an array in Django Rest Framework

I'm implementing an API using Django Rest framework. I wonder Python can send POST params as an array like Ruby does?
For example:
POST /api/end_point/
params = { 'user_id': [1,2,3] }
# In controller, we get an array of user_id:
user_ids = params[:user_id] # [1,2,3]
There are a number of ways to deal with this in django-rest-framework, depending on what you are actually trying to do.
If you are planning on passing this data through POST data then you should use a Serializer. Using a serializer and django-rest-frameworks Serializer you can provide the POST data through json or through a form.
Serializer documentation: http://www.django-rest-framework.org/api-guide/serializers/
Serializer Field documentation: http://www.django-rest-framework.org/api-guide/fields/
Specifically you will want to look at the ListField.
It's not tested, but you will want something along the lines of:
from rest_framework import serializers
from rest_framework.decorators import api_view
class ItemSerializer(serializers.Serializer):
"""Your Custom Serializer"""
# Gets a list of Integers
user_ids = serializers.ListField(child=serializers.IntegerField())
#api_view(['POST'])
def item_list(request):
item_serializer = ItemSerializer(data=request.data)
item_serializer.is_valid(raise_exception=True)
user_ids = item_serializer.data['user_ids']
# etc ...

Django: return query result as api

I'm trying to build an API server using Django. I have a few tables and I need to run queries based on the parameters passed in by url:
http://server.com/api/request/p1=123&p2=321...
and the server would extract p1 and p2 from url and run queries using them, and then return the result in json or xml.
I have tried Tastypie, and it's very easy to set up for retrieving data from one model. But my case is a bit complicated than that, and sometimes I need to run spatial queries. So if I could run the query and return result as json/xml, it would be great!
Very new to the backend technology, any help for a start point is appreciated!
Thanks!
[Edit]
Just want to make my case clearer. Say I ran a raw query using qs = cursor.execute(sql), etc., and I want to return that result as json/xml to a api call. Can I do that with either Tastypie or Rest Framework? Or can I do it without any help from 3rd party framework?
Here's a view I use return json, you should be able to adapt it pretty easy:
import json
from django.http import HttpResponse
from django.template.defaultfilters import slugify
from .models import *
def response_times(request):
response_data = {} #Create an empty dictionary
websites = Website.objects.all() #Query your models
for site in websites:
key = slugify(site.name)
response_data[key] = {}
history = History.objects.filter(website=site)[:60]
response_data[key]['response_times'] = []
for response in history:
response_data[key]['response_times'].append({'time': str(response.time), 'timestamp': response.added.strftime('%s')})
return HttpResponse(json.dumps(response_data), content_type="application/json")
It seems you have in the result QuerySet of some Model instances. So you could specify serializer to work with your model and use it to serialize from model instances to native python data using Rest Framework. Look here for details.