serialization

DRF, get foreignkey objects using option

DRF, get foreignkey objects using option Question: I’m trying to make backend using DRF, and I just faced a problem. models.py: class SchoolYear(models.Model): title = models.CharField(max_length=255, unique=True) class Student(models.Model): name = models.CharField(max_length=10) school_year = models.ForeignKey( "students.SchoolYear", related_name="school_year", on_delete=models.CASCADE, ) class StudentSerializer(serializers.ModelSerializer): class Meta: model = Student fields = ‘__all__’ Result from POSTMAN when using the …

Total answers: 1

DRF, why my root api urls are got mixed (combined)?

DRF, why my root api urls are got mixed (combined)? Question: These are my code snippets: # serializers.py from rest_framework import serializers from .models import User class UserSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = User fields = ‘__all__’ class UserActivitySerializer(serializers.HyperlinkedModelSerializer): class Meta: model = User fields = ( ‘id’, ’email’, ‘last_login’, ‘last_requested_at’, ) # urls.py router = …

Total answers: 1

Serialize a Class Object using vars()?

Serialize a Class Object using vars()? Question: beginner with Python here ! When serializing an instance in Python I tried using vars(), which returns returns the dict of said instance. class Foo: def __init__(self, doc_id, name): self.doc_id = doc_id self.name = name user1 = Foo(3, "Eddy") serialized = vars(user1) print("user1", user1.__dict__) print("serialized", serialized) So far …

Total answers: 2

Only Owner of the Profile able to Update the data

Only Owner of the Profile able to Update the data Question: Using class Based (APIView) in Django rest framework for Getting and Patch (Updating) UserInfo data. views.py class getUserInfo(APIView): permission_classes = [permissions.IsAuthenticated] def get(self, request, format=None): user = request.user userinfos = user.userinfo_set.all() serializer = UserInfoSerializers(userinfos, many=True) return Response(serializer.data) def patch(self, request, pk, format=None): user = …

Total answers: 2

How to serialize data and fix the wrong one in Python

How to serialize data and fix the wrong one in Python? Question: I’m new to Python. I need to serialize the data by the first two chars is taken from the first and the last char in the given string plus the transposed number just like this one: Input: ["HOMAGE", "DESIGN", "PROTECTION", "COMPANY"] Output: ["HE01", …

Total answers: 1

Django: normalize/modify a field in model serializer

Django: normalize/modify a field in model serializer Question: I have a model serializer like this: class FoooSerializers(serializers.ModelSerializer): class Meta: model = Food fields = [ ‘id’, ‘price’,] Here I have the price with trailing zeros like this: 50.000 and I want to .normalize() to remove the trailing zeros from it. Is this possible to do …

Total answers: 1

How to serialize custom classes in Python?

How to serialize custom classes in Python? Question: I have a custom class and I want to serialize it for multiprocessing, but pickle and dill doesn’t work fine and loses important data. How can I fix this? My class: import pickle import dill import pandas as pd import numpy as np class C(pd.Series): def __init__(self, …

Total answers: 1

Serializing two CharFields into one object

Serializing two CharFields into one object Question: I have serializer like this below. Now I have separated fields for store_photo and store_name. class ProductSerializer(serializers.ModelSerializer): store_photo = serializers.CharField(source=’store.photo’, read_only=True) store_name = serializers.CharField(source=’store.name’, read_only=True) class Meta: model = Product fields = [‘store_photo’, ‘store_name’, …] Can I somehow serialize it together in one object? I mean something like …

Total answers: 1

json.dumps() encodes StrEnum with custom __new__ to empty string

json.dumps() encodes StrEnum with custom __new__ to empty string Question: I have an enum similar to the following: from enum import Enum class Currencies(str, Enum): EURO = ("EUR", True) YEN = ("JPY", False) supports_decimals: bool def __new__(cls, value: str, supports_decimals: bool): obj = super().__new__(cls) obj._value_ = value setattr(obj, "supports_decimals", supports_decimals) return obj This allows things …

Total answers: 1

Pydantic model parse pascal case fields to snake case

Pydantic model parse pascal case fields to snake case Question: I have a Pydantic class model that represents a foreign API that looks like this: class Position(BaseModel): AccountID: str AveragePrice: str AssetType: str Last: str Bid: str Ask: str ConversionRate: str DayTradeRequirement: str InitialRequirement: str PositionID: str LongShort: str Quantity: int Symbol: str Timestamp: str …

Total answers: 1