serialization

TypeError: Object of type 'datetime' is not JSON serializable (with serialize function)

TypeError: Object of type 'datetime' is not JSON serializable (with serialize function) Question: I’m I getting this TypeError: Object of type ‘datetime’ is not JSON serializable error, even though I have a specific serialize function described in my model. This is my code: Flask route (rendered by React): menus.py @menus_bp.route(‘/menus’, methods=[‘GET’, ‘POST’]) def menus(): response_object …

Total answers: 3

How can I artificially nest schemas in Marshmallow?

How can I artificially nest schemas in Marshmallow? Question: In Marshmallow, is there a way to pass the current object to a Nested field in order to produce artificially nested serializations? For example, consider this object that I’m serializing: example = Example( name=”Foo”, address=”301 Elm Street”, city=”Kalamazoo”, state=”MI”, ) I want to produce JSON for …

Total answers: 3

Creating nested dataclass objects in Python

Creating nested dataclass objects in Python Question: I have a dataclass object that has nested dataclass objects in it. However, when I create the main object, the nested objects turn into a dictionary: @dataclass class One: f_one: int f_two: str @dataclass class Two: f_three: str f_four: One Two(**{‘f_three’: ‘three’, ‘f_four’: {‘f_one’: 1, ‘f_two’: ‘two’}}) Two(f_three=’three’, …

Total answers: 12

Send and receive objects through sockets in Python

Send and receive objects through sockets in Python Question: I have searched a lot on the Internet, but I haven’t been able to find the solution to send an object over the socket and receive it as is. I know it needs pickling which I have already done. And that converts it to bytes and …

Total answers: 3

Key: value store in Python for possibly 100 GB of data, without client/server

Key: value store in Python for possibly 100 GB of data, without client/server Question: There are many solutions to serialize a small dictionary: json.loads/json.dumps, pickle, shelve, ujson, or even by using sqlite. But when dealing with possibly 100 GB of data, it’s not possible anymore to use such modules that would possibly rewrite the whole …

Total answers: 7

How can I serialize a queryset from an unrelated model as a nested serializer?

How can I serialize a queryset from an unrelated model as a nested serializer? Question: I’m trying to add a nested serializer to an existing serializer based on some criteria of the parent model, not a foreign key. The use case is to return a ‘Research’ object with an array of ‘ResearchTemplate’ objects that are …

Total answers: 1

How do I save a trained model in PyTorch?

How do I save a trained model in PyTorch? Question: How do I save a trained model in PyTorch? I have read that: torch.save()/torch.load() is for saving/loading a serializable object. model.state_dict()/model.load_state_dict() is for saving/loading model state. Asked By: Wasi Ahmad || Source Answers: Found this page on their github repo: Recommended approach for saving a …

Total answers: 10

When to use Serializer's create() and ModelViewset's perform_create()

When to use Serializer's create() and ModelViewset's perform_create() Question: I want to clarify the given documentation of Django-rest-framework regarding the creation of a model object. So far I have found that there are 3 approaches on how to handle such events. The Serializer’s create() method. Here is the documentation class CommentSerializer(serializers.Serializer): def create(self, validated_data): return …

Total answers: 2

Django Rest Framework Recursive Nested Parent Serialization

Django Rest Framework Recursive Nested Parent Serialization Question: I have a model with a self referential field called parent. Model: class Zone(BaseModel): name = models.CharField(max_length=200) parent = models.ForeignKey(‘self’, models.CASCADE, blank=True, null=True, related_name=’children’) def __unicode__(self): return self.name Serializer: class ZoneSerializer(ModelSerializer): parent = PrimaryKeyRelatedField(many=False, queryset=Zone.objects.all()) parent_disp = StringRelatedField(many=False, source=”parent”) class Meta: model = Zone fields = (‘id’, …

Total answers: 4