python - Django rest frame getting values from all tables where the PK is referenced as FK -


with parent model - studioprofile, other models have foreign key relationship studioprofile. how can related table data when api request made studioprofile serializer. below models, views , serializers,

class studioprofile(models.model):     name = models.charfield(max_length = 120)     address_1 = models.charfield(max_length = 200)     address_2 = models.charfield(max_length = 200)  class studioservices(models.model):      studio_profile = models.foreignkey(studioprofile, related_name = "services")     service_name = models.charfield(max_length = 50)  class studiopicture(models.model):      studio_profile  = models.foreignkey(studioprofile, related_name = "pic_of_studio")     picture = models.imagefield(upload_to = 'img_gallery', null = true, blank = true) 

serializers.py

class studioservicesserializer(serializers.modelserializer):     class meta:         model = studioservices         fields = ('studio_profile',   'service_name')  class studiopicserializer(serializers.modelserializer):      class meta:          model = studiopicture          fields = ('picture')   class studioprofileserializer(serializers.modelserializer):     services = studioservicesserializer(source = "studioservices")     pic_of_studio = studiopicserializer(source = "studiopicture")     class meta:         model = studioprofile         fields = ( 'address_1', 'address_2','services','pic_of_studio' ) 

views.py

class studioprofile(listapiview):     permission_classes = (readwithoutauthentication,)     serializer_class = studioprofileserializer     queryset = studioprofile.objects.select_related().filter(id = 1) 

am not able data. doing wrong here? when request studioprofile class how can related entries.

traceback:

got attributeerror when attempting value field service_name on serializer studioprofileserializer. serializer field might named incorrectly , not match attribute or key on studioprofile instance. original exception text was: 'studioprofile' object has no attribute 'studioservices'.

i think might need include many=true , change source related_name in studioprofileserializer:

class studioprofileserializer(serializers.modelserializer):     services = studioservicesserializer(many = true, source = "services")     pic_of_studio = studiopicserializer(many = true, source = "pic_of_studio")     class meta:         model = studioprofile         fields = ( 'address_1', 'address_2','services','pic_of_studio' ) 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -