chibisov / drf-extensions

DRF-extensions is a collection of custom extensions for Django REST Framework
http://chibisov.github.io/drf-extensions/docs
MIT License
1.47k stars 208 forks source link

PUT/POST not working on nested view sets #106

Open sauliuni opened 9 years ago

sauliuni commented 9 years ago

Hello,

update: it seems, the extension does not support the creation of such objects. Im adding Question 2:

im using the NestedViewSet for my REST application. The problem is that it seems to be correclty configured, and viewing nested rescources works as it should be. Even deletion and put of elements can be made. The problem i have is the POST (creation of new element): the element is created but it is not linked to the parent object, e.g.

POST localhost/elements --> Works, Element is created with id 1 POST localhost/elements/1/blocks -> Block is created but is not in the element 1 list

Question 1: How can i use the POST request? Do i need to rewrite the save in the serializer or is it supported by the extension? I could not really understand how parents_query_lookups and base_name are used by the extension ( using the documentation)

Question 2: How can i access the elements id in block serializer?

Im using following code

Routing:

router = ExtendedSimpleRouter()
(
router.register(elements', SequenceViewSet, base_name='element')
 .register(r'blocks', BlockViewSet, base_name='elements-blocks', parents_query_lookups=['element'])
)
urlpatterns = router.urls

The Models looks like this:

class Element(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.TextField(null=True)
    parameter = models.ManyToManyField(Parameter, blank=True, null=True)
    block = models.ManyToManyField(Block, blank=True, null= True)

class Block(models.Model):
    parent = XXClassExtension('self', null=True, blank=True, related_name='children', db_index=True)
    name = models.TextField()
    parameter = models.ManyToManyField(Parameter, blank=True, null=True)

Views:

class ElementViewSet(NestedViewSetMixin,viewsets.ModelViewSet):
    model = Element
    queryset = Element.objects.all()
    serializer_class = ElementSerializer

class BlockViewSet(NestedViewSetMixin, viewsets.ModelViewSet):
    model = Block
    queryset = Block.objects.all()
    serializer_class = BlockSerializer

Serializers:

class ElementSerializer(serializers.ModelSerializer):
    class Meta:
        model = Element
        fields = ('id', 'name', 'parameter','block')

class BlockSerializer(PartialUpdateSerializerMixin, serializers.ModelSerializer):
    class Meta:
        model = Block
        fields = ('id','name','parent','parameter','connection')
chibisov commented 9 years ago

Nesting feature only applies to queryset. parents_query_lookups just adds lookups to view's queryset. There is no way drf-extensions would do that for write requests. You have to extend default view's create/update methods. NestedViewSetMixin.get_parents_query_dict method can help for retrieving parent lookups.

stuartc commented 4 years ago

Ah man, I wish this was documented. I just spent the better part of 4 hours trying to work out why my records weren't being associated. Thanks for the tip!