Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

double save #97

Open
Lewiscowles1986 opened this issue Oct 25, 2016 · 0 comments
Open

double save #97

Lewiscowles1986 opened this issue Oct 25, 2016 · 0 comments

Comments

@Lewiscowles1986
Copy link

Not sure if this can be integrated, but it would be nice to see it as it's one of the most common things I encounter across a range of technologies (not just python or django)

So I recently saw this in some django code (I've made it generic)

class ItemViewSet(viewsets.ModelViewSet):
    queryset = models.Item.objects.all()
    serializer_class = ItemSerializer

    def perform_create(self, serializer):
        instance = serializer.save()
        instance.url = reverse('item-detail', args=[instance.pk], request=self.request)
        instance.save()

This causes a double-save to a single instance, which IMO is never acceptable and a sign of a bad design.

Instead I recommend adding this to ItemSerializer (basically generating every time before sending to view)

class ItemSerializer(HyperlinkedModelSerializer):
    url = HyperlinkedIdentityField(view_name='item-detail', read_only=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant