DRF queryset to return specific field - python

I'm creating a django rest framework application with this structure (assuming imports are correct, so I omit them from the code below.
models.py:
class Door(models.Model):
type = models.CharField(max_length=40)
color = models.CharField(max_length=40)
serializers.py:
class DoorSerializer(serializers.ModelSerializer):
class Meta:
model = Door
fields = ['type', 'color']
views.py:
class DoorViewSet(viewsets.ModelViewSet):
serializer_class = DoorSerializer
queryset = Door.objects.all()
def get_queryset(self, *args, **kwargs):
queryset = Door.objects.all()
parameter = self.request.query_params.get('type', '')
if parameter:
return queryset.filter(type=parameter)
else:
return queryset
So far this behaves as intended, when I make an api call to localhost/Doors it lists all the doors. And when I make an api call to localhost/Doors/?type=big it lists all the doors that have the value "big" in their "type" field.
The addition I would like to make is another parameter check which would return a list of all the unique door types that exist in the database. This can be achieved in the manage.py shell by using: Door.objects.all().values('type').distinct()
My attempt was the following modifications to views.py:
...
parameter = self.request.query.params.get('type', '')
unique = self.request.query.params.get('unique', '')
if parameter:
...
elif unique:
return Door.objects.all().values('type').distinct()
...
My assumption was that this would return the same as Door.objects.all().values('type').distinct() when I make a call to localhost/Doors/?unique=whatever
However I am getting the error: "Got KeyError when attempting to get a value for field color on serializer DoorSerializer.\nThe serializer field might be named incorrectly and not match any attribute or key on the dict instance.\nOriginal exception text was: 'color'."
I assume this means that the serializer expects an object or a list of objects that contains all the fields of the corresponding model.
Is there some way I could circumvent this by fixing the view or should I create a different serializer? In either case, since I've gotten pretty confused with DRF / django differences and it is possible I won't be able to follow abstract instructions, could you provide a code solution that addresses the issue? Also, in the very likely case that my assumption is completely off, could you also explain what is causing the problem? Thank you for your time!
Edit for clarifying the desired result:
Assuming my database has 4 doors which are:
{
"id": 1,
"type": "big",
"color": "blue"
},
{
"id": 2,
"type": "big",
"color": "yellow"
},
{
"id": 3,
"type": "small",
"color": "green"
},
{
"id": 4,
"type": "big",
"color": "red"
},
I would like to make a get request to some url, for instance localhost/Doors/?unique=Yes and have the api return to me the list {"big", "small}

WRITING YOUR OWN VIEW: Short view that returns the list of type. You need to set up a new path here. I'd personally go for this option as the response you expect is way different to what the rest of your view does.
from rest_framework.decorators import api_view
from rest_framework.response import Response
#api_view()
def Unique_door_types(request):
types = Door.objects.values_list('type', flat=True).distinct()
return Response({"types": list(types)})
WITHOUT AN ADDITIONAL VIEW:
No need for additional view or serializer. Override the list method. Note that this is closer to a trick than to a good way of programming.
from rest_framework.response import Response
class DoorViewSet(viewsets.ModelViewSet):
serializer_class = DoorSerializer
def get_queryset(self, *args, **kwargs):
queryset = Door.objects.all()
parameter = self.request.query_params.get('type', '')
if parameter:
return queryset.filter(type=parameter)
else:
return queryset
def list(self, request):
unique = self.request.query_params.get('unique', '')
if unique:
types = Door.objects.values_list('type', flat=True).distinct()
return Response({"types": list(types)})
return super().list()

My suggestion would be to create a separate route like /doors/types/. You do this by adding a method to your DoorViewSet class with a #action decorator. See https://www.django-rest-framework.org/api-guide/viewsets/#marking-extra-actions-for-routing for more details about how to do this.

Related

Best architecture for dynamically validating and saving field

I am looking for the good architecture for my problem. I am using django rest framework for building an API. I receive a list of dict which contains an id and a list of values. The list of values need to be validated according to the id.
Example of my code:
class AttributesSerializer(serializers.Serializer):
id = serializers.PrimaryKeyRelatedField(queryset=Attribute.objects.all(), source="attribute", required=True)
values = serializers.ListField()
def validate(self, validated_data):
attribute = validated_data["attribute"]
values = validated_data["values"]
# This function returns the corresponding field according to attribute
values_child_field = get_values_field(attribute)
self.fields["values"].child = values_child_fields
new_values = self.fields["values"].run_child_validation(values)
set_value(validated_data, "values", new_values)
return validated_data
class BaseObjectApiInputSerializer(serializers.Serializer):
category_id = serializers.PrimaryKeyRelatedField(
queryset=Category.objects.all()
)
attributes = AttributesSerializer(many=True)
I want to parse json like this:
{
"categorty_id": 42, # Category pk of the baseobject. which defines some constraints about attributes available
"attributes": [
{"id": 124, "values": ["value"]},
{"id": 321, "values": [42]},
{
"id": 18,
"values": [
{
"location": {"type": "Point", "geometry": {...}},
"address": "an address",
}
],
},
]
}
Currently, this code does not work. DRF seems to try to revalidate all values entries for each iteration with each child field. I do not understand why... I guess I could make it work without using this fields["values"] for making the validation and just retrieve the field and use it directly, but i need this field for making the save later.
Do you think my architecture is ok? What is the good way for parsing this type of data with DRF?
EDIT:
Structure of models are complex but a version simplified following:
class Attribute(models.Model):
class DataType(models.TextChoices):
TEXT = "TEXT", _("datatype_text")
INTEGER = "INTEGER", _("datatype_integer")
DATETIME = "DATETIME", _("datatype_datetime")
BOOL = "BOOL", _("datatype_bool")
# Some examples, but there are about 30 items with
# type very complicated like RecurrenceRule (RFC2445)
# or GeoJSON type
label = models.CharField()
category = models.ForeignKey(Category)
attribute_type = models.CharField(choices=DataType.choices)
class AttributeValue(models.Model):
attribute = models.ForeignKey(Attribute)
# a model which represents an object with list of attributes
baseobject = models.ForeignKey(BaseObject)
value = models.TextField()
AttributeValue is like a through table for manytomany relation between BaseObject model and Attribute model.
My JSON represents the list of attribute/values attached to a baseobject.
In fact I don't understand why DRf doesn't allow delegating registration in the child serializers of the parent serializer. This would allow much greater flexibility in code architecture and separation of responsibilities.
EDIT 2 :
My urls.py
router = routers.DefaultRouter()
router.register("baseobjects", BaseObjectViewSet, basename="baseobjects")
I am using the default router and url for DRF viewset.
The view looks like:
class BaseObjectViewSet(viewsets.ModelViewSet):
permission_classes = [IsAuthenticated]
authentication_classes = [TokenAuthentication]
def create(self, request, *args, **kwargs):
serializer = BaseObjectApiInputSerializer(
data=request.data
)
if not serializer.is_valid():
return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)
baseobject: BaseObject = serializer.save()
return Response(
{"results": [{"id": baseobject.pk}]}, status=HTTP_200_OK
)
I think you should use ListField with JSONField as child argument for values field.
validators = {
TinyurlShortener.DataType.TEXT: serializers.CharField(),
TinyurlShortener.DataType.INTEGER: serializers.IntegerField(),
TinyurlShortener.DataType.DATETIME: serializers.DateTimeField(),
TinyurlShortener.DataType.BOOL: serializers.BooleanField(),
}
class AttributesSerializer(serializers.Serializer):
id = serializers.PrimaryKeyRelatedField(queryset=Attribute.objects.all(), source="attribute", required=True)
values = serializers.ListField(
child=serializers.JSONField()
)
def validate(self, attrs):
attribute = attrs.get('id')
field = validators[attribute.attribute_type]
for v in attrs['values']:
field.run_validation(json.loads(v.replace("'", '"')))
return super().validate(attrs)
class BaseObjectApiInputSerializer(serializers.Serializer):
category_id = serializers.PrimaryKeyRelatedField(
queryset=Category.objects.all()
)
attributes = AttributesSerializer(many=True)

Sending a Python Dictionary Through a Single POST method in Django REST Framework

I have been attempting to implement this method several ways in which seems like it should be pretty straightforward. I have a web scraper that scrapes stock data and stores it into a Python dictionary in a python script. This then makes a post request to my Django API which is handled under my views using GenericAPIView. This post request sends a QueryDict and I am able to post a single instance but not multiple at once.
Yes, I have set many=True on my serializer and attempted to override these methods. Each solution I have made is inefficient and I am frustrated not finding a clear and simple solution. This is my first time working with the Django Rest Framework so bear with me and thanks for your help in advance!
CryptoView (Mostly followed a tutorial on Medium.com to improve my method)
class CryptoView(ListCreateAPIView):
queryset = Crypto.objects.all()
serializer_class = CryptoSerializer
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
class SingleCryptoView(RetrieveUpdateDestroyAPIView):
queryset = Crypto.objects.all()
serializer_class = CryptoSerializer
Cryptotest.py
crypto = []
# For Loop - Yahoo Finance requires us to crawl through specific
# attributes to find data
for listing in soup.find_all('tr', attrs={'class':'simpTblRow'}):
listing_dict = {}
for name in listing.find_all('td', attrs={'aria-label':'Name'}):
listing_dict["name"] = name.text
for price in listing.find_all('td', attrs={'aria-label':'Price (Intraday)'}):
listing_dict["price"] = price.text.replace(',', '')
for change in listing.find_all('td', attrs={'aria-label':'Change'}):
listing_dict["change"] = change.text
for percentChange in listing.find_all('td', attrs={'aria-label':'% Change'}):
listing_dict["percentChange"] = percentChange.text.replace('%', '')
index += 1
crypto.append(listing_dict)
headers = {'Content-type': 'application/json'}
requests.post(url=API_ENDPOINT, json=json.dumps(crypto), headers=headers)
CryptoSerializer (Was testing with the init accepting many)
class CryptoSerializer(serializers.ModelSerializer):
def __init__(self, *args, **kwargs):
many = kwargs.pop('many', True)
super(CryptoSerializer, self).__init__(many=many, *args, **kwargs)
class Meta:
model = Crypto
fields = ('name', 'price', 'change', 'percentChange')
def create(self, validated_data):
return Crypto.objects.create(**validated_data)
def update(self, instance, validated_data):
instance.name = validated_data.get('name', instance.name)
instance.price = validated_data.get('price', instance.price)
instance.change = validated_data.get('change', instance.change)
instance.volume = validated_data.get('percentChange', instance.volume)
instance.save()
return instance
UPDATE:
Able to send the data as a JSON array to the post method. Now getting the error of Bad Request from the serializer which fails on the serializer.is_valid() check. If I remove the raise_exception=True then my create() gets no data passed in. This is how my JSON array is formatted if it is correct I don't know: [{"id": 0, "name": "Bitcoin USD", "price": "5314.87", "change": "+54.02", "percentChange": "+1.03"},
Thanks again for the help it is much appreciated as I am excited to keep improving this project!
UPDATE (2):
As requested, here is the exact JSON data passed to the serializer with the create() method. This is not all of the data but it is the exact format [{"id": 107, "name": "FirstCoin USD", "price": "0.0017", "change": "-0.0012", "percentChange": "-41.52"}, {"id": 108, "name": "MCAP USD", "price": "0.0046", "change": "0.0000", "percentChange": "0.00"}, {"id": 109, "name": "ATBCoin USD", "price": "0.0011", "change": "+0.0002", "percentChange": "+17.10"}, {"id": 110, "name": "Exchange Union USD", "price": "0.6857", "change": "+0.0026", "percentChange": "+0.38"}]Bad Request: /crypto/
This data is not present when attemping to print(request.data) within my serializer. All that is returned is {}. Here is my Crypto Model
Models.py
# Template Model
class Crypto(models.Model):
name = models.CharField(max_length=30) # Name of the stock
price = models.FloatField() # Opening stock price
change = models.FloatField() # Closing stock price
percentChange = models.FloatField() # Amount of sales
def __str__(self):
return self.name
OK, first read this example to know more about creating multiple at once.
Your problem is simply with the format you passed to post.
In JSON when you are passing multiple objects then we expect something like that
[
{
"name":"John",
"age":30,
"cars":[ "Ford", "BMW", "Fiat" ]
},
{
"name":"Matt",
"age":25,
"cars":["BMW", "Fiat" ]
},
]
As you can see it's a JSON array.
In DRF when you are creating a serializer you write something to convert from JSON to one model instance and vice versa. So when we have multiple objects like the above example and we want to do whatever with it, we add the keyword many=True to our serializer so it knows that the passed data has many objects, not just one, so it loops over it and converts for each one.
Let's get back to your created data:
crypto = {
"id": [],
"name": [],
"price": [],
"change": [],
"percentChange": []
}
Which is a dictionary and each key has an array, you can see now why it doesn't work at all as it doesn't match what the serializer is expecting
Of course, if this is the format you want to pass, we can work on the serializer and write some code and change its fields so it handles it, but I feel it's better to construct the data as an array of JSON and pass it.
You can use zip() to loop on two lists or more at the same time, so you create the complete object at once, or whatever you want there are too many ways.
UPDATE:
As a suggestion, the simplest you can do to make an array of dictionaries is,
crypto = []
# For Loop - Yahoo Finance requires us to crawl through specific
# attributes to find data
for listing in soup.find_all('tr', attrs={'class':'simpTblRow'}):
listing_dic={}
for name in listing.find_all('td', attrs={'aria-label':'Name'}):
listing_dic["name"] = name.text
for price in listing.find_all('td', attrs={'aria-label':'Price (Intraday)'}):
listing_dic["price"] = price.text.replace(',', '')
for change in listing.find_all('td', attrs={'aria-label':'Change'}):
listing_dic["change"] = change.text
for percentChange in listing.find_all('td', attrs={'aria-label':'% Change'}):
listing_dic["percentChange"] = percentChange.text.replace('%', ''))
crypto.append(listing_dic)
Note: I assumed every child for loop is running only once because it is one row, as I'm not a soup expert I will let you handle this.
Then you can pass data or use json.dump() or whatever, it's ready for use now
UPDATE 2:
apply json.dump() on every dict inside the loop, then apply it agin on the array.
crypto.append(json.dump(listing_dic))
then data=json.dump(stocks)

