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 identified by filtering on a Postgres ArrayField. Models class Research(TimeStampedModel): category = models.CharField(max_length=100, choices=RESEARCH_TEMPLATE_CATEGORIES, default=’quote’) body = models.CharField(max_length=1000, blank=True, default=”)
Tag: serialization
Django model DateTimeField set auto_now_add format or modify the serializer
I have this field in my model: And it is saved with this format: 2016-05-18T15:37:36.993048Z So I would like to convert it to this format DATE_INPUT_FORMATS = (‘%d-%m-%Y %H:%M:S’) but I dont know where to do it. I have a simple serializer class, could I override it to modify the format? or maybe create a get_date() model method? My settings:
Python2: Should I use Pickle or cPickle?
Python 2 has both the pickle and cPickle modules for serialization. cPickle has an obvious advantage over pickle: speed. What, if any, advantage does pickle have over cPickle? Answer The pickle module implements an algorithm for turning an arbitrary Python object into a series of bytes. This process is also called serializing” the object. The byte stream representing the object
I want my django rest framework serializer to accept input but not add to model
I removed some fields from my model, but I want the serializer to still accept the fields as input. How do I have fields the serializer accepts but doesn’t use? Answer From http://www.django-rest-framework.org/api-guide/serializers/ You can add extra fields to a ModelSerializer or override the default fields by declaring fields on the class, just as you would for a Serializer class.
Get current user in Model Serializer
Is it possible to get the current user in a model serializer? I’d like to do so without having to branch away from generics, as it’s an otherwise simple task that must be done. My model: My serializer: And my view: How can I get the model returned, with an additional field user such that my response looks like this:
How to JSON serialize sets?
I have a Python set that contains objects with __hash__ and __eq__ methods in order to make certain no duplicates are included in the collection. I need to json encode this result set, but passing even an empty set to the json.dumps method raises a TypeError. I know I can create an extension to the json.JSONEncoder class that has a
How to get string objects instead of Unicode from JSON
I’m using Python 2 to parse JSON from ASCII encoded text files. When loading these files with either json or simplejson, all my string values are cast to Unicode objects instead of string objects. The problem is, I have to use the data with some libraries that only accept string objects. I can’t change the libraries nor update them. Is
How to serialize Python objects in a human-readable format? [closed]
Closed. This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 years ago. The community reviewed whether to reopen this question 1 year ago and left it closed: Original close reason(s) were not resolved Improve this question