-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Required flag in many to many relations. #2674
Required flag in many to many relations. #2674
Conversation
…ds is wrong if blank==True.
Does this pull request reference any existing issue? Understand the basic premise of this pull request, but it does need further review - it's not even obvious what |
There is no issue existing yet. I did not open one because I wanted to get first review. In my projects I often use many to many fields with The restframework does something specific: If required is True, it forces to transmit the key in POST and PUT requests but with an empty array it's stil valid (thats the next bug I think). If required is False, you can send an empty array or even omit the key in POST and PUT requests. By the way: Thanks for this nice project. I love it. :-) |
Related: #2250. |
Could you rephrase this into what set of inputs you'd expect the API to allow for M2M model fields with blank = True. The existing behavior of |
Sure: Imagine the following models: class Topping(models.Model):
name = models.CharField(...)
class Pizza(models.Model):
name = models.CharField(...)
toppings = models.ManyToManyField(Topping) In this case I would expect that the default behavior of the framework would be to expect at least one topping in every pizza set. Omitting the key But if you change the models toppings = models.ManyToManyField(Topping, blank=True) then it should be possible to send new pizzas just by the name and omit the key toppings completely ( At the moment, the framework does neither like in the first nor in the second example because For me it would be enough if you fix the second example case meaning setting |
+1 |
1 similar comment
+1 |
I'm not sure which blank you're referring to.
|
There are two possible options given the example from @normanjaeckel...
I think I'd probably be happy enough with 2, although it feels a bit wooly to me. (I'd still rather my own client APIs sent a blank empty list for that case) |
That would be nice for the next bugfixing or minor release. But nevertheless it looks strange that I can send an empty list to a required field in DRF but not in Django forms. E. g. it is also invalid to send an empty string to a required char field in DRF and Django forms. Things should be consistent here. So this point might be something for the next major release ... |
So are we saying that setting |
No. If the field is not required, you can send what you want. But if it is required, in my opinion None and empty lists should be forbidden. |
Considering closing this in favor of #2804 - would that be reasonable? |
Sure. |
Here a test to show that the required flag in ManyToMany relation fields is wrong if
blank=True
.This does not happen if
null=True
is also set. I expected the required flag be False ifblank=True
regardless ofnull=True
is set or not. (In Django ManyToMany fieldsnull=True
is not evaluated I think.)