I don't know how to merge all refetch_related objects from MeasurememtResult table to one list objects:
models.py
class DeviceMeasurement(models.Model):
patient = models.ForeignKey(Patient, blank=True, null=True, on_delete=models.CASCADE)
device = models.ForeignKey(Device, on_delete=models.CASCADE)
created_date = models.DateTimeField()
front_id = models.UUIDField(blank=True, null=True)
class MeasurememtResult(models.Model):
measurement_result = models.FloatField()
measurement_type = models.CharField(choices=MEASUREMENT_TYPES, max_length=30)
device_measurement_id = models.ForeignKey(DeviceMeasurement, related_name='res_data', on_delete=models.CASCADE)
views.py
class GetMeasurements(viewsets.GenericViewSet, mixins.ListModelMixin):
permission_classes = (IsAuthenticated,)
serializer_class = GetMeasurementsSerializer
queryset = DeviceMeasurement.objects.values('patient__first_name', 'device__id', 'created_date', 'front_id',
'results__measurement_result',
'results__measurement_type').prefetch_related('res_data').all()
serializers.py
class GetMeasurementsSerializer(serializers.Serializer):
created_date = serializers.DateTimeField()
front_id = serializers.UUIDField()
patient__first_name = serializers.CharField()
device__id = serializers.IntegerField()
results__measurement_result = serializers.IntegerField()
results__measurement_type = serializers.CharField() `
So, I got Response like as:
[
{
"created_date": "2020-06-05T15:03:22.481032+03:00",
"front_id": null,
"patient__first_name": "Alex",
"device__id": 8,
"results__measurement_result": 100,
"results__measurement_type": "blood_pressure_SYS"
},
{
"created_date": "2020-06-05T15:03:22.481032+03:00",
"front_id": null,
"patient__first_name": "Alex",
"device__id": 8,
"results__measurement_result": 120,
"results__measurement_type": "blood_pressure_DIA"
}
]
But I should return list of MeasurememtResult objects like as:
In 2 objects i have equal values of device_id.
{
"created_date": "2020-06-05T15:03:22.481032+03:00",
"front_id": null,
"patient__first_name": "Alex",
"device__id": 8,
"res_data": [
{
"results__measurement_result": 120,
"results__measurement_type": "blood_pressure_DIA"
},
{
"results__measurement_result": 100,
"results__measurement_type": "blood_pressure_SYS"
}
]
}
Prefetch related is for database query optimization . It has no relation with how your response is structured .
To return queryset with distinct device id ,
queryset = DeviceMeasurement.objects.all().distinct("device").prefetch_related('res_data')
Note that the distinct("fieldname") will only work if you using postgres database .Please research on how to get it working in your database.
Select DISTINCT individual columns in django?
Then in your serializer:
class MeasurementResultSerializer(serializers.ModelSerializer):
class Meta:
model = MeasurememtResult
fields = "__all__"
read_only_fields = fields
class GetMeasurementsSerializer(serializers.Serializer):
res_data = serializers.SerializerMethodField()
created_date = serializers.DateTimeField()
front_id = serializers.UUIDField()
patient__first_name = serializers.CharField()
device__id = serializers.IntegerField()
def get_res_data(self, instance):
return MeasurementResultSerializer(instance.res_data.all(), many=True).data
You can edit this with your required fields , Inside fields array you can give fields like :
fields = ["measurement_result", "measurement_type"]
Related
I'm using Django RestFramework to create a simple eCommerce API where one product could have many images and I would like to get the URLs of all these images on a json field.
For now, I got the first image url using "imagembicicleta_set.all.first.image.url" on the serializer, but I need all URLs list:
{
"count": 7,
"next": null,
"previous": null,
"results": [
{
"id": 1,
"nome": "Specialized Roubaix",
"marca__nome": "Specialized",
"categoria": "Bicicletas de Estrada",
"atividades": [
1
],
"terrenos": [
"Asfalto"
],
"preco": "16999.00",
"ano": 1,
"absolute_url": "/bicicletas/Specialized/specialized-roubaix-2020/",
"img_url": "/media/images/bicicletas/roubaix1.jpeg"
},
{
"id": 2,
"nome": "Specialized Roubaix Sport",
"marca__nome": "Specialized",
Following how is my setup:
Models.py
class Bicicleta(models.Model):
id = models.AutoField(primary_key=True)
nome = models.CharField(max_length=200, blank=False, null=False)
slug = models.SlugField(unique=True)
status = models.IntegerField(choices=STATUS_CHOICES, default=1, blank=False, null=False)
descricao = RichTextField(blank=True, null=True)
marca = models.ForeignKey(MarcaBicicleta, blank=True, null=True, on_delete=models.SET_NULL)
...
class ImagemBicicleta (models.Model):
bicicleta = models.ForeignKey(Bicicleta, default=None, on_delete=models.CASCADE)
image = models.ImageField(upload_to='images/bicicletas')
Serializer.py
class BicicletasSerializer(serializers.ModelSerializer):
marca__nome = serializers.CharField(source='marca.nome')
categoria = serializers.CharField(source='categoria.nome')
terrenos = serializers.StringRelatedField(many=True)
absolute_url = serializers.URLField(source='get_absolute_url', read_only=True)
img_url = serializers.URLField(source='imagembicicleta_set.all.first.image.url', read_only=True) #I could get the first image using this
class Meta:
model = Bicicleta
fields = ['id', 'nome', 'marca__nome', 'categoria', 'atividades', 'terrenos', 'preco', 'ano', 'absolute_url', 'img_url']
views.py
class BicicletasView(generics.ListAPIView):
serializer_class = BicicletasSerializer
queryset = Bicicleta.objects.all()
filter_backends = (DjangoFilterBackend, SearchFilter)
filterset_fields = ['marca', 'terrenos', 'status']
search_fields = {'nome': ['icontains'], }
How could I get all images URLs in the field?
Per example, if a product has 3 different images, I would expect to have the img field like this:
"img_url": [ "/media/images/bicicletas/roubaix1.jpeg","/media/images/bicicletas/roubaix2.jpeg","/media/images/bicicletas/roubaix3.jpeg" ],
You can add a method serializer which will collect all the urls for each individual object like this:
class BicicletasSerializer(serializers.ModelSerializer):
marca__nome = serializers.CharField(source='marca.nome')
categoria = serializers.CharField(source='categoria.nome')
terrenos = serializers.StringRelatedField(many=True)
absolute_url = serializers.URLField(source='get_absolute_url', read_only=True)
img_url = serializers.SerializerMethodField()
def get_image_url(self , instance):
return ImagemBicicleta.objects.filter(bicicleta=instance).values_list('image',flat=True)
class Meta:
model = Bicicleta
fields = ['id', 'nome', 'marca__nome', 'categoria', 'atividades', 'terrenos', 'preco', 'ano', 'absolute_url', 'img_url']
I have seen many tutorials about nested serializer, but unfortunately I can`t solve this task. Please, give me some tips.
I need to create this JSON
{
"external_id": "11",
"details": [
{
"amount": 7,
"price": "12.00",
"product": {
"name": "Car"
}
}
]
}
My models consist the next relative:
from django.db import models
class Order(models.Model):
NEW = 'new'
ACCEPTED = 'accepted'
FAILED = 'failed'
order_status = [
(NEW, 'new'),
(ACCEPTED, 'accepted'),
(FAILED, 'failed'),
]
status = models.CharField(max_length=12, choices=order_status, default='new', blank=False)
created_at = models.DateTimeField(auto_now_add=True)
external_id = models.CharField(max_length=128)
def __str__(self):
return f'Order № {self.external_id}'
class Product(models.Model):
name = models.CharField(max_length=64)
def __str__(self):
return self.name
class OrderDetail(models.Model):
order = models.ForeignKey(Order, on_delete=models.CASCADE,
related_name='details',
null=True, blank=True)
amount = models.IntegerField(null=True, blank=True)
product = models.ForeignKey(Product, on_delete=models.CASCADE,
related_name='product',
null=True)
price = models.DecimalField(decimal_places=2, max_digits=6, null=True, blank=True)
def __str__(self):
return f'Detail for {self.order}, detail for product {self.product}'
My view
class ProductViewSet(ModelViewSet):
queryset = Product.objects.all()
serializer_class = ProductSerializer
class OrderViewSet(ModelViewSet):
queryset = Order.objects.all()
serializer_class = OrderSerializer
pagination_class = ContentRangeHeaderPagination
class OrderDetailViewSet(ModelViewSet):
queryset = OrderDetail.objects.all()
serializer_class = OrderDetailSerializer
My serializer
class OrderDetailSerializer(serializers.ModelSerializer):
class Meta:
model = OrderDetail
fields = ['id', 'amount', 'price']
depth = 1
class ProductSerializer(serializers.ModelSerializer):
product = OrderDetailSerializer(many=True)
class Meta:
model = Product
fields = ['id', 'name', 'product']
class OrderSerializer(serializers.ModelSerializer):
details = OrderDetailSerializer(many=True)
class Meta:
model = Order
fields = ['id', 'status', 'created_at', 'external_id', 'details']
depth = 2
def create(self, validated_data): # works for first nesting
print(validated_data)
details_data = validated_data.pop('details')
request = Order.objects.create(**validated_data)
for detail_data in details_data: #
products_data = detail_data.pop('product')
request_detail = OrderDetail.objects.create(order=request, **detail_data)
for product_data in products_data:
Product.objects.create(product=request_detail, **product_data)
return request
I have errors when I try to send POST request. => KeyError 'products'
I wanted to get product fields using a loop. But I can't get this field, because I didn't identified it.
My question is: how to receive this field in OrderSerializer.
Thanks for your answers.
I'm trying to make an API for a butcher.
With this API and the website that I will build by the next, the client will be able to make his order remotly.
Here is my probleme.
With the order form, the client send me JSON data like here :
{
"user": 8,
"orderDay": "2020-06-24",
"deliveryDay": "2020-06-30",
"deliveryAddress": "Place des Fêtes",
"comment": "",
"orderDetail":
[
{
"product": 2,
"byProduct": 2,
"quantity": 43
},
{
"product": 3,
"byProduct": 3,
"quantity": 5
}
]
}
These data must be saved in the database.
These are the models that I use : models.py
class order(models.Model):
user = models.ForeignKey(memberArea, on_delete=models.CASCADE)
comment = models.TextField(null=True, blank=True)
orderDay = models.DateTimeField(auto_now_add=True)
deliveryDay = models.DateField()
deliveryAddress = models.CharField(max_length=255)
state = models.CharField(max_length=255)
price = models.TextField(null=True, blank=True)
response = models.TextField(null=True, blank=True)
class orderDetail(models.Model):
order = models.ForeignKey(order, on_delete=models.CASCADE)
product = models.ForeignKey(product, on_delete=models.CASCADE)
byProduct = models.ForeignKey(byProduct, on_delete=models.CASCADE)
quantity = models.CharField(max_length=255)
class product(models.Model):
name = models.CharField(max_length=255)
prix_uni = models.TextField(null=True, blank=True)
prix_kg = models.TextField(null=True, blank=True)
dispo = models.BooleanField(null=True, blank=True)
category = models.ForeignKey(category, on_delete=models.CASCADE)
redu = models.TextField(null=True, blank=True)
class byProduct(models.Model):
product = models.ForeignKey(product, on_delete = models.CASCADE)
name = models.CharField(max_length=255)
I make a serializer file like this serializer.py
class orderDetailSerializer(serializers.ModelSerializer):
order = serializers.PrimaryKeyRelatedField(many=False, queryset = order.objects.all())
class Meta:
model = orderDetail
fields = '__all__'
class OrderSerializer(serializers.ModelSerializer):
orderDetail = orderDetailSerializer(many=True)
class Meta:
model = order
fields = ['user', 'comment', 'deliveryAddress', 'deliveryDay', 'orderDetail']
def create(self, validated_data):
order_detail_data = validated_data.pop('orderDetail')
new_order = order.objects.create(**validated_data)
new_order.save()
for product in order_detail_data:
order_detail = orderDetail.objects.create(order=new_order, **product)
new_order.orderDetail.add(order_detail.id)
return new_order
And this is my view : views.py:
#Make an order
#api_view(['POST'])
def order(request, format=None):
if request.method == 'POST':
serializer = OrderSerializer(data=request.data)
data = {}
if serializer.is_valid():
serializer.save()
data['response'] = "Your order went well"
return Response(data)
return Response(serializer.errors)
When I try to run my code, it tells me that the order data is missing :
{
"orderDetail": [
{
"order": [
"This field is required."
]
},
{
"order": [
"This field is required."
]
}
]
}
I don't know how to add this because the order_id that I need is created at the same time that the orderDetail.
Thank's by advance for helping me.
you should make order field readonly in orderDetailSerializer:
class orderDetailSerializer(serializers.ModelSerializer):
class Meta:
model = orderDetail
fields = '__all__'
read_only_fields = ('order',)
I am trying to use nested writable serializer in django-rest-framework. When I send a POST request with following data:
{
"acc_name": "Salary Card",
"acc_type_id": {
"id": 2,
"acc_type_name": "Debit Card"
},
"credit_amt": null,
"bill_dt": null,
"due_dt": null,
"balance": "0.00",
"comments": null
}
I got an error:
Cannot assign "OrderedDict([('acc_type_name', 'Debit Card')])": "Accounts.acc_type_id" must be a "AccountTypes" instance.
I actually passed id for AccountTypes, but why restframework remove it automatically? How can I resolve this problem? How can I create a new account with existing account type?
Views:
class AccountTypesViewSet(ListAPIView):
name = __name__
pagination_class = None
queryset = AccountTypes.objects.filter(active='Y')
serializer_class = AccountTypesSerializer
class AccountsViewSet(viewsets.ModelViewSet):
pagination_class = None
queryset = Accounts.objects.all()
serializer_class = AccountsSerializer
Models:
class AccountTypes(models.Model):
id = models.AutoField(primary_key=True, editable=False)
acc_type_name = models.CharField(max_length=50)
active = models.CharField(max_length=1, default='Y')
class Meta:
db_table = f'"{schema}"."taccount_types"'
managed = False
class Accounts(models.Model):
id = models.AutoField(primary_key=True, editable=False)
acc_name = models.CharField(max_length=1024)
acc_type_id = models.ForeignKey(
to=AccountTypes,
db_column='acc_type_id',
related_name='accounts',
on_delete=models.DO_NOTHING
)
credit_amt = models.DecimalField(max_digits=11, decimal_places=2, null=True)
bill_dt = models.DateField(null=True)
due_dt = models.DateField(null=True)
balance = models.DecimalField(max_digits=11, decimal_places=2, default=0)
comments = models.CharField(max_length=1024, null=True)
class Meta:
db_table = f'"{schema}"."taccounts"'
managed = False
Serializers:
class AccountTypesSerializer(serializers.ModelSerializer):
class Meta:
model = AccountTypes
fields = ('id', 'acc_type_name')
class AccountsSerializer(serializers.ModelSerializer):
# acc_type = serializers.SlugRelatedField(slug_field='acc_type_name', source='acc_type_id', read_only=True)
acc_type_id = AccountTypesSerializer()
class Meta:
model = Accounts
fields = ('id', 'acc_name', 'acc_type_id', 'credit_amt',
'bill_dt', 'due_dt', 'balance', 'comments')
def create(self, validated_data):
accounts_instance = Accounts.objects.create(**validated_data)
return accounts_instance
You don't need to set id field because Django will do it by itself, so remove that lines:
id = models.AutoField(primary_key=True, editable=False)
Next, you need to send an id of AccountTypes model to create new account and you give Django a dict fields = ('id', 'acc_type_name'). It doesn't know what to do with that. So just send id as number. And you don't need AccountTypesSerializer and that line acc_type_id = AccountTypesSerializer() also.
Next, you should create account in view, not in serializer, and you have to save it after creating.
I cannot save multiple values for the Foreignkey field when adding instances to the database. I don't understand exactly what the problem is: in my code or in the format of the JSON object being passed.
models.py
class VendorContacts(models.Model):
contact_id = models.AutoField(primary_key=True)
vendor = models.OneToOneField('Vendors', on_delete=models.CASCADE)
contact_name = models.CharField(max_length=45, blank=True)
phone = models.CharField(max_length=45, blank=True)
email = models.CharField(max_length=80, blank=True, unique=True)
class Meta:
db_table = 'vendor_contacts'
class VendorModuleNames(models.Model):
vendor = models.OneToOneField('Vendors', on_delete=models.CASCADE, primary_key=True)
module = models.ForeignKey(Modules, models.DO_NOTHING)
timestamp = models.DateTimeField(auto_now=True)
class Meta:
db_table = 'vendor_module_names'
unique_together = (('vendor', 'module'),)
class Vendors(models.Model):
COUNTRY_CHOICES = tuple(COUNTRIES)
vendorid = models.AutoField(primary_key=True)
vendor_name = models.CharField(max_length=45, unique=True)
country = models.CharField(max_length=45, choices=COUNTRY_CHOICES)
nda = models.DateField(blank=True, null=True)
user_id = models.ForeignKey('c_users.CustomUser', on_delete=models.PROTECT)
timestamp = models.DateTimeField(auto_now_add=True)
class Meta:
db_table = 'vendors'
unique_together = (('vendorid', 'timestamp'),)
serializers.py
class VendorsSerializer(serializers.ModelSerializer):
class Meta:
model = Vendors
fields = ('vendor_name',
'country',
'nda',
'parent_vendor',)
class VendorContactSerializer(serializers.ModelSerializer):
class Meta:
model = VendorContacts
fields = (
'contact_name',
'phone',
'email',)
class VendorModulSerializer(serializers.ModelSerializer):
class Meta:
model = VendorModuleNames
fields = ('module',)
views.py
class VendorsCreateView(APIView):
"""Create new vendor instances from form"""
serializer_class = (VendorsSerializer)
def post(self, request, *args, **kwargs):
vendor_serializer = VendorsSerializer(data=request.data)
vendor_contact_serializer = VendorContactSerializer(data=request.data)
vendor_modules_serializer = VendorModulSerializer(data=request.data)
try:
vendor_serializer.is_valid(raise_exception=True) \
and vendor_contact_serializer.is_valid(raise_exception=True) \
and vendor_modules_serializer.is_valid(raise_exception=True) \
vendor = vendor_serializer.save(user_id=request.user)
vendor_contact_serializer.save(vendor=vendor)
vendor_modules_serializer.save(module= maybe something here?????, vendor=vendor)
except ValidationError:
return Response({"errors": (vendor_serializer.errors,
vendor_contact_serializer.errors,
vendor_modules_serializer.errors
)},
status=status.HTTP_400_BAD_REQUEST)
else:
return Response(request.data, status=status.HTTP_200_OK)
JSON body
{
"vendor_name": "Awrazofgsxsdsxjwszsslsasdjegdzasas",
"country": "Canada",
"module": [
1,
2
],
"NDA date": "",
"contact_name": "Tim",
"email": "teaszt#tesstd.gmail",
"phone": "+3464784594940",
"parent_vendor": "23"
}
When I send JSON, I get the response
{
"module": [
"Incorrect type. Expected pk value, received list."
]
}
Looks like I'm finally confused about multiple saving
Your ForeignKey should be set on the related class Modules.