I have almost tried everything but am not able to reach at the point
model.py file
class RecievingImages(models.Model):
"""Original and Masked Images"""
....
name = models.CharField(max_length = 100, unique = True, primary_key=True)
area = models.IntegerField()
number = models.IntegerField()
agency_name = models.CharField(max_length=100, default='general')
rawImage = models.ImageField(upload_to=imageNameForRawImage,)
...
class UpdationImages(models.Model):
""" Update Image wrt name"""
....
name = models.ForeignKey(RecievingImages, on_delete=models.PROTECT, related_name='updated')
updated_image = models.ImageField(upload_to=UpdatedImageFolder,)
updated_image_url = models.TextField(default='None')
....
serializer.py
class UpdatedImageSerializer(serializers.ModelSerializer):
class Meta:
model = UpdationImages
fields = ('name', 'updated_image', 'updated_image_url')
class RecievingImagesSerializer(serializers.ModelSerializer):
updated = UpdatedImageSerializer(many= True, read_only=True)
class Meta:
model = RecievingImages
fields = ('updated','name','area','number','agency_name', rawImage)
I have used related_name in the model and also following the documentation along with that with many = True
But still in serializer.data updated does not show
views.py
class MappingSinglePhoto(APIView):
""" Mapping Single Image """
def post(self, request):
try:
data = request.data
# import pdb; pdb.set_trace()
name_of_image = data['name']
mapped_images_qs = UpdationImages.objects.select_related('name').filter(name = name_of_image)
for image in mapped_images_qs:
serializer = RecievingImagesSerializer(instance = image)
pdb.set_trace()
serializer.data
# return Response(serializer.data)
except Exception as e:
print(e)
NOTE
if I use depth=1 then it's working fine, but I am not looking for all the fields that depth displays.
Thanks & Regards
Thanks to #ArakkalAbu
There was a problem in query views.py
class MappingSinglePhoto(APIView):
""" Mapping Single Image """
def post(self, request):
try:
data = request.data
# import pdb; pdb.set_trace()
name_of_image = data['name']
mapped_images_qs = RecievingImages.objects.filter(name = name_of_image)
for image in mapped_images_qs:
serializer = RecievingImagesSerializer(instance = image)
pdb.set_trace()
serializer.data
# return Response(serializer.data)
except Exception as e:
print(e)
Related
I want to create Note object which one of the fields of the model is an ImageField using Django Rest Framework.
I can already create objects and update all different fields using my API, except for images.
My code:
models.py
class Note(OwnedModel):
note_id = models.UUIDField(primary_key=True,
default=uuid.uuid4,
editable=False)
# note_owner = models.ForeignKey(, null=True, blank=True, on_delete=models.SET_NULL)
note_name = models.CharField(max_length=50)
body = models.TextField()
updated = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
qr_image = models.ImageField(upload_to='notes', null=True)
def __str__(self):
return self.note_name[0:50]
class Meta:
ordering = ['-updated']
views.py
#api_view(['GET', 'POST'])
def getNote(request, pk=None):
if request.method == 'GET':
note = Note.objects.get(note_id=pk)
serializer = NoteSerializer(note, many=False)
return Response(serializer.data)
elif request.method == 'POST':
_data = request.data.copy()
owner = request.user.id
_data["owner"] = owner
# Generate QR code
qr_image = generate_qr(_data["note_name"])
# HOW TO PASS THE IMAGE TO THE SERIALIZER?
_data["qr_image"] = qr_image
# _data["qr_image"] = qr_image[0]
# _data["qr_image"] = qr_image[1]
serializer = NoteSerializer(data=_data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(status=status.HTTP_400_BAD_REQUEST)
serializers.py
class NoteSerializer(ModelSerializer):
class Meta:
model = Note
fields = '__all__'
qr_code.py
import qrcode
def generate_qr(qr_file_name=None):
qr = qrcode.QRCode(
version=1,
# error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=4,
border=3,
)
qr.add_data(qr_file_name)
qr.make(fit=True)
img = qr.make_image()
# img = qr.make_image(fill_color="black", back_color="white")
path='images/notes/'+str(qr_file_name)+'.jpg'
img.save(path)
return path, img
The QR code is properly generated and saved in the upload path perfectly.
What I cannot manage to build line _data["qr_image"] = qr_image correctly, or if I need to return the image in a different way from the generate_qr function. Everything else it is working well (for example create the Note object form the Admin and uploading the qr image).
#api_view(['GET', 'POST'])
def getNote(request, pk=None):
if request.method == 'GET':
# automatically raise 404 if obj does not exist
note = get_object_or_404(Note.objects, note_id=pk)
# many=False is default, dont need that
serializer = NoteSerializer(note)
return Response(serializer.data)
elif request.method == 'POST':
serializer = NoteSerializer(data=request.data)
# raise_exception=True will raise validation error (400) automatically
serializer.is_valid(raise_exception=True)
# get note name after validation to be sure it has proper length etc.
qr_image = generate_qr(serializer.validated_data.get("note_name"))
# attributes provided here will bypass validation
# and will be injected directly to model create method
serializer.save(
owner=request.user,
qr_image=qr_image
)
return Response(serializer.data, status=status.HTTP_201_CREATED)
Im not sure that your generate_qr function should save image in the given path because Note should do it for you on create.
Tip: check the viewsets.GenericViewSet class - it will properly split your view into methods and makes the life easier.
I am hoping that I can find a way to resize an uploaded image file before it is put into the database.
I am new to Django with REST, so I am not sure how this would be done. It seems that whatever is serialized is just kind of automatically railroaded right into the model. Which I suppose is the point (it's certainly an easy thing to setup).
To clarify, I already have a function tested and working that resizes the image for me. That can be modified as needed and is no problem for me. The issue really is about sort of "intercepting" the image, making my changes, and then putting it into the model. Could someone help me out with some ideas of tactics to get that done? Thanks.
The Model:
class Media(models.Model):
objects = None
username = models.ForeignKey(User, to_field='username',
related_name="Upload_username",
on_delete=models.DO_NOTHING)
date = models.DateTimeField(auto_now_add=True)
media = models.FileField(upload_to='albumMedia', null=True)
file_type = models.CharField(max_length=12)
MEDIA_TYPES = (
('I', "Image"),
('V', "Video")
)
media_type = models.CharField(max_length=1, choices=MEDIA_TYPES, default='I')
user_access = models.CharField(max_length=1, choices=ACCESSIBILITY, default='P')
class Meta:
verbose_name = "MediaManager"
The View with post method:
class MediaView(APIView):
queryset = Media.objects.all()
parser_classes = (MultiPartParser, FormParser)
permission_classes = [permissions.IsAuthenticated, ]
serializer_class = MediaSerializer
def post(self, request, *args, **kwargs):
user = self.request.user
print(user.username)
request.data.update({"username": user.username})
media_serializer = MediaSerializer(data=request.data)
# media_serializer.update('username', user.username)
if media_serializer .is_valid():
media_serializer.save()
return Response(media_serializer.data, status=status.HTTP_201_CREATED)
else:
print('error', media_serializer.errors)
return Response(media_serializer.errors,status=status.HTTP_400_BAD_REQUEST)
The Serializer:
class MediaSerializer(serializers.ModelSerializer):
class Meta:
model = Media
fields = '__all__'
def to_representation(self, instance):
data = super(MediaSerializer, self).to_representation(instance)
return data
You can use validate method to validate and/or change the values from data dictionary.
class MediaSerializer(serializers.ModelSerializer):
...
def validate(self, data):
value_from_form = data['value_from_form']
value_from_form = 'Something else'
data['value_from_form'] = value_from_form
return data
I used Django restframework and mongoengine.
Here is my model and serializer.
[model.py]
class Attachment(EmbeddedDocument):
attachment_id = SequenceField()
path = StringField()
path_small = StringField()
class Book(Document):
book_id = SequenceField()
user_id = LongField(required=True)
attachments = ListField(EmbeddedDocumentField(Attachment))
created_at = DateTimeField(default=datetime.now().replace(microsecond=0))
updated_at = DateTimeField(default=datetime.now().replace(microsecond=0))
[serializer.py]
from rest_framework_mongoengine.serializers import DocumentSerializer
from rest_framework.serializers import ImageField
from books.models.mongo import Book
class BookSerializer(DocumentSerializer):
image = ImageField()
class Meta:
model = Appeal
fields = (
'book_id',
'image',
)
Work flow like this.
Upload image to s3
Get s3 path
Save s3 path to attachments field in models.py.
So do not defined attachments to ImageField() in models.py.
Just set image = ImageField() in serializer to validate it is correct image.
But when I validate with serializer.is_valid(), image get None.
[views.py]
class BookList(GenericAPIView):
serializer_class = BookSerializer
queryset = ''
def post(self, request: Request) -> Union[Response, NoReturn]:
serializer = BookSerializer(data=request.data)
if serializer.is_valid():
print(serializer.data)
appeal = CreateAppealInteractor().execute(request=serializer.data)
return Response(status=status.HTTP_200_OK)
As you know that after serializer.is_valid(), I printed serializer.data.
But it throw None like this -> {'book_id': 1, 'image': None}
Is there any error in my code?
Thanks.
Problem is solved in chat room. Just use validated_data.
class BookList(GenericAPIView):
serializer_class = BookSerializer
queryset = ''
def post(self, request: Request) -> Union[Response, NoReturn]:
serializer = BookSerializer(data=request.data)
if serializer.is_valid():
appeal = CreateAppealInteractor().execute(request=serializer.validated_data)
return Response(status=status.HTTP_200_OK)
I am building an activity model, somewhat similar to this package. It has an actor, verb and the target.
class Activity(models.Model):
actor_type = models.ForeignKey(ContentType, related_name='actor_type_activities')
actor_id = models.PositiveIntegerField()
actor = GenericForeignKey('actor_type', 'actor_id')
verb = models.CharField(max_length=10)
target_type = models.ForeignKey(ContentType, related_name='target_type_activities')
target_id = models.PositiveIntegerField()
target = GenericForeignKey('target_type', 'target_id')
pub_date = models.DateTimeField(default=timezone.now)
Now whenever a new object of whichever models (Tender, Job and News) is created, a new Activity object is created, with the target being the objects of any of these three models.
eg. user (actor) published (verb) title (target)
class Tender(models.Model):
title = models.CharField(max_length=256)
description = models.TextField()
class Job(models.Model):
title = models.CharField(max_length=256)
qualification = models.CharField(max_length=256)
class News(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
title = models.CharField(max_length=150)
To get this data I am making an API which will get me the required json data. I am using django-rest-framework for this and very new with it.
class ActorSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('id', 'username', 'email')
class ActivitySerializer(serializers.HyperlinkedModelSerializer):
actor = ActorSerializer()
class Meta:
model = Activity
fields = ('url', 'actor', 'verb', 'pub_date')
In the above serializers, I knew that actor will be the User. And so I used the User model for the ActorSerializer class. But as for the target, it can be any of these three models (News/Job/Tender).
How can I make a serializer (eg. TargetSerialier class) for the ContentType object so that I can use the target in the ActivitySerializer class field?
Okay so answering my own question here. I had some help with zymud's answer. So, apparently in the documentation, there is a way to serialize the Generic relation.
So, all I had to do was create a custom field and associate that field in the serializer itself:
class ActivityObjectRelatedField(serializers.RelatedField):
def to_representation(self, value):
if isinstance(value, User):
return 'User: ' + value.username
elif isinstance(value, News):
return 'News: ' + value.title
elif isinstance(value, Job):
return 'Job: ' + value.title
elif isinstance(value, Tender):
return 'Tender: ' + value.title
raise Exception('Unexpected type of tagged object')
class ActivitySerializer(serializers.HyperlinkedModelSerializer):
actor = ActivityObjectRelatedField(read_only=True)
target = ActivityObjectRelatedField(read_only=True)
class Meta:
model = Activity
fields = ('url', 'actor', 'verb', 'target', 'pub_date')
You can implement custom field for generic key. Example:
from django.core.urlresolvers import resolve
from rest_framework.fields import Field
class GenericRelatedField(Field):
"""
A custom field that expect object URL as input and transforms it
to django model instance.
"""
read_only = False
_default_view_name = '%(model_name)s-detail'
lookup_field = 'pk'
def __init__(self, related_models=(), **kwargs):
super(GenericRelatedField, self).__init__(**kwargs)
# related models - list of models that should be acceptable by
# field. Note that all this models should have corresponding
# endpoint.
self.related_models = related_models
def _get_url_basename(self, obj):
""" Get object URL basename """
format_kwargs = {
'app_label': obj._meta.app_label,
'model_name': obj._meta.object_name.lower()
}
return self._default_view_name % format_kwargs
def _get_request(self):
try:
return self.context['request']
except KeyError:
raise AttributeError('GenericRelatedField have to be initialized with `request` in context')
def to_representation(self, obj):
""" Serializes any object to its URL representation """
kwargs = {self.lookup_field: getattr(obj, self.lookup_field)}
request = self._get_request()
return request.build_absolute_uri(reverse(self._get_url_basename(obj), kwargs=kwargs))
def clear_url(self, url):
""" Removes domain and protocol from url """
if url.startswith('http'):
return '/' + url.split('/', 3)[-1]
return url
def get_model_from_resolve_match(self, match):
queryset = match.func.cls.queryset
if queryset is not None:
return queryset.model
else:
return match.func.cls.model
def instance_from_url(self, url):
url = self.clear_url(url)
match = resolve(url)
model = self.get_model_from_resolve_match(match)
return model.objects.get(**match.kwargs)
def to_internal_value(self, data):
""" Restores model instance from its URL """
if not data:
return None
request = self._get_request()
user = request.user
try:
obj = self.instance_from_url(data)
model = obj.__class__
except (Resolver404, AttributeError, MultipleObjectsReturned, ObjectDoesNotExist):
raise serializers.ValidationError("Can`t restore object from url: %s" % data)
if model not in self.related_models:
raise serializers.ValidationError('%s object does not support such relationship' % str(obj))
return obj
Example of usage:
class ActivitySerializer(serializers.HyperlinkedModelSerializer):
target = GenericRelatedField(related_models=(News, Job, Tender))
...
There is a third party lib as per documentation that did the heavy lifting already:
https://www.django-rest-framework.org/api-guide/relations/#rest-framework-generic-relations
It is pretty neat actually, my serializer class ended up few readable lines:
class ActivityTypeSerializer(serializers.ModelSerializer):
target = GenericRelatedField({
User: UserSerializer(),
Device: DeviceSerializer(),
})
class Meta:
model = Activity
fields = ('target', 'target_id', 'verb', 'target_ct',)
My POST request to url http://127.0.0.1:8000/airlines/ fails
Here are models and corresponding serializers in my project. Initially I want to create Airlines information and then add flights later
Can someone please let me know where am I going wrong
models.py
class AirlineFirm(models.Model):
operator_name = models.CharField(max_length=30)
def __unicode__(self):
return "%s" % (self.operator_name)
class Flight(models.Model):
flight_num = models.CharField(max_length=7, primary_key=True)
src = models.CharField(max_length=20)
dest = models.CharField(max_length=20)
outbound_time = models.DateTimeField()
inbound_time = models.DateTimeField()
num_of_seats = models.IntegerField()
ticketPrice = models.FloatField()
delayed = models.BooleanField()
airlinefirm = models.ForeignKey(AirlineFirm)
serializers.py
class FlightSerializer(serializers.Serializer):
flight_num = serializers.CharField(read_only=True)
src = serializers.CharField()
dest = serializers.CharField()
outbound_time = serializers.DateTimeField()
inbound_time = serializers.DateTimeField()
num_of_seats = serializers.IntegerField()
ticketPrice = serializers.FloatField()
delayed = serializers.BooleanField()
airlinefirm = serializers.RelatedField(read_only='True')
#passengers = models.ManyToManyField(Passenger)
def create(self, validated_data):
return Flight.objects.create(**validated_data)
def update(self, instance, validated_data):
pass
class AirlineSerializer(serializers.ModelSerializer):
flights = FlightSerializer(many=True)
class Meta:
model = AirlineFirm
fields = ('operator_name','flights')
def create(self, validated_data):
flights_data = validated_data.pop('flights')
airlinefirm = AirlineFirm.objects.create(**validated_data)
for flight_data in flights_data:
Flight.objects.create(airlinefirm=airlinefirm, **flight_data)
return airlinefirm
views.py
#api_view(['GET','POST'])
def airline(request, format=None):
if request.method == 'GET':
airlines = AirlineFirm.objects.all()
serializer = AirlineSerializer(airlines, many=True)
return Response(serializer.data)
if request.method == 'POST':
#data = JSONParser().parse(request)
serializer = AirlineSerializer(data=request.data)
#import pdb;pdb.set_trace()
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
import pdb;pdb.set_trace()
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
When I send a POST request http://127.0.0.1:8000/airlines/ to my airlines view class I get 404 response
http request
import json
import requests
payload = {'operator_name':'AmericanAirlines','flights':[]}
headers = {'Content-type':'application/json'}
r = requests.post('http://127.0.0.1:8000/',data=json.dumps(payload),headers=headers)
Here is the error message:
AttributeError: Got AttributeError when attempting to get a value for field flights on serializer AirlineSerializer.
The serializer field might be named incorrectly and not match any attribute or key on the AirlineFirm instance.
Original exception text was: 'AirlineFirm' object has no attribute 'flights'.
[18/Feb/2016 13:43:58] "POST /airlines/ HTTP/1.1" 500 112039
You need to and an endpoint to the api in your urls.py if it's not there, then point to it in your request, like:
r = requests.post('http://127.0.0.1:8000/airlines',data=json.dumps(payload),headers=headers)