﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
17851	Db Field instances cannot be compared to other objects	Simon Charette	Simon Charette	"[https://github.com/django/django/blob/master/django/db/models/fields/__init__.py#L126-L128 `django.db.models.fields.Field.__cmp__`] assumes that it's always compared to a another `Field` instance thus raising an `AttributeError` when the following code is executed:

{{{#!python
from django.db.models.fields import Field

Field() == None
}}} 

It should make sure that `other` is a `Field` instance before accessing the `creation_counter` property and if not return `NotImplemented`.

I stumbled on this issue when trying to use a `PickledObjectField` to store `Field` instance. When calling `full_clean` on the field it raised an `AttributeError` because it was trying to compare it `EMPTY_VALUES`: {{{value in EMPTY_VALUES}}}.

I agree that this is NOT a common use case of the Field class however I think that `Field` instances should be able to compare themselves to other objects without raising a confusing `AttributeError`.

I'm attaching a patch that sticks with simple comparison (although we should REALLY move to advanced comparison) with tests.

Here's a chat log I had on #python concerning simple comparison:
{{{
charettes 13:31:23
What should an object that implements __cmp__ method should return if it can't compare to other.
papna 13:31:49
charettes: 1) Don't write __cmp__ methods unless you're targetting mesozoic Python 2) NotImplemented
charettes 13:32:41
papna: But the doc doesn't say that... it says only to use it for advanced comparaison
charettes 13:32:58
papna: http://docs.python.org/reference/datamodel.html#object.__cmp__
papna 13:33:42
charettes: Why are you using __cmp__?
charettes 13:34:42
papna: actually I'm writing a patch for a project that used this method... should I suggest to switch to adavanced comparaison?
papna 13:36:43
charettes: IIRC __cmp__ still uses the paradigm that __eq__ and friends and __add__ and friends and plenty of other stuff does, but I might be wrong. It's not flexible, which is why it has been deprecated well over a decade.
charettes 13:37:27
papna: aight ty for your time
}}}

I can adapt the current patch to use advanced comparison instead if you prefer."	Bug	closed	Database layer (models, ORM)	dev	Normal	fixed	field compare		Accepted	1	0	0	0	0	0
