Best way for POSTing two models

54 views
Skip to first unread message

Rob Deutsch

unread,
Nov 13, 2019, 6:44:38 PM11/13/19
to Django REST framework
I currently have a single Django model (lets call it Book) being made available via a ModelViewSet.

I want to add a second model (call it Author) with a foreign key to Book.

I want to insist that whenever a Book is created an Author is also created at the same time. It should not be possible to create a Book without an Author.

I have determined that it's impossible to do this using Django in models.py. I know there must be a way to do it with DRF in serializers.py and/or views.py, but I've been unable to figure out the best way to do it. I've tried a few different things, but keep hitting nonsensical roadblocks, and I think that I conceptually just don't know the best way to assemble DRF to achieve this.

Any suggestions?


Rob Deutsch

unread,
Nov 13, 2019, 6:49:28 PM11/13/19
to Django REST framework
One addendum: What I would like to do, from the public facing perspective, is change the Book ModelViewSet's POST event so that it includes the parameters include everything needed to create both a Book and an Author, but I still want it to return a Book model.

Ben Warren

unread,
Nov 13, 2019, 8:33:18 PM11/13/19
to django-res...@googlegroups.com
You're describing writable nested serializers.  See the docs here.  See also Tom's writing on data encapsulation here

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/e2c5b788-21f5-41b7-b0a1-901f34675870%40googlegroups.com.

Rob Deutsch

unread,
Nov 13, 2019, 11:04:59 PM11/13/19
to Django REST framework
Thank you Ben. I was implementing this as nested serializers, so its good to confirm that I was on the right track. Also thank you for the link to Tom's writing, that will be very useful!

I am still having a problem though. Using the Album/Track models in the docs: I want my ModelViewSet's create() function to accept as input an AlbumSerializer but return an AlbumSerializerWithoutTracks (which is almost the same thing, but no tracks custom field).

I've looked at the CreateModelMixin and determined that I cannot use it, and I have to write my own create() function. That is fine. But what is the most logical way to to have this shown in the generated schema?

On Thursday, November 14, 2019 at 12:33:18 PM UTC+11, Ben Warren wrote:
You're describing writable nested serializers.  See the docs here.  See also Tom's writing on data encapsulation here

On Wed, Nov 13, 2019 at 6:49 PM Rob Deutsch <rob...@gmail.com> wrote:
One addendum: What I would like to do, from the public facing perspective, is change the Book ModelViewSet's POST event so that it includes the parameters include everything needed to create both a Book and an Author, but I still want it to return a Book model.

On Thursday, November 14, 2019 at 10:44:38 AM UTC+11, Rob Deutsch wrote:
I currently have a single Django model (lets call it Book) being made available via a ModelViewSet.

I want to add a second model (call it Author) with a foreign key to Book.

I want to insist that whenever a Book is created an Author is also created at the same time. It should not be possible to create a Book without an Author.

I have determined that it's impossible to do this using Django in models.py. I know there must be a way to do it with DRF in serializers.py and/or views.py, but I've been unable to figure out the best way to do it. I've tried a few different things, but keep hitting nonsensical roadblocks, and I think that I conceptually just don't know the best way to assemble DRF to achieve this.

Any suggestions?


--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

On Thursday, November 14, 2019 at 12:33:18 PM UTC+11, Ben Warren wrote:
You're describing writable nested serializers.  See the docs here.  See also Tom's writing on data encapsulation here

On Wed, Nov 13, 2019 at 6:49 PM Rob Deutsch <rob...@gmail.com> wrote:
One addendum: What I would like to do, from the public facing perspective, is change the Book ModelViewSet's POST event so that it includes the parameters include everything needed to create both a Book and an Author, but I still want it to return a Book model.

On Thursday, November 14, 2019 at 10:44:38 AM UTC+11, Rob Deutsch wrote:
I currently have a single Django model (lets call it Book) being made available via a ModelViewSet.

I want to add a second model (call it Author) with a foreign key to Book.

I want to insist that whenever a Book is created an Author is also created at the same time. It should not be possible to create a Book without an Author.

I have determined that it's impossible to do this using Django in models.py. I know there must be a way to do it with DRF in serializers.py and/or views.py, but I've been unable to figure out the best way to do it. I've tried a few different things, but keep hitting nonsensical roadblocks, and I think that I conceptually just don't know the best way to assemble DRF to achieve this.

Any suggestions?


--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

On Thursday, November 14, 2019 at 12:33:18 PM UTC+11, Ben Warren wrote:
You're describing writable nested serializers.  See the docs here.  See also Tom's writing on data encapsulation here

On Wed, Nov 13, 2019 at 6:49 PM Rob Deutsch <rob...@gmail.com> wrote:
One addendum: What I would like to do, from the public facing perspective, is change the Book ModelViewSet's POST event so that it includes the parameters include everything needed to create both a Book and an Author, but I still want it to return a Book model.

On Thursday, November 14, 2019 at 10:44:38 AM UTC+11, Rob Deutsch wrote:
I currently have a single Django model (lets call it Book) being made available via a ModelViewSet.

I want to add a second model (call it Author) with a foreign key to Book.

I want to insist that whenever a Book is created an Author is also created at the same time. It should not be possible to create a Book without an Author.

I have determined that it's impossible to do this using Django in models.py. I know there must be a way to do it with DRF in serializers.py and/or views.py, but I've been unable to figure out the best way to do it. I've tried a few different things, but keep hitting nonsensical roadblocks, and I think that I conceptually just don't know the best way to assemble DRF to achieve this.

