Fixed #29782 -- Added better error message when filtering queryset with AnonymousUser.

This commit is contained in:
Ramon Saraiva 2018-09-25 18:16:21 -03:00 committed by Tim Graham
parent 18098d261f
commit 2349cbd909
3 changed files with 12 additions and 0 deletions

View File

@ -694,6 +694,7 @@ answer newbie questions, and generally made Django that much better:
Ramez Ashraf <ramezashraf@gmail.com>
Ramin Farajpour Cami <ramin.blackhat@gmail.com>
Ramiro Morales <ramiro@rmorales.net>
Ramon Saraiva <ramonsaraiva@gmail.com>
Ram Rachum <ram@rachum.com>
Randy Barlow <randy@electronsweatshop.com>
Raphaël Barrois <raphael.barrois@m4x.org>

View File

@ -383,6 +383,9 @@ class AnonymousUser:
def __hash__(self):
return 1 # instances always return the same hash value
def __int__(self):
raise TypeError('Cannot cast AnonymousUser to int. Are you trying to use it in place of User?')
def save(self):
raise NotImplementedError("Django doesn't provide a DB representation for AnonymousUser.")

View File

@ -345,6 +345,14 @@ class AnonymousUserTests(SimpleTestCase):
def test_hash(self):
self.assertEqual(hash(self.user), 1)
def test_int(self):
msg = (
'Cannot cast AnonymousUser to int. Are you trying to use it in '
'place of User?'
)
with self.assertRaisesMessage(TypeError, msg):
int(self.user)
def test_delete(self):
with self.assertRaisesMessage(NotImplementedError, self.no_repr_msg):
self.user.delete()