serialization

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

I want to group an entity based on one foreign key relation in rest django

I want to group an entity based on one foreign key relation in rest django Question: I have two model here and records are related to employee: class Employee(models.Model): name = models.CharField(max_length=100) position = models.CharField(max_length=100) site = models.CharField(max_length=100) wage = models.DecimalField(max_digits=4, decimal_places=0, default=0) class Record(models.Model): employee = models.ForeignKey(Employee, related_name=’employee’, on_delete=models.DO_NOTHING) date = models.DateField() cash = …

Total answers: 1

pass a parameter into serilazer under ListModelMixin

pass a parameter into serilazer under ListModelMixin Question: I am passing a parameter to a serilaizer like this: serializer = AttractionTicketSerializer(attraction, context={‘api_consumer’:request.auth.application}) I have a view which inherits from ListModelMixin, I need to pass this context param to the serilizer as well. here is a summarized view: class AttractionView(mixins.ListModelMixin, generics.GenericAPIView): authentication_classes = AUTHENTICATION_CLASSES permission_classes = …

Total answers: 1

Serial.serial is not working, thonny: module 'serial' has no attribute 'Serial'

Serial.serial is not working, thonny: module 'serial' has no attribute 'Serial' Question: I just started with Ardunino, Python and a serial Communication between RPI and Ardunio (using Thonny). I checked out some tutorials (e.g https://roboticsbackend.com/raspberry-pi-arduino-serial-communication/) to get my code run. But not even the standard code is running. That is my code: if __name__ == …

Total answers: 1

Nested Serializer save post foreign key in django

Nested Serializer save post foreign key in django Question: In my project, the relationship between Product and Group is ManytoOne. When I tried to post a new product, it cannot work. I’m sure there are more issues with this code and I will appreciate a detailed answer, because I am new to Django and Python. …

Total answers: 1

Why am I getting a IntegrityError at, null value in a column that doesn't exists… Django

Why am I getting a IntegrityError at, null value in a column that doesn't exists… Django Question: I am trying to hit an external api, when a user submits a form. I am using Django and Postgresql My Model class League_Mod(models.Model): host = models.CharField(max_length=50) Espn_League_Id = models.IntegerField(unique = True) Espn_S2 = models.CharField(max_length=3000) Espn_Swid = models.CharField(max_length=300) …

Total answers: 1

How to properly update a many to many nested serializer?

How to properly update a many to many nested serializer? Question: I have been able to replicate the create method to add the correct nested serializers in a POST request. However, I’m still having issues updating in a PUT or PATCH. When using a PUT or PATCH request and I pass the entire object data …

Total answers: 2