django-bulk-update-or-create icon indicating copy to clipboard operation
django-bulk-update-or-create copied to clipboard

Invalid default value for match_field

Open NikolayCherniy opened this issue 2 years ago • 4 comments

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

NikolayCherniy avatar Oct 13 '21 14:10 NikolayCherniy

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?

fopina avatar Oct 13 '21 22:10 fopina

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"

NikolayCherniy avatar Oct 14 '21 07:10 NikolayCherniy

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

fopina avatar Oct 14 '21 07:10 fopina

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'

NikolayCherniy avatar Oct 14 '21 10:10 NikolayCherniy