django-ninja icon indicating copy to clipboard operation
django-ninja copied to clipboard

ModelSchema: allow model_fields = []

Open hiaselhans opened this issue 3 years ago • 4 comments
trafficstars

This PR tests model_fields and model_exclude against None, so the following two ModelSchemes are valid:

class SampleSchema3(ModelSchema):
    class Config:
        model = User
        model_fields  = []


class SampleSchema4(ModelSchema):
    class Config:
        model = User
        model_exclude  = [] # equivalent to model_fields = "__all__"

hiaselhans avatar Sep 07 '22 15:09 hiaselhans

What's the reasoning behind this versus what already works?

Especially model_fields = [] doesn't naturally infer that you mean all fields. It reads as the opposite!

SmileyChris avatar Sep 07 '22 22:09 SmileyChris

its more idiomatic and it allows for a modelschema without any model_fields.

I think your deduction is wrong, please read the test included:

model_fields = [] => no fields model_exclude = [] => all fields

I think thats exactly what one would expect and its much easier to remember than __all__

hiaselhans avatar Sep 08 '22 05:09 hiaselhans

You're right, I just glanced at the code and description here and thought both options being an empty list were supposed to represent the same behaviour. My apologies for the overly quick skim read and reply -- thanks for following up with your reasoning.

I'm now personally just ambivalent on the change, probably because I'm used to seeing __all__ historically and I don't see that much benefit in introducing a second way to represent something we already can do. I can see your reasoning though. We'll see if anyone else has an opinion :)

SmileyChris avatar Sep 08 '22 09:09 SmileyChris

No worries, and sorry for the short answer :)

After all it might turn out to be a really just a matter of preference.

In my opinion, if i can exclude one field, I should also be able to exclude zero fields. If i can include one, i should also be able to include 0.

hiaselhans avatar Sep 08 '22 10:09 hiaselhans

sorry, closing this - as of django ninja v1 - we are following same guides as django model forms - so now recomended way is

class Foo(ModelSchema):
    class Meta:
          model = Foo
          fields = ['some'] # NOT !! model_fields

vitalik avatar Nov 17 '23 11:11 vitalik