fopina / django-bulk-update-or-create

`bulk_update_or_create` for Django model managers
MIT License
148 stars 16 forks source link

Is it possible to pass the dict variable directly into the Model object #1

Closed bbyan closed 4 years ago

bbyan commented 4 years ago

Hello fopina, Thank you for writing this very useful module. But when using it, there was a problem that I couldn't solve.

for example: I have data of dict type dicts = {a:'b',c:'d',e:'f'} In your use case, I must do: items = [ data(a='b',c='e',e='f')]

But in my case , this dic is dynamic and I cannot write dead code.And there are quite a few KEYs in the actual dicts. I would like to ask if related functions will be supported next, or how can I deal with this problem.

thank you,

fopina commented 4 years ago

not sure I understand, you want to pass a dict of a specific model attributes or a dict not even attached to a specific model?

The latter is not possible as Django ORM is inherently bound to a Model and this method uses ORM bulk_update behind it which requires the batch update to happen on the same model/table.

if you just want to instantiate objects with different attributes you can use the typical **dict syntax

using RandomData as example

d1 = {'uuid': 1, 'data': 'data for 1', 'optional_field': 1}
d2 = {'uuid': 1, 'data': 'data for 1', 'other_optional_field': 2}
items = [
    RandomData(**d1),
    RandomData(**d2),
]

It does however imply that (from this example) both optional_field and other_optional_field need to be part of the batch update_fields.