Opened 18 years ago

Closed 18 years ago

#1496 closed enhancement (fixed)

[patch] [magic-removal] Equality test doesn't work between models and model wrappers, e.g. User and UserWrapper

Reported by: Antti Kaihola Owned by: Adrian Holovaty
Component: Core (Other) Version:
Severity: minor Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If I have a model with a ForeignKey(User), I can't do this in my template:

{% ifequal user myobject.user %}

but must use

{% ifequal user.id myobject.user.id %}

Attachments (3)

compare-users.diff (1.1 KB ) - added by Antti Kaihola 18 years ago.
models.User and middleware.UserWrapper equality tests for contrib.auth
model_wrapper_equality.diff (669 bytes ) - added by Antti Kaihola 18 years ago.
Enhanced equality test for models; works correctly for wrappers like UserWrapper
models.py (1.5 KB ) - added by Antti Kaihola 18 years ago.
Unit test for enhanced equality test. Could go in tests/modeltests/wrappers/.

Download all attachments as: .zip

Change History (11)

by Antti Kaihola, 18 years ago

Attachment: compare-users.diff added

models.User and middleware.UserWrapper equality tests for contrib.auth

comment:1 by Antti Kaihola, 18 years ago

Summary: Can't test user equality directly, must use user.id[patch] [magic-removal] Can't test user equality directly, must use user.id

The above patch allows for the more intuitive user equality test syntax.

I hope the patch doesn't have bad side-effects.

comment:2 by akaihola <antti.kaihola@…>, 18 years ago

priority: normallowest
Severity: normalminor
Type: defectenhancement

Ok, as I feared, it wasn't that simple. Trouble in admin. I'll try to find what's causing it.

comment:3 by Malcolm Tredinnick <malcolm@…>, 18 years ago

By the way, the patch has problems in the change to middleware.py: it defines __eq__ twice (the second one should be __ne__).

by Antti Kaihola, 18 years ago

Attachment: model_wrapper_equality.diff added

Enhanced equality test for models; works correctly for wrappers like UserWrapper

comment:4 by Antti Kaihola, 18 years ago

Summary: [patch] [magic-removal] Can't test user equality directly, must use user.id[patch] [magic-removal] Equality test doesn't work between models and model wrappers, e.g. User and UserWrapper

Thanks, Malcolm.

I decided to look at the problem from a broader perspective of camparing any models and their wrapper classes. I came up with a different patch, which refines the equality test at the django.db.models.Model level.

The current equality test is based on two criteria. If x is a subclass of django.db.models.Model, x == y is True if:

  1. isinstance(y, x.class), and
  2. both objects have the same primary key value.

I enhanced this logic to work correctly also for wrapper classes like django.contrib.auth.middleware.UserWrapper. The criteria are now:

  1. a) isinstance(y, x.class), OR b) y._meta exists and is the same object as x._meta AND
  2. both objects have the same primary key value.

This works, because UserWrapper passes through all attributes (_meta among them) of the wrapped User object.

I quickly tested several operations in Admin and didn't find anything broken after applying this patch.

I also wrote a unit test (see next patch).

by Antti Kaihola, 18 years ago

Attachment: models.py added

Unit test for enhanced equality test. Could go in tests/modeltests/wrappers/.

comment:5 by Adrian Holovaty, 18 years ago

What problem did you encounter with the first patch? It would be *much* cleaner to make a change to UserWrapper than change the equality logic framework-wide.

comment:6 by jkocherhans, 18 years ago

FWIW UserWrapper needs to die a horrible death, or at least be changed quite a bit. See #1488 for one possible patch. A reasonable solution to that would also resovle this ticket.

comment:7 by Antti Kaihola, 18 years ago

Adrian, no problem with the first patch apart from the typo Malcolm found.

I simply noticed that a generic fix to make wrapper class equality tests work is easy to do on the model level. Is there a drawback there?

comment:8 by Adrian Holovaty, 18 years ago

Resolution: fixed
Status: newclosed

Yeah, the drawback is that your patch solves a non-problem: Wrapper classes aren't a common solution, and it's overengineering to solve this on a generic level. (I can't think of any wrapper class other than the UserWrapper thing.)

And the UserWrapper problem was solved in another ticket -- #1488 and [2590] -- so I'm marking this one as fixed.

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