I'm currently using Django RestFramework to create APIs that use both GET and POST to retrieve/insert data within my Django application. I currently have a database model called TransactionDateTime that has a field called start_dt (see below), which takes DateTimeField. The challenge is that I'm passing a string in my json POST data structure (see below) and I need to override the create method in order to loop through the JSON structure to convert the string to the appropriate datetime structure. I know the logic to be used to successfully convert string to datetime, because I was able to perform it in the Django shell (see below), but I don't know how to override the create method and write the appropriate code within the create method to make this happen. Please assist. Below is a copy of my view that is successfully returning JSON data structure via GET, with a BIG question within the create method
TransactionDateTime model from models.py
class TransactionDateTime(models.Model):
room = models.ForeignKey(Room, on_delete = models.CASCADE)
start_dt = models.DateTimeField('start_dateTime')
end_dt = models.DateTimeField('end_dateTime', blank=True, null=True)
def __str__(self):
return str(self.start_dt)
Data Structure to be used on POST
[
{
"start_dt": "2015-01-28 03:00:00"
},
{
"start_dt": "2015-01-28 05:30:00"
}
]
Logic to convert string to datetime
from datetime import datetime
my_date = datetime.strptime('2015-01-28 05:30:00', '%Y-%m-%d %H:%M:%S')
Django Mixin and View
class DateTimeMixin(object):
serializer_class = SimpleSerializer4
permission_classes = (permissions.IsAuthenticated,)
class DateTimeViewSet(DateTimeMixin, generics.BulkModelViewSet):
def get_queryset(self):
num = self.kwargs['dt_rm']
num2 = self.kwargs['id']
r1 = Room.objects.get(id = num)
s1 = Schedule.objects.get(pk=num2)
u= self.request.user.pk
usr = User.objects.get(pk=u)
if(s1.user.username == usr.username):
queryset = r1.transactiondatetime_set.all()
return queryset
else: raise Http404("User does not exist")
def get_serializer_context(self):
num = self.kwargs['id']
s1 = Schedule.objects.get(pk=num)
var = s1.user.username
context = super(DateTimeViewSet, self).get_serializer_context()
return {'request' : var}
def created(self, request, *args, **kwargs):
???
I think the proper way to go about it is not to write your own custom create method, but rather to teach your serializer how to accept the date format you use, e.g.
class SimpleSerializer4(something_here):
...
start_dt = serializers.DateTimeField(format=api_settings.DATETIME_FORMAT, input_formats=['%Y-%m-%d %H:%M:%S'])
...
Then later all you have to do is to add CreateModelMixin to you ViewSet and it should work, e.g.
from rest_framework.mixins import CreateModelMixin
...
class DateTimeViewSet(DateTimeMixin, CreateModelMixin, generics.BulkModelViewSet):
...
Docs on DateTimeField here
Docs on extending view sets here
And I found it often very helpful to look how things are done in the rest_framework itself, so here is the link to the source on GitHub
Related
I have 3 models, Run, RunParameter, RunValue:
class Run(models.Model):
start_time = models.DateTimeField(db_index=True)
end_time = models.DateTimeField()
class RunParameter(models.Model):
parameter = models.ForeignKey(Parameter, on_delete=models.CASCADE)
class RunValue(models.Model):
run = models.ForeignKey(Run, on_delete=models.CASCADE)
run_parameter = models.ForeignKey(RunParameter, on_delete=models.CASCADE)
value = models.FloatField(default=0)
class Meta:
unique_together=(('run','run_parameter'),)
A Run can have a RunValue, which is a float value with the value's name coming from RunParameter (which is basically a table containing names), for example:
A RunValue could be AverageTime, or MaximumTemperature
A Run could then have RunValue = RunParameter:AverageTime with value X.
Another Run instance could have RunValue = RunParameter:MaximumTemperature with value Y, etc.
I created an endpoint to query my API, but I only have the RunParameter ID (because of the way you can select which parameter you want to graph), not the RunValue ID directly. I basically show a list of all RunParameter and a list of all Run instances, because if I showed all instances of RunValue the list would be too long and confusing, as instead of seeing "Maximum Temperature" you would see:
"Maximum Temperature for Run X"
"Maximum Temperature for Run Y"
"Maximum Temperature for Run Z", etc. (repeat 50+ times).
My API view looks like this:
class RunValuesDetailAPIView(RetrieveAPIView):
queryset = RunValue.objects.all()
serializer_class = RunValuesDetailSerializer
permission_classes = [IsOwnerOrReadOnly]]
And the serializer for that looks like this:
class RunValuesDetailSerializer(ModelSerializer):
run = SerializerMethodField()
class Meta:
model = RunValue
fields = [
'id',
'run',
'run_parameter',
'value'
]
def get_run(self, obj):
return str(obj.run)
And the URL just in case it's relevant:
url(r'^run-values/(?P<pk>\d+)/$', RunValuesDetailAPIView.as_view(), name='values_list_detail'),
Since I'm new to REST API, so far I've only dealt with having the ID of the model API view I am querying directly, but never an ID of a related field. I'm not sure where to modify my queryset to pass it an ID to get the appropriate model instance from a related field.
At the point I make the API query, I have the Run instance ID and the RunParameter ID. I would need the queryset to be:
run_value = RunValue.objects.get(run=run_id, run_parameter_id=param_id)
While so far I've only ever had to do something like:
run_value = RunValue.objects.get(id=value_id) # I don't have this ID
If I understand correctly, you're trying to get an instance of RunValue with only the Run id and the RunParameter id, i.e. query based on related fields.
The queryset can be achieved with the following:
run_value = RunValue.objects.get(
run__id=run_id,
run_parameter__id=run_parameter_id
)
Providing that a RunValue instance only ever has 1 related Run and RunParameter, this will return the instance of RunValue you're after.
Let me know if that's not what you mean.
The double underscore allows you to access those related instance fields in your query.
Well its pretty simple, all you have to do is override the get_object method, for example(copy pasted from documentation):
# view
from django.shortcuts import get_object_or_404
class RunValuesDetailAPIView(RetrieveAPIView):
queryset = RunValue.objects.all()
serializer_class = RunValuesDetailSerializer
permission_classes = [IsOwnerOrReadOnly]]
lookup_fields = ["run_id", "run_parameter_id"]
def get_object(self):
queryset = self.get_queryset() # Get the base queryset
queryset = self.filter_queryset(queryset) # Apply any filter backends
filter = {}
for field in self.lookup_fields:
if self.kwargs[field]: # Ignore empty fields.
filter[field] = self.kwargs[field]
obj = get_object_or_404(queryset, **filter) # Lookup the object
self.check_object_permissions(self.request, obj)
return obj
# url
url(r'^run-values/(?P<run_id>\d+)/(?P<run_parameter_id>\d+)/$', RunValuesDetailAPIView.as_view(), name='values_list_detail'),
But one big thing you need to be careful, is not to have duplicate entries with same run_id and run_parameter_id, then it will throw errors. To avoid it, either use unique_together=['run', 'run_parameter'] or you can use queryset.filter(**filter).first() instead of get_object_or_404 in the view. But second option will produce wrong results when duplicate entries are created.
I have a django model
class UserInfluencerGroupList(models.Model):
list_name = models.CharField(max_length=255)
influencers = models.ManyToManyField(Influencer, blank=True)
user = models.ForeignKey(MyUser, on_delete = models.CASCADE)
def __str__(self):
return self.list_name
and my views function is:
def get_lists(request,user_email):
"""Get all the lists made by user"""
try:
user_instance = MyUser.objects.get(email=user_email)
except MyUser.DoesNotExist:
return HttpResponse(json.dumps({'message':'User not found'}),status=404)
if request.method == 'GET':
influencers_list = UserInfluencerGroupList.objects.all().order_by('id').filter(user=user_instance)
influencers_list = serializers.serialize('json',influencers_list, fields =['id','influencers','list_name'], indent=2, use_natural_foreign_keys=True, use_natural_primary_keys=True)
return HttpResponse(influencers_list,content_type='application/json',status=200)
else:
return HttpResponse(json.dumps({'message':'No lists found'}), status=400)
Apart from the usual data from list I also want to calculate the total_followers, total_likes and total_comments of each influencer in the list. The influencer model has fields for total_likes, comments and followers.
How should I write a function to calculate and display it along with all the other data that the list is returning
You should consider to use Django Rest Framework if you want to return a json of your own choice or/and if you're about to create your own rest api.
Alternative is to create the json all manually, i.e build the dictionary and then use json.dumps.
(If you really want to go "manual" see answer Convert Django Model to dict)
The django serializers does not support what you want to do:
option for serializing model properties (won't fix)
Quote for not fixing:
"I'm afraid I don't see the benefit of what you are proposing. The
serialization framework exists for the easy serialization of Django
DB-backed objects - not for the arbitrary serialization of _any_
object, and derived properties like the ones you are highlighting as
examples don't add anything to the serialized representation of a
DB-backed object..."
I trying to execute a periodic task, so I used celery with Django 1.8 and Django Rest Framework and Postgres as Database. When I try to send my obj to the task I get TypeError: foreign_model_obj is not JSON serializable. How can I pass my queryset object to my Task.
views.py :
class MyModelCreateApiView(generics.CreateAPIView):
queryset = MyModel.objects.all()
serializer_class = MyModelSerializer
authentication_classes = (TokenAuthentication,)
def create(self, request, *args, **kwargs):
data = dict()
data['foreign_model_id'] = kwargs['pk']
foreign_model_obj = MyForeignModel.objects.get(id=data['foreign_model_id'])
obj = MyModel.objects.create(**data)
result = serialize_query(MyModel, {"id": obj.id})
local_time = foreign_model_obj.time
my_celery_task.apply_async([foreign_model_obj], eta=local_time)
return Response(result)
tasks.py :
#celery_app.task(name="my_celery_task")
def my_first_celery_task(mymodel_obj):
# ... updating obj attributes
mymodel_obj.save()
You have just to send the id of your instance and retrieve the object within the task.
It's a bad practice to pass the instance, since it can be altered in meantime, specially that you are excuting your task with a deplay as it seems to be.
views.py :
class MyModelCreateApiView(generics.CreateAPIView):
queryset = MyModel.objects.all()
serializer_class = MyModelSerializer
authentication_classes = (TokenAuthentication,)
def create(self, request, *args, **kwargs):
data = dict()
data['foreign_model_id'] = kwargs['pk']
foreign_model_obj = MyForeignModel.objects.get(id=data['foreign_model_id'])
obj = MyModel.objects.create(**data)
result = serialize_query(MyModel, {"id": obj.id})
local_time = foreign_model_obj.time
my_celery_task.apply_async([foreign_model_obj.id], eta=local_time) # send only the obj id
return Response(result)
tasks.py :
#celery_app.task(name="my_celery_task")
def my_celery_task(mymodel_obj_id):
my_model_obj = MyModel.objects.get(id=mymodel_obj_id) # retrieve your object here
# ... updating obj attributes
mymodel_obj.save()
Actually, IMHO the best way to go is to get a picklable component of the queryset, then regenerate the queryset in the task (https://docs.djangoproject.com/en/1.9/ref/models/querysets/):
import pickle
query = pickle.loads(s) # Assuming 's' is the pickled string.
qs = MyModel.objects.filter(a__in=[1,2,3]) # whatever you want here...
querystr = pickle.dumps(qs.query) # pickle the queryset
my_celery_task.apply_async(querystr, eta=local_time) # send only the string...
The task:
#celery_app.task(name="my_celery_task")
def my_celery_task(querystr):
my_model_objs = MyModel.objects.all()
my_model_objs.query = pickle.loads(querystr) # Restore the queryset
# ... updating obj attributes
item=my_model_objs[0]
This is the best approach, I think, because the query will get executed (perhaps the first time) in the task, preventing various timing issues, it need not be executed in the caller (so no doubling up on the query).
You could change method of serialization to pickle, but it is not recommended to pass queryset as a parameter. Quote from Celery documentation:
Another gotcha is Django model objects. They shouldn’t be passed on as arguments to tasks. It’s almost always better to re-fetch the object from the database when the task is running instead, as using old data may lead to race conditions.
http://docs.celeryproject.org/en/latest/userguide/tasks.html
So, after reading the Django REST Framework document, and a bunch of tutorials, I am still having trouble understanding how to use the Django serializers to convert incoming POST (JSON) data into a Python object (sorry, I'm new).
Given that I am posting a JSON string to, say, api/foo/bar, how do I write its serializer?
Example JSON:
{ 'name': 'Mr. Foo', address:'Bar Street' }
My controller, Foo contains a bar method as follows:
#detail_route(
methods=['post']
)
def bar(self, request, uuid=None):
serializer = MySampleSerializer(data=request.DATA)
something.clone(serializer.object)
return Response(status=status.HTTP_201_CREATED)
Can somebody explain to me what should my serializer look like? And how do I access the serialized data from the serializer?
As you do not want to use a model, you have to create the serializer from scratch. Something like this should maybe work:
class MySerializer(serializers.Serializer):
name = serializers.CharField(max_length = 100)
adress = serializers.CharField(max_length = 100)
And then you could use it in a request like this:
def bar(self, request, uuid=None):
data = JSONParser().parse(request)
serializer = MySerializer(data = data)
return Response(status=status.HTTP_201_CREATED)
Note however, as you have not created an Django model, you will not be able to save the serialized data (and thus nothing will be saved in the database)
Basically, you pass in the JSON data to the serializer, and then access the data field which will return an ordered dictionary.
def bar(self, request, uuid=None):
serializer = MySampleSerializer(data=request.data)
if serializer.is_valid(raise_exception=True):
my_object = serializer.data # Grab the dict of values
To define a serializer:
class MySampleSerializer(serializers.Serializer):
name = serializers.CharField(max_length=30)
address = serializers.CharField(max_length=30)
You do not have to use the ModelSerializer:
from rest_framework import serializers
class CommentSerializer(serializers.Serializer):
email = serializers.EmailField()
content = serializers.CharField(max_length=200)
created = serializers.DateTimeField()
and access:
serializer = CommentSerializer(data=data)
serializer.is_valid()
# True
serializer.validated_data
by the way, all the above is from the DRF website
I have two tabels(Ingredient_Step and Ingredient) in on relation as you can see below:
Models.Py
class Ingredient_Step(models.Model):
ingredient = models.ForeignKey(Ingredient)
Step = models.ForeignKey(Step)
def __unicode__(self):
return u'{}'.format(self.Step)
class Ingredient(models.Model):
IngredientName = models.CharField(max_length=200,unique=True)
Picture = models.ImageField(upload_to='Ingredient')
def __unicode__(self):
return u'{}'.format(self.IngredientName)
In a function, i need serialize a JSON object from a query that returns from "Ingredient_step", but I need send the field "IngredientName", who comes from "Ingredient" table.
I try using "ingredient__IngredientName" but it fails.
Views.Py:
def IngredientByStep(request):
if request.is_ajax() and request.GET and 'id_Step' in request.GET:
if request.GET["id_Step"] != '':
IngStp = Ingredient_Step.objects.filter(Step =request.GET["id_Step"])
return JSONResponse(serializers.serialize('json', IngStp, fields=('pk','ingredient__IngredientName')))
How i can call extends field from a relation?
Thanks
This "feature" of Django (and many ORM's like SQLAlchemy) are called Lazy Loading, meaning data is only loaded from related models if you specifically ask for them. In this case, build your IngStp as a list of results, and make sure to access the property for each result before serializing.
Here's an example of how to do that: Django: Include related models in JSON string?