I have two models, one with M2M relation and a related name. I want to include all fields in the serializer and the related field.
models.py:
class Pizza(models.Model):
name = models.CharField(max_length=50, unique=True)
toppings = models.ManyToManyField(Topping, null=True, blank=True, related_name='pizzas')
class Topping(models.Model):
name = models.CharField(max_length=50, unique=True)
price = models.IntegerField(default=0)
serializer.py:
class ToppingSerializer(serializers.ModelSerializer):
class Meta:
model = Topping
fields = '__all__'
This works but it doesn't include the related field.
fields = ['name', 'price', 'pizzas']
This works exactly as I want, but what happens when Toppings model has a lot of fields. I want to do something like :
fields = ['__all__', 'pizzas']
This syntax results in an error saying:
Field name
__all__
is not valid for model
Is there a way to achieve the wanted behavior? Or the fields must be typed manually when using a related name ?
I just checked the source code of Django Rest Framework. The behaviour you want seems not to be supported in the Framework.
The
fields
option must be a list, a tuple or the text__all__
.Here is a snippet of the relevant source code:
You cannot add 'all' additionally to the tuple or list with fields...
Like @DanEEStart said, DjangoRestFramework don't have a simple way to extend the 'all' value for fields, because the
get_field_names
methods seems to be designed to work that way.But fortunately you can override this method to allow a simple way to include all fields and relations without enumerate a tons of fields.
I override this method like this:
Note that this method only change the behaviour of this serializer, and the
extra_fields
attribute only works on this serializer class.If you have a tons of serializer like this, you can create a intermediate class to include this
get_fields_names
method in one place and reuse'em many times. Some like this:Hi I could achieve the expected result by using Django's _meta API , which seems to be available since Django 1.11. So in my serializer I did:
In programming there's always many ways to achieve the same result, but this one above, has really worked for me.
Cheers!
An old issue but thought that this may help others in the future.
I've just hit a similar issue and got the "all" option working by specifying an additional field manually as per the following example. I'm not sure if this would also solve your issue. It's a damn sight cleaner than anything else I've seen.
http://www.django-rest-framework.org/api-guide/relations/#nested-relationships
I would assume this would work for any of the other related field options listed on the same page: http://www.django-rest-framework.org/api-guide/relations/#serializer-relations
I'm using the Django Rest Framework version 3.6.2
Reverse relation example as requested:
to include all the fields and the other fields defined in your serializer you can just say
exclude = ()
This will list all the field values with the extra argument pizzas