python - AttributeError when serializing relationship with 'through' table -


i'm trying make django rest framework return me to-many relationship constructed through keyword. however, i'm getting error , have no idea what's wrong.

the error i'm getting is:

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

here model:

class step(basemodel):      description = models.textfield(null=false, blank=false)  class stepinfo(basemodel):      recipe = models.foreignkey('recipe')     step = models.foreignkey(step)     step_number = models.integerfield()  class recipe(basemodel):      title = models.charfield(max_length=100)     steps = models.manytomanyfield(step, through='stepinfo') 

and implementation of serializers:

class stepinfoserializer(serializers.hyperlinkedmodelserializer):      description = serializers.charfield(source="step.description")      class meta:         model = stepinfo         fields = ('id',                   'step_number',                   'description')   class recipeserializer(serializers.hyperlinkedmodelserializer):      steps = stepinfoserializer(many=true, read_only=true)      class meta:         model = recipe         fields = ('id',                   'steps') 

can cast light on problem? i'd appreciate! thanks!

the simplest way change change source of steps in recipeserializer

class recipeserializer(serializers.hyperlinkedmodelserializer):      steps = stepinfoserializer(many=true, read_only=true, source='stepinfo_set') 

now make explanation that.

first, in django world.

for example, rec instance object of recipe model.

get difference of 2 statement.

rec.steps.all() rec.stepinfo_set.all() 

rec.steps.all() returns array of step object instance, make sure it's target model of manytomanyfield, not through model.

if want access through model, need visit related_name of foreignkey refer current model in through model (stepinfo model).

later, add example related_name.

since in stepinfo not specify related_name, django automatically give default related_name stepinfo_set. (this name 1 in first snippet).

so return how through instances. rec.setpinfo_set.all().

all right, in end, suggest add related_name foreignkey in through models.

class stepinfo(basemodel):      recipe = models.foreignkey('recipe', related_name='stepinfos')     step = models.foreignkey(step, related_name='stepinfos')     step_number = models.integerfield()  class recipeserializer(serializers.hyperlinkedmodelserializer):      steps = stepinfoserializer(many=true, read_only=true, source='stepinfos') 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -