Hi there,
I have the following model:
class Sachbearbeiter(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
verein = models.ForeignKey(Verein)
def __str__(self):
return "{0.user.last_name}, {0.user.first_name}".format(self)
class Meta:
permissions = (
("can_read_sachbearbeiter", "Can read Sachbearbeiter"),
("can_add_sachbearbeiter", "Can add Sachbearbeiter"),
("can_change_sachbearbeiter", "Can change Sachbearbeiter"),
("can_delete_sachbearbeiter", "Can delete Sachbearbeiter"),
)
and this Serializer
class SachbearbeiterSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Sachbearbeiter
fields = ('verein',)
I need the following form of representation. It must be read-only.
{
'pk': '1',
'first_name': 'Peter',
'last_name': 'Parker',
'verein_pk': '1',
'verein': 'My Verein',}
Now the problem is, that I cannot simply use "fields = ('pk', 'verein',
'user.first_name', 'user.last_name', 'verein.pk', 'verein.name')" as this is
not supported.
I read all of the page
http://www.django-rest-framework.org/api-guide/relations/, but unless I
overlooked something, all the solutions on this page are nested, which is not
what I need.
Is there a way to have fields of related models flat in the serializer as
mentioned before? (I am not married to the field names of the related model e.g
'verein_pk')
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.