serialization

Convert string that was originally a `timedelta` back into a `timedelta` object in Python

Convert string that was originally a `timedelta` back into a `timedelta` object in Python Question: I have strings which were originally produced from timedelta objects like so: print(f'{my_delta}’). I have many of these statements logged (e.g. "12:21:00", "1 day, 0:53:00", "2 days, 9:28:00") and I simply want to parse these logged statements to convert back …

Total answers: 3

Pydantic BaseModel schema and instance serialization/deserialization not working

Pydantic BaseModel schema and instance serialization/deserialization not working Question: I am attempting to serialize a Pydantic model schema and then deserialize it in another script. The serialization process is working as expected, and it has created two JSON files: model.json and data.json. In test_save.py, I defined the MainModel schema and then serialized it along with …

Total answers: 2

How to get specific objects based on ManyToMany field match

How to get specific objects based on ManyToMany field match Question: I’m doing a cookbook app, which help users find meal thay can do with their ingridients. I’m using Django RestFramework, and i need to return list of avaliable meals that user can do, but don’t know how to do search by ingridients My models.py: …

Total answers: 1

Change json.dumps behaviour : customize serialization

Change json.dumps behaviour : customize serialization Question: Imagine, I’ve got a dict {"a": "hello", "b": b"list"} ‘a’ is a string ‘b’ is a byte string I would like to serialize the dict into the "json"(*) string –> ‘{"a": "hello", "b": list}’ (*) : not really json compliant For that, i’ve written that method, it works …

Total answers: 1

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