fopina / django-bulk-update-or-create

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

Invalid default value for match_field #28

Open NikolayCherniy opened 3 years ago

NikolayCherniy commented 3 years ago

Get exception FieldDoesNotExist when match_field is not specified. match_field='pk' match_field = (match_field,) if isinstance(match_field, str) else match_field _match_fields = [self.model._meta.get_field(name) for name in match_field]

Easy way to reproduce from django.contrib.auth import get_user_model User = get_user_model() User._meta.get_field("pk") django.core.exceptions.FieldDoesNotExist: User has no field named 'pk'

Django==3.2.8 django-bulk-update-or-create==0.3.0

fopina commented 3 years ago

Not sure I understand the issue. If the model does not have a "pk" field, it's expected that match_field should fail if you use the default. Just specify the one you want. Or have I misunderstood the report?

NikolayCherniy commented 3 years ago

I think it would be more clear if the default value will be "id", cause every model has field "id" from the stock, and doesn't have field "pk". Or pk field name should be got through "_meta.pk.attname" method or replaced with "_get_pk_val"

fopina commented 3 years ago

I was under the impression it would be otherwise actually. The default primary key is usually “id” but “pk” would always translate to the actual one (even if primary key was manually set on another model)

I’ll add a test for this and update accordingly

NikolayCherniy commented 3 years ago

It works only with model attribute "pk" for example

User = get_user_model()
User.objects.create(...)
User.objects.first().pk

works as you expect, but for get_field that doesn't work:

User._meta.get_field("pk")
django.core.exceptions.FieldDoesNotExist: User has no field named 'pk'