Any suggestions?


--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

On Thursday, November 14, 2019 at 12:33:18 PM UTC+11, Ben Warren wrote:
You're describing writable nested serializers.  See the docs here.  See also Tom's writing on data encapsulation here

On Wed, Nov 13, 2019 at 6:49 PM Rob Deutsch <rob...@gmail.com> wrote:
One addendum: What I would like to do, from the public facing perspective, is change the Book ModelViewSet's POST event so that it includes the parameters include everything needed to create both a Book and an Author, but I still want it to return a Book model.

On Thursday, November 14, 2019 at 10:44:38 AM UTC+11, Rob Deutsch wrote:
I currently have a single Django model (lets call it Book) being made available via a ModelViewSet.

I want to add a second model (call it Author) with a foreign key to Book.

I want to insist that whenever a Book is created an Author is also created at the same time. It should not be possible to create a Book without an Author.

I have determined that it's impossible to do this using Django in models.py. I know there must be a way to do it with DRF in serializers.py and/or views.py, but I've been unable to figure out the best way to do it. I've tried a few different things, but keep hitting nonsensical roadblocks, and I think that I conceptually just don't know the best way to assemble DRF to achieve this.

Any suggestions?


--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

On Thursday, November 14, 2019 at 12:33:18 PM UTC+11, Ben Warren wrote:
You're describing writable nested serializers.  See the docs here.  See also Tom's writing on data encapsulation here

On Wed, Nov 13, 2019 at 6:49 PM Rob Deutsch <rob...@gmail.com> wrote:
One addendum: What I would like to do, from the public facing perspective, is change the Book ModelViewSet's POST event so that it includes the parameters include everything needed to create both a Book and an Author, but I still want it to return a Book model.

On Thursday, November 14, 2019 at 10:44:38 AM UTC+11, Rob Deutsch wrote:
I currently have a single Django model (lets call it Book) being made available via a ModelViewSet.

I want to add a second model (call it Author) with a foreign key to Book.

I want to insist that whenever a Book is created an Author is also created at the same time. It should not be possible to create a Book without an Author.

I have determined that it's impossible to do this using Django in models.py. I know there must be a way to do it with DRF in serializers.py and/or views.py, but I've been unable to figure out the best way to do it. I've tried a few different things, but keep hitting nonsensical roadblocks, and I think that I conceptually just don't know the best way to assemble DRF to achieve this.

Any suggestions?


--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

On Thursday, November 14, 2019 at 12:33:18 PM UTC+11, Ben Warren wrote:
You're describing writable nested serializers.  See the docs here.  See also Tom's writing on data encapsulation here

On Wed, Nov 13, 2019 at 6:49 PM Rob Deutsch <rob...@gmail.com> wrote:
One addendum: What I would like to do, from the public facing perspective, is change the Book ModelViewSet's POST event so that it includes the parameters include everything needed to create both a Book and an Author, but I still want it to return a Book model.

On Thursday, November 14, 2019 at 10:44:38 AM UTC+11, Rob Deutsch wrote:
I currently have a single Django model (lets call it Book) being made available via a ModelViewSet.

I want to add a second model (call it Author) with a foreign key to Book.

I want to insist that whenever a Book is created an Author is also created at the same time. It should not be possible to create a Book without an Author.

I have determined that it's impossible to do this using Django in models.py. I know there must be a way to do it with DRF in serializers.py and/or views.py, but I've been unable to figure out the best way to do it. I've tried a few different things, but keep hitting nonsensical roadblocks, and I think that I conceptually just don't know the best way to assemble DRF to achieve this.

Any suggestions?


--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

On Thursday, November 14, 2019 at 12:33:18 PM UTC+11, Ben Warren wrote:
You're describing writable nested serializers.  See the docs here.  See also Tom's writing on data encapsulation here

On Wed, Nov 13, 2019 at 6:49 PM Rob Deutsch <rob...@gmail.com> wrote:
One addendum: What I would like to do, from the public facing perspective, is change the Book ModelViewSet's POST event so that it includes the parameters include everything needed to create both a Book and an Author, but I still want it to return a Book model.

On Thursday, November 14, 2019 at 10:44:38 AM UTC+11, Rob Deutsch wrote:
I currently have a single Django model (lets call it Book) being made available via a ModelViewSet.

I want to add a second model (call it Author) with a foreign key to Book.

I want to insist that whenever a Book is created an Author is also created at the same time. It should not be possible to create a Book without an Author.

I have determined that it's impossible to do this using Django in models.py. I know there must be a way to do it with DRF in serializers.py and/or views.py, but I've been unable to figure out the best way to do it. I've tried a few different things, but keep hitting nonsensical roadblocks, and I think that I conceptually just don't know the best way to assemble DRF to achieve this.

Any suggestions?


--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

Rob Deutsch

unread,
Nov 13, 2019, 11:30:38 PM11/13/19
to Django REST framework
Wait a second! I think I solved it! Armed with the knowledge that nested serializers are the right way to go, I ransacked cdrf.co and the DRF source code and found the write_only property.

Is it as simple as using the write_only property:

class AlbumSerializer(serializers.ModelSerializer):
    tracks
= TrackSerializer(many=True, write_only=True)

When I do that it looks like the requestBody includes tracks, but the response excludes tracks. Which is what I wanted.
Reply all
Reply to author
Forward
0 new messages