Django Rest Framework receive primary key value in POST and return model object as nested serializer

I'm not completely sure that the title of my question is as specific as I wanted it to be, but this is the case:
I have a HyperlinkedModelSerializer that looks like this:
class ParentArrivalSerializer(serializers.HyperlinkedModelSerializer):
carpool = SchoolBuildingCarpoolSerializer()
class Meta:
model = ParentArrival
As you can see the carpool is defined as a nested serializer object and what I want is to be able to make a POST request to create a ParentArrival in this way (data as application/json):
{
...
"carpool": "http://localhost:8000/api/school-building-carpools/10/"
...
}
And receive the data in this way:
{
"carpool": {
"url": "http://localhost:8000/api/school-building-carpools/10/"
"name": "Name of the carpool",
...
}
}
Basically, I'm looking for a way to deal with nested serializers without having to send data as an object (but id or url in this case) in POST request, but receiving the object as nested in the serialized response.
I have been happy with my previous solution, but decided to look again and I think I have another solution that does exactly what you want.
Basically, you need to create your own custom field, and just overwrite the to_representation method:
class CarpoolField(serializers.PrimaryKeyRelatedField):
def to_representation(self, value):
pk = super(CarpoolField, self).to_representation(value)
try:
item = ParentArrival.objects.get(pk=pk)
serializer = CarpoolSerializer(item)
return serializer.data
except ParentArrival.DoesNotExist:
return None
def get_choices(self, cutoff=None):
queryset = self.get_queryset()
if queryset is None:
return {}
return OrderedDict([(item.id, str(item)) for item in queryset])
class ParentArrivalSerializer(serializers.HyperlinkedModelSerializer):
carpool = CarpoolField(queryset=Carpool.objects.all())
class Meta:
model = ParentArrival
This will allow you to post with
{
"carpool": 10
}
and get:
{
"carpool": {
"url": "http://localhost:8000/api/school-building-carpools/10/"
"name": "Name of the carpool",
...
}
}
It's simple.
As you know, Django appends "_id" to the field name in the ModelClass, and you can achieve it in the SerializerClass, and the original filed can also be achieved. All you have to do is like this
class ParentArrivalSerializer(serializers.HyperlinkedModelSerializer):
# ...
carpool_id = serializers.IntegerField(write_only=True)
carpool = SchoolBuildingCarpoolSerializer(read_only=True)
# ...
class Meta:
fields = ('carpool_id', 'carpool', ...)
And use carpool_id in POST request.
How about overriding the to_representation method?
class YourSerializer(serializers.ModelSerializer):
class Meta:
model = ModelClass
fields = ["id", "foreignkey"]
def to_representation(self, instance):
data = super(YourSerializer, self).to_representation(instance)
data['foreignkey'] = YourNestedSerializer(instance.foreignkey).data
return data
One way to do it is to keep 'carpool' as the default you get from DRF, and then add a read-only field for the nested object.
Something like this (I don't have time to test the code, so consider this pseudo-code. If you cannot get it to work, let me know, and will spend more time):
class ParentArrivalSerializer(serializers.HyperlinkedModelSerializer):
carpool_info = serializers.SerializerMethodField(read_only=True)
class Meta:
model = ParentArrival
fields = ('id', 'carpool', 'carpool_info',)
def get_carpool_info(self, obj):
carpool = obj.carpool
serializer = SchoolBuildingCarpoolSerializer(carpool)
return serializer.data
If your only nested object is carpool, I would also suggest switching to the regular ModelSerializer so carpool only shows the ID (10) and the nested object then can show the URL.
class ParentArrivalSerializer(serializers.ModelSerializer):
....
and then if it all works, you will be able to do a post with
{
"carpool": 10
}
and your get:
{
"carpool": 10
"carpool_info": {
"url": "http://localhost:8000/api/school-building-carpools/10/"
"name": "Name of the carpool",
...
}
}
I have never found another solution, so this is the trick I have used several times.

Django DRF multi model view

I wanna retrieve few models in a single request so i'll get:
{
"cars": [
{
"id": "1",
"name": "foo"
}
],
"trucks": [
{
"id": "1",
"name": "goo"
}
],
"bikes": [
{
"id": "1",
"name": "doo"
}
],
}
for that I've create a serializer:
class VehiclesSerializer(serializers.Serializer):
cars = CarSerializer(many=True, read_only=True)
trucks = TruckSerializer(many=True, read_only=True)
bikes = BikeSerializer(many=True, read_only=True)
and a view:
class VehiclesListView(generics.ListAPIView):
queryset = ???????
serializer_class = VehiclesSerializer
but as you can see, I haven't manage to figure out how to write the queryset.
Any help?
UPDATE:
Just to clarify my question. There is no Vehicle model.
That's why I'm NOT writing the regular
queryset = Vehicles.objects.all()
There are a few options I think. The two cleanest ones would be:
Have different endpoints for your models. This feels like the most RESTful approach to me.
Create a VehicleModel that is ForeignKey related to your other models. That should even work with the Serializer you have written.
The 2nd approach would look something like this for the models:
# models.py
class Vehicle(models.Model):
pass
class Truck(models.Model):
vehicle = models.ForeignKey(Vehicle, related_name='trucks')
....
And like this for the views:
#views.py
class VehiclesListView(generics.ListAPIView):
queryset = Vehicle.objects.prefetch_related('cars', 'trucks', 'bikes').all()
serializer_class = VehiclesSerializer
Note on the prefetch_related() in there: Django will use four DB queries to get your objects, one for each related model and one for the main model. If you use Vehicle.objects.all() by itself, the Serializer will create a DB requests for every Vehicle it encounters. See Django docs here
If you don't want to do that, you can always override ListAPIView.list with your custom logic, even bypass the serializer completely:
class VehiclesListView(generics.ListAPIView):
def list(self, request, *args, **kwargs):
cars = Cars.objects.values('id', 'name')
trucks = Trucks.objects.values('id', 'name')
bikes = Bikes.objects.values('id', 'name')
out = {
'trucks': trucks,
'cars': cars,
'bikes': bikes,
}
return Response(out)
Note on using values() instead of all(): You don't really use any other model fields in your serializer, so there's no use in querying extra fields. Docs
You should really read the API Documentation.
queryset = Vehicles.objects.all();
or to get random 100 or 10 objects:
Vehicles.objects.all().order_by('?')[:100]
or
Vehicles.objects.all().order_by('?')[:10]
queryset = Vehicles.objects.all()
Or if you need to filter on datetime or request properties.
def get_queryset(self):
return Vehicles.objects.all()
The best that I've got was without using generics:
class VehiclesListView(APIView):
def get(self, request, *args, **kwargs):
ser = VehiclesSerializer({
'cars': Car.objects.all(),
'trucks': Truck.objects.all(),
'bikes': Bike.objects.all()
})
return JsonResponse(ser.data)
Thanks all for your help :)
The easiest way is to use DjangoRestMultipleModels
The documentation is beautiful.
In your case the view would look like this:
from drf_multiple_model.views import MultipleModelAPIView
class VehicleView(MultipleModelAPIView):
queryList = [
(Car.objects.all(),CarSerializer), #(queryset,Serializer)
(Truck.objects.all(),TruckSerializer),
(Bike.objects.all(), BikeSerializer),
]
Hope this helps someone..

