django - How to set SlugRelated field to a field within an object field -


i have following models:

class category(models.model):     name = models.charfield()     ... # fields omitted  class prediction(models.model):     conversation = models.foreignkey(conversation)     category = models.foreignkey(category)     ... # fields omitted  class conversation(models.model):     sid = models.charfield()     ... # fields omitted 

now i'm trying create model serializer category return me following serialized object:

{     "name":"blah",     "conversations":[         "2af22188c5c97256",    # value of sid field         "073aef6aad0883f8",         "5d3dc73fc8cf34be",      ] } 

here have in serializer:

class categoryserializer(serializers.modelserializer):     conversations = serializers.slugrelatedfield(many=true,              read_only=true,               source="prediction_set",               slug_field='conversation.sid')      class meta:         model = models.class         fields = ('class_name', 'conversations') 

however, doesn't work because somehow django doesn't allow me set slug_field field that's within object field. suggestions on how accomplish this?

you modelling many-to-many relationship between categorys , conversations explicit table called prediction. django way of doing explicitly state many-to-many on either side of relationship , specify prediction "through-model":

shamelessly stolen example this question:

class category(models.model):     name = models.charfield(max_length=255)     slug = models.slugfield(unique=true, max_length=255, blank=true,default=none)     desc = models.textfield(blank=true, null=true )      ...  class post(models.model):    title = models.charfield(max_length=255)    pub_date = models.datetimefield(editable=false,blank=true)    author = models.foreignkey(user, null=true, blank=true)    categories = models.manytomanyfield(category, blank=true, through='cattopost')     ...   class cattopost(models.model):     post = models.foreignkey(post)     category = models.foreignkey(category)      ... 

this shows way set relationship.

equally shamelessly stolen answer @amir masnouri:

class categoryserializer(serializers.modelserializer):       class meta:             model = category             fields = ('name','slug')  class postserializer(serializers.modelserializer):        class meta:             model = post               fields = ('id','{anything want}','categories')             depth = 2 

this shows nice way of achieving nested serialization behavior like.