Home > Net >  TypeError when using Django update_or_create
TypeError when using Django update_or_create

Time:10-05

I try to use update_or_create method but got a TypeError

Django 2.2.5 Python 3.7

TypeError: update_or_create() got multiple values for keyword argument 'dcf_ide'

I probably misunderstand how to use update_or_create method I've read this post

datas in DataCorrectionForm model before update

         dcf_ide     pat      status                       crated_date
0   43_inc_poi_1  01-001           1  2022-10-04 08:36:21.991740 00:00
1   43_inc_poi_3  01-003           1  2022-10-04 08:36:22.031926 00:00
2   43_inc_poi_7  02-001           1  2022-10-04 08:36:22.054673 00:00
3   43_inc_poi_9  01-050           1  2022-10-04 08:36:22.075715 00:00
4  43_inc_poi_11  01-002           1  2022-10-04 08:36:22.096237 00:00
5   46_inc_tai_2  02-002           1  2022-10-04 08:36:22.147911 00:00
6   46_inc_tai_4  01-004           1  2022-10-04 08:36:22.175169 00:00
7   46_inc_tai_7  02-001           1  2022-10-04 08:36:22.193514 00:00
8   46_inc_tai_9  01-050           1  2022-10-04 08:36:22.210608 00:00
records = DCF_AFTER_UPDATE.to_dict(orient='records')
for record in records:
    DataCorrectionForm.objects.update_or_create(
         dcf_ide=record['dcf_ide'],
         **record
    )

Update/Create

         dcf_ide     pat      status                       crated_date
0   43_inc_poi_1  01-001           0  2022-10-04 08:36:21.991740 00:00    #<= update status column
1   43_inc_poi_3  01-003           2  2022-10-04 08:36:22.031926 00:00    #<= update status column
2   43_inc_poi_7  02-001           2  2022-10-04 08:36:22.054673 00:00    #<= update status column
3   43_inc_poi_9  01-050           2  2022-10-04 08:36:22.075715 00:00    #<= update status column
4  43_inc_poi_11  01-002           2  2022-10-04 08:36:22.096237 00:00    #<= update status column
5   46_inc_tai_2  02-002           2  2022-10-04 08:36:22.147911 00:00    #<= update status column
6   46_inc_tai_4  01-004           2  2022-10-04 08:36:22.175169 00:00    #<= update status column
7   46_inc_tai_7  02-001           0  2022-10-04 08:36:22.193514 00:00    #<= update status column
8   46_inc_tai_9  01-050           2  2022-10-04 08:36:22.210608 00:00    #<= update status column
1   43_inc_poi_2  02-002           1  2022-10-04 10:00:02.896153 00:00    #<= create row
0   46_inc_tai_1  01-001           1  2022-10-04 10:00:10.937815 00:00    #<= create row

CodePudding user response:

Looking at the post you shared I notice a difference, you define your code like:

DataCorrectionForm.objects.update_or_create(
     dcf_ide=record['dcf_ide'],
     **record
)

Where you use the **kwargs notation for the **record.

In the post they define the update_or_create as:

Model.objects.update_or_create(field1=val1,
                               field2=val2,
                               defaults={
                                   'field3': val3,
                                   'field4': val4
                               })

You can see the difference with the defaults={'field3': val3,'field4': val4})

I suppose you should follow this example to make it work and write your code like this:

DataCorrectionForm.objects.update_or_create(
         dcf_ide=record['dcf_ide'],
         defaults=record
    )

Please note I've not ran this code myself, I just looked at the example you shared.

CodePudding user response:

When you do: update_or_create(dcfe_ide=record['dcf_ide'], **record) you are adding the entire record dictionary as argument.

if your record variable is something like: {'dcf_ide': '...', 'other': '...'}, you are actually doing:

DataCorrectionForm.objects.update_or_create(
    dcf_ide='...',
    dcf_ide='...',
    other: '...',
)

You cannot pass the same keyword argument twice to a function. In fact, with what you provided us, the dcf_ide=record['dcf_ide'] argument does not have any sense since you then pass the entire record dict as kwargs.

Doing DataCorrectionForm.objects.update_or_create(**record) is enough.

  • Related