Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid default value for match_field #28

Open
NikolayCherniy opened this issue Oct 13, 2021 · 4 comments
Open

Invalid default value for match_field #28

NikolayCherniy opened this issue Oct 13, 2021 · 4 comments

Comments

@NikolayCherniy
Copy link

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
Copy link
Owner

fopina commented Oct 13, 2021

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
Copy link
Author

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
Copy link
Owner

fopina commented Oct 14, 2021

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
Copy link
Author

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'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants