Opened 10 years ago

Closed 10 years ago

#23443 closed Cleanup/optimization (fixed)

Inconsistent FieldError message under some circumstances

Reported by: Thomas C Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Under some circumstances, FieldError are raised with inconsistent messages Cannot resolve keyword u'name' into field. Choices are: name, ...

Reproduced with the following code :

# Model
class Tag(models.Model):
    name = models.CharField(max_length=10)
    parent = models.ForeignKey('self', blank=True, null=True,
            related_name='children')
    category = models.ForeignKey(NamedCategory, null=True, default=None)

# FieldError
Tag.objects.values_list('name__foo')

Change History (6)

comment:2 by Baptiste Mispelon, 10 years ago

Patch needs improvement: set
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

Hi,

I agree that we should try and improve the message.
However, your proposed patch now generates the following message:

Cannot resolve keyword 'foo' into field. Choices are: category, category_id, children, id, name, parent, parent_id

I'm not sure if that's an improvement since this message seems to indicate that doing name__category would be valid.

Instead, I think the error message should say that name is not a field you can "jump" with the double underscore syntax (I'm not sure what the correct term for that is).
Looking at the code, this will probably require some more in-depth changes since the exit condition of the loop conflates non-existing field names and fields that can't be "jumped".

Thanks.

comment:3 by Thomas C, 10 years ago

You're right indeed, I missed that part of the message.

comment:4 by Thomas C, 10 years ago

I updated my PR after bmispelon comment.

comment:5 by Tim Graham, 10 years ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

Don't forget to uncheck "Patch needs improvement" so the ticket appears in the review queue. The patch looks okay to me, but would be good to have another review from an ORM expert to make sure nothing has been overlooked.

comment:6 by Loic Bistuer <loic.bistuer@…>, 10 years ago

Resolution: fixed
Status: newclosed

In e008a10c2ffef7d64100bcbe83ef2c5f4b1b2168:

Fixed #23443 -- Corrected erroneous FieldError message.

Thanks Tim Graham for the review.

Note: See TracTickets for help on using tickets.
Back to Top