tkhyn / django-gm2m

MIT License
36 stars 23 forks source link

Confusing error message with through model #44

Open tkhyn opened 5 years ago

tkhyn commented 5 years ago

Original report by Olivier Lefevre (Bitbucket: olefevre, GitHub: olefevre).


I created and used this through model:

class Group2Target(models.Model):
    group = models.ForeignKey(Group)
    target = GenericForeignKey(ct_field='target_type', fk_field='target_id')
    target_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    target_id = models.PositiveIntegerField()

(never mind what Group is: it is not the problem field). When I try to execute ControlGroup2Target.objects.values('target') I get:

FieldError: Cannot resolve keyword 'target' into field. Choices are: group, group_id, id, target, target_id, target_type, target_type_id

So you have to use target_id instead but the message is really confusing because "target" is listed as one of the valid choices!

tkhyn commented 5 years ago

Original comment by Thomas Khyn (Bitbucket: tkhyn, GitHub: tkhyn).


Maybe there is something I missed, but no use of GM2MField is made here, so it's unlikely to be related to django-gm2m. I guess you need to report the issue to the django developers.

tkhyn commented 5 years ago

Original comment by Olivier Lefevre (Bitbucket: olefevre, GitHub: olefevre).


Yes this is related to django-gm2m: I said it is a through model. In another model there is this:

targets = GM2MField(through='Group2Target')
tkhyn commented 5 years ago

Original comment by Thomas Khyn (Bitbucket: tkhyn, GitHub: tkhyn).


OK I see. Can you send the full stack trace leading to the exception to know if there is any way to catch it at the django-gm2m level?

tkhyn commented 5 years ago

Original comment by Olivier Lefevre (Bitbucket: olefevre, GitHub: olefevre).


The requested stack trace. stacktrace.txt