Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Sulla Faex
May 14, 2010

No man ever did me so much good, or enemy so much harm, but I repaid him with ENDLESS SHITPOSTING
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:
Present.objects.create(name="Some Supersoaker", wishlists=[wishlist1, wishlist2, wishlist3])
for the model:

code:
class Present(models.Model):
	name = models.CharField(max_length=100)
	# i have tried both with and without the wishlists field def
	wishlists = models.ManyToManyField('Wishlist', 
		blank=True, 
		through='WishlistPresent', 
		through_fields=('present', 'wishlist')
	)
and then have the PresentSerializer intercept it, doing something like:

code:
class PresentSerializer(serializers.ModelSerializer):

	wishlists = WishlistSerializer(many=True)

	def create(self, validated_data):
    		present = Present.objects.create(**validated_data)
    		present.save() # don't think this is necessary but I'm at my wit's end	
    		wishlists = self.request.query_params.get('wishlists')
    		for wishlist in wishlists:
        		wishlist.add(present)
    		return present

	class Meta:
        	model = Present
        	fields = ['pk', 'name', 'wishlists']
        	extra_kwargs = { 'wishlists' : { 'required' : False } }
but it's not working, and I think my problem lies with the model not accepting or forwarding the wishlist param. I've tried going back and making sure my fundamentals are sound but I'm just getting more and more confused. Is there a standard approach for this or some specific terms I should be googling to learn more about this?

Sulla Faex fucked around with this message at 20:57 on Nov 30, 2020

Adbot
ADBOT LOVES YOU

Sulla Faex
May 14, 2010

No man ever did me so much good, or enemy so much harm, but I repaid him with ENDLESS SHITPOSTING

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?

I was thinking it should be along the lines of:
code:
class WishList(models.Model):
	name = models.CharField(max_length=100)

class Present(models.Model):
	name = models.CharField(max_length=100)
        wishlist = models.ForeignKey(Wishlist, related_name='presents', blank=True, null=True)
In your UI code I would just do two separate API calls on the flow where they want to create a Present and add it to a new Wishlist, one to create the WishList and then one to create the Present associated with that WishList. Or if they are associating the present to an existing WishList, just hit the endpoint to create the Present and specify the WishList value. Easy access to wishlist.presents in the ORM if you want the whole wishlist.

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply