Serializing Subclasses

26 views
Skip to first unread message

David Martin

unread,
Feb 27, 2015, 9:25:23 AM2/27/15
to django-res...@googlegroups.com
Hi there!

Since I discovered the DRF i've been working with this awesome django framework for creating my project api's.

But now I'm facing a new problem:

I've got this kind of models in my django app:


class Reservation(models.Model):

class Meta:
verbose_name = _('Reservation')
verbose_name_plural = _('Reservations')

And then...



class FlightReservation(Reservation):

class Meta:
verbose_name = _('Flight reservation')
verbose_name_plural = _('Flights')

So, what i want is to be able to Seralize the global data on all Reservations (by using the ReservationSerializer) but to be able to add some fields from the subclasses, and get usable data.

Thanks in advance!


Chris Foresman

unread,
Mar 4, 2015, 11:56:13 PM3/4/15
to django-res...@googlegroups.com
Sounds like the best option is to write a subclass of `serializers.Serializer` and not `serializers.ModelSerializer`. You'll just have to define all the fields manually, and have optional fields that populate depending on the class of the object passed in to the serializer.
Reply all
Reply to author
Forward
0 new messages