Save related Images Django REST Framework - python

I have this basic model layout:
class Listing(models.Model):
name = models.TextField()
class ListingImage(models.Model):
listing = models.ForeignKey(Listing, related_name='images', on_delete=models.CASCADE)
image = models.ImageField(upload_to=listing_image_path)
Im trying to write a serializer which lets me add an rest api endpoint for creating Listings including images.
My idea would be this:
class ListingImageSerializer(serializers.ModelSerializer):
class Meta:
model = ListingImage
fields = ('image',)
class ListingSerializer(serializers.ModelSerializer):
images = ListingImageSerializer(many=True)
class Meta:
model = Listing
fields = ('name', 'images')
def create(self, validated_data):
images_data = validated_data.pop('images')
listing = Listing.objects.create(**validated_data)
for image_data in images_data:
ListingImage.objects.create(listing=listing, **image_data)
return listing
My Problems are:
I'm not sure how and if I can send a list of images in a nested dictionary using a multipart POST request.
If I just post an images list and try to convert it from a list to a list of dictionaries before calling the serializer, I get weird OS errors when parsing the actual image.
for key, item in request.data.items():
if key.startswith('images'):
# images.append({'image': item})
request.data[key] = {'image': item}
My request code looks like this:
import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder
api_token = 'xxxx'
images_data = MultipartEncoder(
fields={
'name': 'test',
'images[0]': (open('lilo.png', 'rb'), 'image/png'),
'images[1]': (open('panda.jpg', 'rb'), 'image/jpeg')
}
)
response = requests.post('http://127.0.0.1:8000/api/listings/', data=images_data,
headers={
'Content-Type': images_data.content_type,
'Authorization': 'Token' + ' ' + api_token
})
I did find a very hacky solution which I will post in the answers but its not really robust and there needs to be a better way to do this.

So my solution is based off of this post and works quite well but seems very unrobust and hacky.
I change the images field from a relation serializer requiring a dictonary to a ListField. Doing this i need to override the list field method to actually create a List out of the RelatedModelManager when calling "to_repesentation".
This baiscally behaves like a list on input, but like a modelfield on read.
class ModelListField(serializers.ListField):
def to_representation(self, data):
"""
List of object instances -> List of dicts of primitive datatypes.
"""
return [self.child.to_representation(item) if item is not None else None for item in data.all()]
class ListingSerializer(serializers.ModelSerializer):
images = ModelListField(child=serializers.FileField(max_length=100000, allow_empty_file=False, use_url=False))
class Meta:
model = Listing
fields = ('name', 'images')
def create(self, validated_data):
images_data = validated_data.pop('images')
listing = Listing.objects.create(**validated_data)
for image_data in images_data:
ListingImage.objects.create(listing=listing, image=image_data)
return listing

Related

DRF: How to add annotates when create an object manually?

Currently I'm trying to create an expected json to use in my test:
#pytest.mark.django_db(databases=['default'])
def test_retrieve_boards(api_client):
board = baker.make(Board)
objs = BoardSerializerRetrieve(board)
print(objs.data)
url = f'{boards_endpoint}{board.id}/'
response = api_client().get(url)
assert response.status_code == 200
But i'm receiving the following error:
AttributeError: Got AttributeError when attempting to get a value for field `cards_ids` on serializer `BoardSerializerRetrieve`.
E The serializer field might be named incorrectly and not match any attribute or key on the `Board` instance.
E Original exception text was: 'Board' object has no attribute 'cards_ids'
Currently cards_idsare added on my viewSet on get_queryset method:
def get_queryset(self):
#TODO: last update by.
#TODO: public collections.
"""Get the proper queryset for an action.
Returns:
A queryset object according to the request type.
"""
if "pk" in self.kwargs:
board_uuid = self.kwargs["pk"]
qs = (
self.queryset
.filter(id=board_uuid)
.annotate(cards_ids=ArrayAgg("card__card_key"))
)
return qs
return self.queryset
and this is my serializer:
class BoardSerializerRetrieve(serializers.ModelSerializer):
"""Serializer used when retrieve a board
When retrieve a board we need to show some informations like last version of this board
and the cards ids that are related to this boards, this serializer will show these informations.
"""
last_version = serializers.SerializerMethodField()
cards_ids = serializers.ListField(child=serializers.IntegerField())
def get_last_version(self, instance):
last_version = instance.history.first().prev_record
return HistoricalRecordSerializer(last_version).data
class Meta:
model = Board
fields = '__all__'
what is the best way to solve it? I was thinking in create a get_cards_ids method on serializer and remove annotate, but I don't know how to do it and justing googling it now. rlly don't know if this is the correct way to do it.
Test the view, not the serializer, i.e. remove BoardSerializerRetrieve(board) from your test code.
cards_ids is annotated on ViewSet level. The annotated queryset is then passed to serializer.
#pytest.mark.django_db(databases=['default'])
def test_retrieve_boards(api_client):
board = baker.make(Board)
url = f'{boards_endpoint}{board.id}/'
response = api_client().get(url)
assert response.status_code == 200
Also, instead of building the URL manually with url = f'{boards_endpoint}{board.id}/', consider using reverse, e.g. url = reverse("path-name", kwargs={"pk": board.id}).

