Now the serializer displays all the data from the CoinCosts model in response (price,timestamp), but only every 5 element is needed, how to do it? Thanks
I need something like Entry.objects.all()[::5], but I don’t know how to do this with my code.
My code now:
serializers.py
class CoinCostsSerializer(serializers.ModelSerializer):
class Meta:
fields = ('price', 'timestamp')
model = CoinCosts
class CoinSerializer(serializers.ModelSerializer):
class Meta:
fields = ('symbol', 'crr', 'costs')
model = Coins
costs = CoinCostsSerializer(source='filtered_coincosts', many=True)
views.py
class DateTimeGteFilter(filters.IsoDateTimeFilter):
def filter(self, qs, value):
if value != None:
return qs.prefetch_related(Prefetch('coincosts_set', to_attr='filtered_coincosts', queryset=CoinCosts.objects.filter(timestamp__gte=value)
)
)
else:
return qs
class CoinCostFilterSet(filters.FilterSet):
timestamp = DateTimeGteFilter()
class Meta:
model = Coins
fields = {
'symbol': ['exact'],
}
You can use try and except like this in views.py in your def:
try:
obj = Entry.objects.all()[::5]
except IndexError:
obj = None
Related
I have two tables in my database to insert a product record. I am storing the product info in the Product table and the rest of the info like Pirce, Quantity etc storing to another table which is ProductStock.
I am planning to send the data to a server something like this.
{
"name":'product name',
"brand":"socialcodia"
"product_stock":{
"price":"100",
"quantity":"50"
}
}
I am easily able to validate the product info from ProductSerializer. But I don't have any perfect idea to validate the ProductStockSerializer data.
ProductSerializer
from rest_framework import serializers
from .models import Product
from .models import Medical
class ProductSerializer(serializers.ModelSerializer):
medical = serializers.CharField(read_only=True)
id = serializers.CharField(read_only=True)
is_visible = serializers.CharField(read_only=True,default=True)
class Meta:
model = Product
fields = ['id','medical','category','item','brand','is_visible']
def validate(self, attrs):
request = self.context.get('request')
attrs['medical'] = Medical.objects.get(pk=request.info.get('medical'))
return attrs
Here I want to validate the product_stock info as well. cuz it's coming with a single request. So is there any way that i can import the ProductStockSerializer into ProductSerializer and pass the data to that serializer. then validate it.
ProductStockSerializer
from rest_framework import serializers
from .models import ProductStock
from medical.models import Medical
class ProductStockSerializer(serializers.ModelSerializer):
medical = serializers.CharField(read_only=True)
class Meta:
model = ProductStock
fields = ['medical','distributer','product','variant','batch','purchase_price','price','quantity','location','low_stock','expire_date']
def validate(self, attrs):
attrs['medical'] = Medical.objects.get(self.context.get('request').info.get('medical'))
batch = attrs.get('batch')
purchase_price = attrs.get('purchase_price')
price = attrs.get('price'),
if len(batch) < 3 or len(batch) > 30:
raise serializers.ValidationError("Invalid Batch Number")
if type(purchase_price) != int or type(purchase_price) != float:
raise serializers.ValidationError("Invalid Purchase Price")
if type(price) != int or type(price) != float:
raise serializers.ValidationError("Invalid Price")
return attrs;
ProductViewSet
class ProductViewSet(ModelViewSet):
queryset = Product.objects.all()
serializer_class = ProductSerializer
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated]
def get_queryset(self):
if self.request.user.is_superuser:
return Product.objects.all()
return Product.objects.filter(medical=self.request.info['medical'])
I literally don't have any idea how to do this.
Thanks
How about adding your ProductStockSerializer data as a field to your ProductSerializer
class ProductSerializer(serializers.ModelSerializer):
...
product_stock = ProductStockSerializer()
class Meta:
fields = [ ... , product_stock ]
You should be able to use product_stock in your validation. If you plan to create ProductStock objects using this nested serializer please make sure to read on writable nested serializers.
The #coderiot answer is absolutely right.
Here's How I Solved, But it has a lot more code than the above answer.
class ProductSerializer(serializers.ModelSerializer):
medical = serializers.CharField(read_only=True)
id = serializers.CharField(read_only=True)
is_visible = serializers.CharField(read_only=True,default=True)
stock = serializers.SerializerMethodField(read_only=True)
class Meta:
model = Product
fields = ['stock','id','medical','category','item','brand','is_visible']
def get_stock(self,instance):
return ProductStockSerializer(instance=instance.stock,context=self.context).data
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
self.fields['stock'] = ProductStockSerializer(context=self.context)
def validate(self, attrs):
request = self.context.get('request')
attrs['medical'] = Medical.objects.get(pk=request.info.get('medical'))
return attrs
Can we pass the URL queryset parameter into serizalizer to have filitering/calculation?
here is my endpoint: ?start_date=20210312&end_date=20210317
herer is my viewset:
class BrandChartsViewSet(ModelViewSet):
serializer_class = BrandChartsSerializer
pagination_class = BrandChartsPagination
queryset = Brand.objects.all()
def get_queryset(self):
start_date = self.request.query_params.get('start_date',None)
end_date = self.request.query_params.get('end_date',None)
if start_date is None:
raise InvalidInputError()
if end_date is None:
raise InvalidInputError()
start_date_obj = datetime.strptime(start_date,'%Y%m%d')
end_date_obj = datetime.strptime(end_date,'%Y%m%d')
serializer = BrandChartsSerializer(start_date_obj,end_date_obj)
queryset = Brand.objects.all()
return queryset
here is my serizalizer:
class BrandChartsSerializer(serializers.ModelSerializer):
rankings = serializers.SerializerMethodField()
instagram = IGSerializer(allow_null=True)
facebook = FBSerializer(allow_null=True)
hashtags = BrandHashtagSerializer(allow_null=True, many=True)
# rename the json field
brand_uid = serializers.IntegerField(source='id')
brand_name = serializers.CharField(source='name')
origin = serializers.CharField(source="country")
class Meta:
model = Brand
fields = [
"brand_uid",
"brand_name",
"origin",
"rankings",
"instagram",
"facebook",
"hashtags",
]
def get_rankings(self, instance):
rank = instance.rankings.all().filter(created__gte=start_date_obj,
created__lte=end_date_obj
).order_by('created')
return BrandRankSerializer(rank, allow_null=True, many=True).data
and it will have Unresolved reference 'start_date_obj' in get_rankings, however I tried to passed the parameter into serizalizer by class BrandChartsSerializer(serializers.ModelSerializer,start_date_obj,end_date_obj)
It still giving error.
Due to my function design, I think it could not use DRF filter to handle it, could I just pass the parameter to serizalizer to do filitering? (I need to filter the rankings model in range and calculate the sum than return)
We can make use of context in such cases.
Try this.
Update the get_rankings method.
def get_rankings(self, instance):
start_date_obj = self.context.get("request").query_params.get("start_date")
end_date_obj = self.context.get("request").query_params.get("end_date")
rank = instance.rankings.all().filter(created__gte=start_date_obj,
created__lte=end_date_obj
).order_by('created')
return BrandRankSerializer(rank, allow_null=True, many=True).data
I'm coding some backend software for a second-hand selling app using Django and DjangoRestFramework. Right now, I'm trying to send a Response object that contains a list of products, but I seem not to be able to return an actual list of products, as I'm getting an error saying
ListSerializer is not JSON serializable.
I've tried both using the serializer constructor like this:
ProductoSerializer(products, many=True)
And by creating a list of ProductoSerializer.data and then creating the Response object with that.
Here's the serializers that I'm using:
class UserSerializer(serializers.HyperlinkedModelSerializer):
ciudad = serializers.SerializerMethodField()
conectado = serializers.SerializerMethodField()
class Meta:
model = Usuario
fields = ('uid', 'nombre', 'ciudad', 'conectado')
def get_ciudad(self, obj):
geolocator = Nominatim(user_agent="bookalo")
location = geolocator.reverse(str(obj.latitud_registro) + ',' + str(obj.longitud_registro))
return location.raw['address']['city']
def get_conectado(self, obj):
ahora = timezone.now()
result = relativedelta(ahora, obj.ultima_conexion)
return result.days == 0 and result.hours == 0 and result.months == 0 and result.years == 0 and result.minutes < 5
class TagSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Tag
fields = ('nombre')
class MultimediaSerializer(serializers.HyperlinkedModelSerializer):
contenido_url = serializers.SerializerMethodField()
class Meta:
model = ContenidoMultimedia
fields = ('contenido_url', 'orden_en_producto')
def get_contenido_url(self, obj):
return obj.contenido.url
class MiniProductoSerializer(serializers.HyperlinkedModelSerializer):
contenido_multimedia = serializers.SerializerMethodField()
class Meta:
model = Producto
fields = ('nombre', 'precio', 'estado_venta', 'contenido_multimedia')
def get_contenido_multimedia(self, obj):
contenido = ContenidoMultimedia.objects.get(producto=obj.pk, orden_en_producto=0)
return MultimediaSerializer(contenido)
class ProductoSerializer(serializers.HyperlinkedModelSerializer):
vendido_por = UserSerializer(read_only=True)
tiene_tags = TagSerializer(many=True, read_only=True)
contenido_multimedia = serializers.SerializerMethodField()
valoracion_media_usuario = serializers.SerializerMethodField()
class Meta:
model = Producto
fields = ('nombre', 'precio', 'estado_producto', 'estado_venta', 'latitud', 'longitud', 'tipo_envio', 'descripcion', 'vendido_por', 'tiene_tags', 'num_likes', 'contenido_multimedia')
def get_contenido_multimedia(self, obj):
contenido = ContenidoMultimedia.objects.filter(producto=obj.pk).order_by('orden_en_producto')
return MultimediaSerializer(contenido, many=True)
def get_valoracion_media_usuario(self, obj):
return Usuario.objects.get(pk=obj.vendido_por).media_valoraciones
class ValidacionEstrellaSerializer(serializers.HyperlinkedModelSerializer):
usuario_que_valora = UserSerializer(read_only=True)
producto_asociado = serializers.SerializerMethodField()
class Meta:
model = ValidacionEstrella
fields = ('estrellas', 'comentario', 'timestamp', 'usuario_que_valora', 'producto_asociado')
def get_producto_asociado(self, obj):
producto = Producto.objects.get(pk=obj.producto)
return MiniProductoSerializer(producto)
class UserProfileSerializer(serializers.HyperlinkedModelSerializer):
usuario_valorado_estrella = serializers.SerializerMethodField()
productos_favoritos = serializers.SerializerMethodField()
class Meta:
model = Usuario
fields = ('uid', 'nombre', 'esta_baneado', 'usuario_valorado_estrella', 'producto_del_usuario')
def get_usuario_valorado_estrella(self, obj):
validaciones = ValidacionEstrella.objects.filter(usuario_valorado=obj.pk).order_by('-timestamp')
return ValidacionEstrellaSerializer(validaciones, many=True, read_only=True)
def get_productos_favoritos(self, obj):
favoritos = Producto.objects.filter(le_gusta_a__in=[obj.pk])
return ProductoSerializer(favoritos, many=True, read_only=True)
class ReportSerializer(serializers.HyperlinkedModelSerializer):
#usuario_reportado = serializers.SerializerMethodField()
usuario_reportado = UserSerializer(read_only=True)
class Meta:
model = Report
fields = ('usuario_reportado', 'causa')
And here's the views.py function that I'm trying to code:
#api_view(['POST'])
#permission_classes((permissions.AllowAny,))
def SearchProduct(request, format=None):
if request.method != 'POST':
return Response(status=status.HTTP_400_BAD_REQUEST)
preposiciones = ['a','ante','bajo','cabe','con','contra','de','desde','en','entre',
'hacia','hasta','para','por','segun','sin','so','sobre','tras']
try:
search = request.POST.get('busqueda')
except:
return Response(status=status.HTTP_404_NOT_FOUND)
products = Producto.objects.none()
for word in search.split():
if word not in preposiciones:
productos_palabra = Producto.objects.filter(nombre__contains=word)
products = products | productos_palabra
products.distinct()
product_list = []
for prod in products:
product_list.append(ProductoSerializer(prod).data)
return Response({'productos': product_list}, status=status.HTTP_200_OK)
I'm using a request object because I also have to server a WebPage, and not only a mobile app, with the same function (the webpage part is still not coded though).
It should return all the products that contain at least one of the words from the user's search, and it all should be structured based on the ProductoSerializer object, but for some reason, it's outputting that error and I'm not quite sure how to fix it.
Thanks in advance, and if you need any extra information which I've missed, please do ask for it... It's been a long day and I probably missed something.
Seems like when you use SerializerMethodField you return serializer instance but not it's data:
For instance:
contenido_multimedia = serializers.SerializerMethodField()
def get_contenido_multimedia(self, obj):
contenido = ContenidoMultimedia.objects.filter(producto=obj.pk).order_by('orden_en_producto')
return MultimediaSerializer(contenido, many=True).data # <-- here try to add .data
It should be changed for all SerializerMethodField methods.
I would like to add an additional field for user's details. But it has to use another value outside of database fields.
For more clearly like this
model:
class User(models.Model):
id= models.AutoField(primary_key=True)
last_name= models.CharField(max_length=20)
first_name=models.CharField(max_length=20)
role_id = models.IntegerField()
serializer:
class UserSerializer(serializers.ModelSerializer):
display_summary = serializers.SerializerMethodField()
login_user_id = serializers.IntegerField(required=False)
class Meta:
model = User
fields = ("id","last_name","first_name", "display_summary", "login_user_id")
def get_display_summary(self, obj):
login_id = self.validated_data.get('login_user_id', None)
login_user = User.objects.filter(pk=login_id).first()
if obj.role_id==2 and login_user.role_id==1:
return 1
return 0
So in my views, when getting just one user, it's all ok:
#api_view(['GET'])
#login_required
def get_user(request, login_user, user_id):
serializer = UserSerializer(User.objects.get(pk=user_id), data={'login_user_id': login_user.id})
if serializer.is_valid():
result = serializer.data
return Response(result, status=status.HTTP_200_OK)
#result:
#{
# "id": 2,
# "last_name": "V",
# "first_name": "Lich",
# "role_id": 2,
# "display_summary": 1
#}
But when I need to return a list, how can I add additional data (login_user_id)?
This is not working:
users = User.objects.filter(last_name__icontains='v')
result_serializer = UserSerializer(users, data={'login_user_id': login_user.id}, many=True)
return result_serializer.data
The error occur say that it's looking for a list, not a dict for inputted param.
Based on #Hai Lang 's idea, I made a new namedtuple to add more info. Something like this:
Serializer:
No use ModelSerializer, use normal Serializer with field I would like to show on this view
class UserSummarySerializer(serializers.Serializer):
user_id = serializers.SerializerMethodField()
last_name = serializers.SerializerMethodField()
first_name = serializers.SerializerMethodField()
display_summary = serializers.SerializerMethodField()
def validate(self, data):
#some validation if needed
return data
def get_user_id(self, obj):
return obj.user.user_id
def get_last_name(self, obj):
return obj.user.last_name
def get_first_name(self, obj):
return obj.user.first_name
def get_display_summary(self, obj):
login_user = obj.login_user
if obj.role_id==2 and login_user.role_id==1:
return 1
return 0
then in my views:
userSummary = namedtuple('userSummary ', ('user', 'login_user'))
users = User.objects.filter(last_name__icontains='v')
objs = [userSummary(u, login_user) for u in users]
result_serializer = UserSerializer(objs, many=True)
return result_serializer.data
Let's say I have a data model like so:
Apple->Fruit->Organic
so Apple has a foreign key to Fruit...
and Organic has 3 fields: name, title, level.
I'm displaying the 3 fields in a single column as name_title_level.
I want to be able to sort the result by clicking on the table header.
I've looked at:
https://djangosnippets.org/snippets/2110/
and tried it:
class SpecialOrderingChangeList(ChangeList):
def apply_special_ordering(self, queryset):
order_type, order_by = [self.params.get(param, None) for param in ('ot', 'o')]
special_ordering = self.model_admin.special_ordering
if special_ordering and order_type and order_by:
try:
order_field = self.list_display[int(order_by)]
ordering = special_ordering[order_field]
if order_type == 'desc':
ordering = ['-' + field for field in ordering]
queryset = queryset.order_by(*ordering)
except IndexError:
return queryset
except KeyError:
return queryset
return queryset
def get_query_set(self):
queryset = super(SpecialOrderingChangeList, self).get_query_set()
queryset = self.apply_special_ordering(queryset)
return queryset
#admin.register(Apple)
class AppleAdmin(admin.ModelAdmin):
list_display = ('x', 'get_name')
def get_name(self, obj):
return "{}_{}_{}".format(obj.fruit.organic.name,\
obj.fruit.organic.title, obj.fruit.organic.level)
special_ordering = {'name': ('fruit__organic__name', 'fruit__organic__title', 'fruit__organic__level')}
def get_changelist(self, request, **kwargs):
return SpecialOrderingChangeList
I'm not getting any error and the sort feature is not doing anything.
The get_query_set method is not being called.
Does anyone know how to do this?
Updates:
Here is the updated code. Now the method gets called but still there is no sorting functionality. I mean there is no link in the header at all.
models.py:
from django.db import models
class Organic(models.Model):
name = models.CharField(max_length=30)
title = models.CharField(max_length=30)
label = models.CharField(max_length=30)
def __unicode__(self):
return self.name
class Fruit(models.Model):
organic = models.ForeignKey(Organic)
def __unicode__(self):
return self.organic.name
class Apple(models.Model):
fruit = models.ForeignKey(Fruit)
color = models.CharField(max_length=30)
def __unicode__(self):
return self.color
admin.py:
from django.contrib import admin
from .models import *
from django.contrib.admin.views.main import ChangeList
#admin.register(Organic)
class OrganicAdmin(admin.ModelAdmin):
pass
#admin.register(Fruit)
class FruitAdmin(admin.ModelAdmin):
pass
class SpecialOrderingChangeList(ChangeList):
def apply_special_ordering(self, queryset):
order_type, order_by = [self.params.get(param, None) for param in ('ot', 'o')]
special_ordering = self.model_admin.special_ordering
if special_ordering and order_type and order_by:
try:
order_field = self.list_display[int(order_by)]
ordering = special_ordering[order_field]
if order_type == 'desc':
ordering = ['-' + field for field in ordering]
queryset = queryset.order_by(*ordering)
except IndexError:
return queryset
except KeyError:
return queryset
return queryset
def get_queryset(self, request):
queryset = super(SpecialOrderingChangeList, self).get_queryset(request)
queryset = self.apply_special_ordering(queryset)
return queryset
#admin.register(Apple)
class AppleAdmin(admin.ModelAdmin):
list_display = ('color', 'get_name')
def get_name(self, obj):
return "{}_{}_{}".format(obj.fruit.organic.name,\
obj.fruit.organic.title, obj.fruit.organic.label)
special_ordering = {'name': ('fruit__organic__name', 'fruit__organic__title', 'fruit__organic__label')}
def get_changelist(self, request, **kwargs):
return SpecialOrderingChangeList
it should be
get_queryset()
not
get_query_set()