HackSoftware / Django-Styleguide

Django styleguide used in HackSoft projects

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Creation services with many fields in the model

jsandovalc opened this issue · comments

Hi everyone,

I noticed that for updating, it's recommended to use a data dict with the fields to update (#57). I was wondering if you do something similar with creates. I have a creation service with a lot of fields, and it the method signature gets really long.

Do you do something similar to updates?

Thanks.

@jsandovalc Hello 👋

If it makes sense to pass a data dictionary for something that needs to create an object with a lot of fields - do so.

The general rule of thumb is to have a strictly-defined list of possible fields, so you don't just do:

for key, value in data.items():
    setattr(obj, key, value)

but rather:

for key in possible_fields:
    if key in data:
        value = data[key]
        setattr(obj, key, value)

Closing for now ✌️