Rest API schema to retrieve value from database and return

I need to make a Rest API which will take 3 inputs: input_list (list of srings sentences), from_lang (string), to_lang (string) and return list of string after fetching values from databse table.
Example:
input - {input_list: ['how are you', 'see you later', 'where are you'], from_lang: 'english', to_lang: 'spanish' }
output - {['cómo estás', 'nos vemos más tarde', 'Dónde estás']}
A service will call this API with list of sentences in any supported language, and in return they will get list of same length with translated sentence if it exist in database or null value if it doesn't exist.
How should I proceed?
What I have done is, I have created a serializer to handle/validate incoming request in serializers.py:
def supported_lang(value):
if value not in SUPPORTED_LANGUAGES:
print(value)
print(SUPPORTED_LANGUAGES)
raise serializers.ValidationError('Language not supported')
class TranslateSerializer(serializers.Serializer):
input_list = serializers.ListField(
child=serializers.CharField(allow_blank=False),
allow_empty=False
)
from_language = serializers.CharField(validators=[supported_lang])
to_language = serializers.CharField(validators=[supported_lang])
And I have defined a simple model for storing translations in model.py:
class TranslationModel(models.Model):
english = models.CharField(blank=False, max_length=MAX_LENGTH, unique=True)
spanish = models.CharField(blank=True, max_length=MAX_LENGTH)
italian = models.CharField(blank=True, max_length=MAX_LENGTH)
is_active = models.BooleanField(default=True)
Then in my views.py I have handled post requests like below
class TranslateView(views.APIView):
def post(self, request):
serializer = TranslateSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
serialized_data = serializer.validated_data
result = self.get_translations(serialized_data) # here i am confused
return Response(result)
So far so good, now I am confused how to fetch the data from model, and how to return it (following best practices).
I have defined a function get_translations() in views.py :
def get_translations(self, request):
output_list = [] # for return response
for sentence in request['input_list']:
if request['from_language'] == 'english':
queryset = TranslationModel.objects.filter(english=sentence)
elif request['from_language'] == 'spanish':
queryset = TranslationModel.objects.filter(hindi=sentence)
elif request['from_language'] == 'italian':
queryset = TranslationModel.objects.filter(telugu=sentence)
try:
resp = queryset.values()[0][request['to_language']]
except:
resp = ""
if not resp:
# print('empty response')
output_list.append(None)
else:
output_list.append(resp)
return output_list
I mainly have three confusions:
Is it good practice to use serializers.Serializer for handling/validating incoming requests
Should I use serializers for model as well, if yes how
How to pass filter value dynamically
Any help is appreciated.
Is it good practice to use serializers.Serializer for handling/validating incoming requests
Of course, It is. That's essentially one of the core concepts of seriaizers (Parsing, Validation and serialization)
Should I use serializers for model as well, if yes how
There is no need for that. You only need a serializer if data is being sent or received from the user which is not the case here.
How to pass filter value dynamically
Use dictionary unpacking
k = {request['from_language']: sentence}
queryset = TranslationModel.objects.filter(**k)

Unexpected error while request parsing using a serializer

