Questions tagged [django-serializer]

Django provides serializers for serializing models into common data formats such as JSON and XML. Not related to django-rest-framework.

Django provides serializers for serializing models into common data formats such as JSON and XML. Serializers support both individual models and querysets, with the ability to...

  • Specify the individual fields to serialize
  • Include inherited data

Django also provides support for basic deserialization of data using the same framework.

Serializing a queryset is as simple as

from django.core import serializers
data = serializers.serialize("json", SomeModel.objects.all())
1885 questions
142
votes
11 answers

Django rest framework serializing many to many field

How do I serialize a many-to-many field into list of something, and return them through rest framework? In my example below, I try to return the post together with a list of tags associated with it. models.py class post(models.Model): tag =…
139
votes
4 answers

How to serialize Model @property with ModelSerializer?

I'm trying to serialize a model containing a property field that I also want to serialize. models.py: class MyModel(models.Model): name = models.CharField(max_length=100) slug = models.AutoSlugField(populate_from='name') @property …
Sander Smits
  • 2,051
  • 3
  • 18
  • 16
117
votes
8 answers

How to add custom field in ModelSerializer?

I created a ModelSerializer and want to add a custom field which is not part of my model. I found a description to add extra fields here and I tried the following: customField = CharField(source='my_field') When I add this field and call my…
Ron
  • 22,128
  • 31
  • 108
  • 206
104
votes
1 answer

Django REST framework serializer without a model

I'm working on a couple endpoints which aggregate data. One of the endpoints will for example return an array of objects, each object corresponding with a day, and it'll have the number of comments, likes and photos that specific user posted. This…
Farid El Nasire
  • 1,753
  • 4
  • 15
  • 17
56
votes
3 answers

Editing django-rest-framework serializer object before save

I want to edit a django-rest-framwork serializer object before it is saved. This is how I currently do it - def upload(request): if request.method == 'POST': form = ImageForm(request.POST, request.FILES) if form.is_valid(): #…
50
votes
3 answers

How to display all model fields with ModelSerializer?

models.py: class Car(): producer = models.ForeignKey(Producer, blank=True, null=True,) color = models.CharField() car_model = models.CharField() doors = models.CharField() serializers.py: class CarSerializer(ModelSerializer): …
Leon
  • 6,316
  • 19
  • 62
  • 97
48
votes
6 answers

How to serialize Django queryset.values() into json?

I have a model that has many fields, however for this problem I only need 3 of those fields. When I try to serialize a .values set I get an exception: 'dict' object has no attribute '_meta' This is my code: queryset =…
bash-
  • 6,144
  • 10
  • 43
  • 51
44
votes
5 answers

Dynamically exclude or include a field in Django REST framework serializer

I have a serializer in Django REST framework defined as follows: class QuestionSerializer(serializers.Serializer): id = serializers.CharField() question_text = QuestionTextSerializer() topic = TopicSerializer() Now I have two API views…
41
votes
9 answers

AssertionError: `HyperlinkedIdentityField` requires the request in the serializer context

I want to create a many-to-many relationship where one person can be in many clubs and one club can have many persons. I added the models.py and serializers.py for the following logic but when I try to serialize it in the command prompt, I get the…
33
votes
4 answers

How to dynamically remove fields from serializer output

I'm developing an API with Django Rest framework, and I would like to dynamically remove the fields from a serializer. The problem is that I need to remove them depending on the value of another field. How could I do that? I have a serializer…
FVod
  • 2,245
  • 5
  • 25
  • 52
27
votes
2 answers

Django Rest Framework How to update SerializerMethodField

I have a serializer like this: class PersonSerializer(serializers.ModelSerializer): gender = serializers.SerializerMethodField() bio = BioSerializer() class Meta: model = Person fields = UserSerializer.Meta.fields +…
Kishan Mehta
  • 2,598
  • 5
  • 39
  • 61
26
votes
3 answers

Unique validation on nested serializer on Django Rest Framework

I have a case like this, where you have a custom nested serializer relation with a unique field. Sample case: class GenreSerializer(serializers.ModelSerializer): class Meta: fields = ('name',) #This field is unique model =…
22
votes
1 answer

'dict' object has no attribute '_meta' on Django Serializer

def display_home(request): from datetime import * now=datetime.today() print 'Month is…
user1003121
  • 1,169
  • 3
  • 9
  • 14
22
votes
3 answers

ModelSerializer is extremely slow in Django REST framework

I am using Django REST framework for my API and yesterday I wanted to see how it works for large data. I found this tutorial about how to profile your requests (written by Tom Christie) and I discovered that for 10.000 users, my request was taking…
AdelaN
  • 3,366
  • 2
  • 25
  • 45
20
votes
5 answers

Change a field in a Django REST Framework ModelSerializer based on the request type?

Consider this case where I have a Book and Author model. serializers.py class AuthorSerializer(serializers.ModelSerializer): class Meta: model = models.Author fields = ('id', 'name') class…
him229
  • 1,284
  • 1
  • 11
  • 21
1
2 3
99 100