DRF and self join

34 views
Skip to first unread message

Shekar Tippur

unread,
Aug 21, 2015, 12:12:00 AM8/21/15
to Django users
Hello,

I am looking for a model/serializer/view for a self join. I can have nested paernt child relationship.

The one I am trying:

model:

class ParentChild(models.Model):
     parent=models.CharField(max_length=100blank=Falsedefault=''unique=True)
     child=models.ForeignKey('self'blank=Truenull=True )

Serializer

class ParentChileSerializer(serializers.ModelSerializer):
class Meta:
model = ParentChild
fields = ('id', 'parent','child_id')

View

??

Not sure how to proceed. Appreciate any help.

- Shekar

James Schneider

unread,
Aug 21, 2015, 1:26:39 AM8/21/15
to django...@googlegroups.com

Have a look at this SO post that the DRF author responded to for a similar situation.

http://stackoverflow.com/questions/13376894/django-rest-framework-nested-self-referential-objects

As far as the view, you'll need to be more specific about what you want the view to do. The serializer is probably the hard part though.

-James

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b4bd6e40-b9ac-4b55-8631-f06417634b3f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Shekar Tippur

unread,
Aug 23, 2015, 4:49:45 AM8/23/15
to Django users
Thanks James. I had seen this link earlier but was not sure if I understood it right. Revisiting this link after your post made it clear.

Thanks again for the pointer.

- Shekar
Reply all
Reply to author
Forward
0 new messages