While parsing my request data from front-end and converting into JSON format using a serializer. I am getting some unexpected errors.
while request parsing pattern using serializers given as mentioned below, it shows me the following error:(I found the below error using: contact_serializer.errors)
{'address': {u'non_field_errors': [u'Invalid data. Expected a dictionary, but got str.']}}
I do not think it will work like this. You have to remember here is that if you input the values like this, it will ultimately be stored in DB, and it is hard coded values. Even if you insist to do it like this, then use a list of dictionary like this:
request.data['phone_number'] = [{'number': '9999999999'}]
request.data['cont_email'] = [{'email':'tim#gmail.com'}]
And update the serializer like this:
class CrmContactSerializer(serializers.ModelSerializer):
phone_number = PhoneNumberSerializer(source = 'contact_number', many=True)
cont_email = ContactEmailSerializer(source = 'contact_email', many=True)
class Meta:
model = RestaurantContactAssociation
fields = ('id','phone_number','cont_email','contact')
def create(self, validated_data):
phone_number = validated_data.pop('contact_number')
cont_email = validated_data.pop('contact_email')
restaurant = super(CrmContactSerializer, self).create(validated_data)
phone_instance = PhoneNumber(**phone_number)
phone_instance.restaurant = restaurant
phone_instance.save()
email_instance = ContactEmail(**phone_number)
email_instance.restaurant = restaurant
email_instance.save()
return restaurant
Reason for many=True is that one restaurant can have multiple numbers or emails(as it has one to many relationship with respective models).
Now, if you think of proper way of implementing, you can make phone_number and cont_email read only fields, so that it will be used when only reading, not writing:
class CrmContactSerializer(serializers.ModelSerializer):
phone_number = PhoneNumberSerializer(source = 'contact_number', read_only=True)
cont_email = ContactEmailSerializer(source = 'contact_email', read_only=True)
class Meta:
model = RestaurantContactAssociation
fields = ('id','phone_number','cont_email','contact')
In that way, validation error can be handled for phone number and cont email.

Django Rest Framework writable nested serializer with multiple nested objects

I'm trying to create a writable nested serializer. My parent model is Game and the nested models are Measurements. I am trying to post this data to my DRF application using AJAX. However, when try to post the data, the nested Measurements are empty OrderedDict().
Here are my models:
class Game(models.Model):
start_timestamp = models.DateTimeField(auto_now_add=False)
end_timestamp = models.DateTimeField(auto_now_add=False)
date_added = models.DateTimeField(auto_now_add=True)
class Measurement(models.Model):
game = models.ForeignKey(Game, on_delete=models.PROTECT, related_name='measurements')
measurement_type = models.CharField(max_length=56)
measurement = models.CharField(max_length=56)
timestamp = models.DateTimeField(auto_now_add=False)
date_added = models.DateTimeField(auto_now_add=True)
Here are my serializers:
class MeasurementSerializer(serializers.ModelSerializer):
timestamp = serializers.DateTimeField(input_formats=(['%Y-%m-%d %H:%M:%S.%Z', 'iso-8601']), required=False)
class Meta:
model = Measurement
fields = ('measurement_type', 'measurement', 'timestamp')
class GameSerializer(serializers.ModelSerializer):
start_timestamp = serializers.DateTimeField(input_formats=(['%Y-%m-%d %H:%M:%S.%Z', 'iso-8601']))
end_timestamp = serializers.DateTimeField(input_formats=(['%Y-%m-%d %H:%M:%S.%Z', 'iso-8601']))
measurements = MeasurementSerializer(many=True)
class Meta:
model = Game
fields = ('id', 'start_timestamp', 'end_timestamp', 'measurements')
def create(self, validated_data):
measurements = validated_data.pop('measurements')
game = Game.objects.create(**validated_data)
for measurement in measurements:
Measurement.objects.create(game=game, **measurement)
return game
My view for Game is the following:
class GameList(generics.ListCreateAPIView):
queryset = Game.objects.all()
serializer_class = GameSerializer
I followed this tutorial for the structure.
I am trying to post to this API via AJAX, the code below:
$.ajax({
url: base_url + '/games/',
dataType: "json",
data: {
"start_timestamp": "2016-02-16 14:51:43.000000",
"end_timestamp": "2016-02-16 14:53:43.000000",
"measurements":[
{'measurement_type':'type1', 'measurement':'71', 'timestamp':'2016-02-16 14:53:43.000000'},
{'measurement_type':'type1', 'measurement':'72', 'timestamp':'2016-02-16 14:54:43.000000'},
{'measurement_type':'type1', 'measurement':'73', 'timestamp':'2016-02-16 14:55:43.000000'},
]
},
type: 'POST'
})
.error(function(r){})
.success(function(data){})
});
On posting this data, I find in the create method within the GameSerializer that the validate_data.pop('measurements') contains a list of 3 ordered dictionaries (OrderedDict()) that are empty.
UPDATE: I've found that that the initial_data coming in via request.data is structured like so:
'emotion_measurements[0][measurement_type]' (4397175560) = {list} ['type1']
'emotion_measurements[0][measurement]' (4397285512) = {list} ['71']
'emotion_measurements[0][timestamp]' (4397285600) = {list} ['2016-02-16 14:53:43.000000']
'emotion_measurements[1][measurement_type]' (4397175040) = {list} ['type1']
'emotion_measurements[1][measurement]' (4397285864) = {list} ['72']
'emotion_measurements[1][timestamp]' (4397285952) = {list} ['2016-02-16 14:54:43.000000']
'emotion_measurements[2][measurement_type]' (4397175040) = {list} ['type1']
'emotion_measurements[2][measurement]' (4397285864) = {list} ['73']
'emotion_measurements[2][timestamp]' (4397285952) = {list} ['2016-02-16 14:55:43.000000']
Has anyone encountered this issue before? Thanks!
UPDATE #2
I was able to resolve this (although I believe it is more of a workaround than a solution) by adding the following to my MeasurementSerializer:
def to_internal_value(self, data):
formatted_data = json.dumps(data)
formatted_data = formatted_data.replace("[", "").replace("]","")
formatted_data = json.loads(formatted_data)
return formatted_data
The Measurement data coming in was a QueryDict when I believe I needed a Dict. There were also some extra brackets around the key and values so I had to remove those as well.
Still seeking a better answer than this!
The problem here is on the front-end side. By default the server interprets the data as application/x-www-form-urlencoded and in order for it to understand that you are sending it a json, you need to specify the contentType in your $.ajax request:
$.ajax({
url: base_url + '/games/',
dataType: "json",
data: {...},
contentType: 'application/json; charset=UTF-8', // add this line
type: 'POST'
})
.error(function(r){})
.success(function(data){});
Now your validated_data.pop('measurements') in create() method of your GameSerializer should yield three objects with your measurements (but don't forget to redo your workaround from Update#2).

Foreign Key Resource from dynamic field

I've got an API endpoint called TrackMinResource, which returns the minimal data for a music track, including the track's main artist returned as an ArtistMinResource. Here are the definitions for both:
class TrackMinResource(ModelResource):
artist = fields.ForeignKey(ArtistMinResource, 'artist', full=True)
class Meta:
queryset = Track.objects.all()
resource_name = 'track-min'
fields = ['id', 'artist', 'track_name', 'label', 'release_year', 'release_name']
include_resource_uri = False
cache = SimpleCache(public=True)
def dehydrate(self, bundle):
bundle.data['full_artist_name'] = bundle.obj.full_artist_name()
if bundle.obj.image_url != settings.NO_TRACK_IMAGE:
bundle.data['image_url'] = bundle.obj.image_url
class ArtistMinResource(ModelResource):
class Meta:
queryset = Artist.objects.all()
resource_name = 'artist-min'
fields = ['id', 'artist_name']
cache = SimpleCache(public=True)
def get_resource_uri(self, bundle_or_obj):
return '/api/v1/artist/' + str(bundle_or_obj.obj.id) + '/'
The problem is, the artist field on Track (previously a ForeignKey) is now a model method called main_artist (I've changed the structure of the database somewhat, but I'd like the API to return the same data as it did before). Because of this, I get this error:
{"error": "The model '<Track: TrackName>' has an empty attribute 'artist' and doesn't allow a null value."}
If I take out full=True from the 'artist' field of TrackMinResource and add null=True instead, I get null values for the artist field in the returned data. If I then assign the artist in dehydrate like this:
bundle.data['artist'] = bundle.obj.main_artist()
...I just get the artist name in the returned JSON, rather than a dict representing an ArtistMinResource (along with the associated resource_uris, which I need).
Any idea how to get these ArtistMinResources into my TrackMinResource? I can access an ArtistMinResource that comes out fine using the URL endpoint and asking for it by ID. Is there a function for getting that result from within the dehydrate function for TrackMinResource?
You can use your ArtistMinResource in TrackMinResource's dehydrate like this (assuming that main_artist() returns the object that your ArtistMinResource represents):
artist_resource = ArtistMinResource()
artist_bundle = artist_resource.build_bundle(obj=bundle.obj.main_artist(), request=request)
artist_bundle = artist_resource.full_dehydrate(artist_bundle)
artist_json = artist_resource.serialize(request=request, data=artist_bundle, format='application/json')
artist_json should now contain your full artist representation. Also, I'm pretty sure you don't have to pass the format if you pass the request and it has a content-type header populated.

Categories

Resources