I'm not sure why I'm struggling so much with this, I have been googling and troubleshooting like crazy but I'm running up against a wall. With Django Rest Framework, what's the best way to accept optional parameters on a model creation that will then let me do something with the returned instance and those specific parameters? Say I want to track Presents and Wishlists, which is a list of Presents. Presents can be created without being on a Wishlist (sorry kids), but I also want the possibility to, when creating a Present, automatically add it to a Wishlist. Both the Present and Wishlist models have a ManyToManyField through a WishListPresent model which I specifically created (I know you don't need to but.. I can't get this stuff working so I'm trying to be as explicit as possible). The relationship works and I can manually enter items in the shell by creating a Present, saving, creating a Wishlist, saving, then WishlistPresent(present=present, wishlist=wishlist).save(), but I don't know where/how to fit this in to the normal DRF Present creation process. I thought I would be able to just add optional extra parameters to the Present creation call, which in the shell would look like: code:
code:
code:
Sulla Faex fucked around with this message at 20:57 on Nov 30, 2020 |
|
# ¿ Nov 30, 2020 20:39 |
|
|
# ¿ May 18, 2024 05:23 |
fletcher posted:I would switch up the schema a bit, the list of Presents seems odd to me. Do you need a Present to be part of multiple Wishlists? The reason I went in this direction is because I wanted specifically for presents and wishlists to be pretty flexible (with the idea of users sharing and/or extending multiple presents and wishlists), but if it don't work then it ain't flexible. So you're right, I'll simplify and get the concept working and then see later if I actually need some specific solution (which I can tackle when I'm more confident) or if I was just over-engineering from the start and missing the basics. It's a better approach anyway, I meant to practice test-first development and I completely spaced on it.. probably could have caught the "why are you starting with a needlessly complicated idea again?" fallacy Thanks! Sulla Faex fucked around with this message at 10:35 on Dec 2, 2020 |
|
# ¿ Dec 2, 2020 10:33 |