How can one customize Django Rest Framework serializers output?

I have a Django model that is like this:
class WindowsMacAddress(models.Model):
address = models.TextField(unique=True)
mapping = models.ForeignKey('imaging.WindowsMapping', related_name='macAddresses')
And two serializers, defined as:
class WindowsFlatMacAddressSerializer(serializers.Serializer):
address = serializers.Field()
class WindowsCompleteMappingSerializer(serializers.Serializer):
id = serializers.Field()
macAddresses = WindowsFlatMacAddressSerializer(many=True)
clientId = serializers.Field()
When accessing the serializer over a view, I get the following output:
[
{
"id": 1,
"macAddresses": [
{
"address": "aa:aa:aa:aa:aa:aa"
},
{
"address": "bb:bb:bb:bb:bb:bb"
}
],
"clientId": null
}
]
Almost good, except that I'd prefer to have:
[
{
"id": 1,
"macAddresses": [
"aa:aa:aa:aa:aa:aa",
"bb:bb:bb:bb:bb:bb"
],
"clientId": null
}
]
How can I achieve that ?
Create a custom serializer field and implement to_native so that it returns the list you want.
If you use the source="*" technique then something like this might work:
class CustomField(Field):
def to_native(self, obj):
return obj.macAddresses.all()
I hope that helps.
Update for djangorestframework>=3.9.1
According to documentation, now you need override either one or both of the to_representation() and to_internal_value() methods. Example
class CustomField(Field):
def to_representation(self, value)
return {'id': value.id, 'name': value.name}
Carlton's answer will work do the job just fine. There's also a couple of other approaches you could take.
You can also use SlugRelatedField, which represents the relationship, using a given field on the target.
So for example...
class WindowsCompleteMappingSerializer(serializers.Serializer):
id = serializers.Field()
macAddresses = serializers.SlugRelatedField(slug_field='address', many=True, read_only=True)
clientId = serializers.Field()
Alternatively, if the __str__ of the WindowsMacAddress simply displays the address, then you could simply use RelatedField, which is a basic read-only field that will give you a simple string representation of the relationship target.
# models.py
class WindowsMacAddress(models.Model):
address = models.TextField(unique=True)
mapping = models.ForeignKey('imaging.WindowsMapping', related_name='macAddresses')
def __str__(self):
return self.address
# serializers.py
class WindowsCompleteMappingSerializer(serializers.Serializer):
id = serializers.Field()
macAddresses = serializers.RelatedField(many=True)
clientId = serializers.Field()
Take a look through the documentation on serializer fields to get a better idea of the various ways you can represent relationships in your API.

Categories

Resources