Hello,

This query is for Django version 1.9 and Python 3.5 with Django Rest 
Framework 3.5.4
*(I know this version is outdated, but moving ahead of this version as of 
now is out of reach for the purpose of solving this query)*

*Exact Error observed:*

    self.client.force_login(self.user)
  File 
"/Users/ds/.pyenv/versions/3.5.7/envs/work_env/lib/python3.5/site-packages/django/test/client.py"
, line 608, in force_login
    self._login(user)
  File 
"/Users/ds/.pyenv/versions/3.5.7/envs/work_env/lib/python3.5/site-packages/django/test/client.py"
, line 621, in _login
    login(request, user)
  File 
"/Users/ds/.pyenv/versions/3.5.7/envs/work_env/lib/python3.5/site-packages/django/contrib/auth/__init__.py"
, line 117, in login
    user_logged_in.send(sender=user.__class__, request=request, user=user)
  File 
"/Users/ds/.pyenv/versions/3.5.7/envs/work_env/lib/python3.5/site-packages/django/dispatch/dispatcher.py"
, line 192, in send
    response = receiver(signal=self, sender=sender, **named)
  File 
"/Users/ds/.pyenv/versions/3.5.7/envs/work_env/lib/python3.5/site-packages/django/contrib/auth/models.py"
, line 23, in update_last_login
    user.save(update_fields=['last_login'])
  File 
"/Users/ds/.pyenv/versions/3.5.7/envs/work_env/lib/python3.5/site-packages/django/contrib/auth/base_user.py"
, line 74, in save
    super(AbstractBaseUser, self).save(*args, **kwargs)
  File 
"/Users/ds/.pyenv/versions/3.5.7/envs/work_env/lib/python3.5/site-packages/django/db/models/base.py"
, line 708, in save
    force_update=force_update, update_fields=update_fields)
  File 
"/Users/ds/.pyenv/versions/3.5.7/envs/work_env/lib/python3.5/site-packages/django/db/models/base.py"
, line 736, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, 
update_fields)
  File 
"/Users/ds/.pyenv/versions/3.5.7/envs/work_env/lib/python3.5/site-packages/django/db/models/base.py"
, line 805, in _save_table
    raise DatabaseError("Save with update_fields did not affect any rows.")
django.db.utils.DatabaseError: Save with update_fields did not affect any 
rows.


*Scenario:*

from django.test import TestCase, LiveServerTestCase

from paths.factories import UserFactory


class ATestClass(LiveServerTestCase):

    @classmethod
    def setUpClass(cls):
        """
        Test Data common across all tests
        """

        super().setUpClass()

        cls.user = UserFactory(is_staff=False, is_superuser=False)

    def test_one(self):
        """
        Login and then do some actions
        """
        self.client.force_login(self.user)

        # do something
        self.assertTrue(True)

    def test_two(self):
        """
        Login and do some actions
        """

        self.client.force_login(self.user)

        # do something
        self.assertFalse(False)


However, if I change the base class from LiveServerTestCase to TestCase, it 
works fine without any errors. I understand this was probably addressed 
in https://code.djangoproject.com/ticket/26823 but can someone help with 
any way in which I can patch this in my tests to avoid raising this issue?

*Why LiveServerTestCase?*

   - My tests need access to live_server_url, hence I am forced to inherit 
   the test class from LiveServerTestCase

*What have I tried?*

   - Logging in in setUp method instead of directly within the test - 
   Doesn't help
   - Disconnecting update_last_login signal by using 
   user_logged_in.disconnect(update_last_login) - Doesn't help
   - Manually setting the last_login field on user object to None before 
   force_login statement - Doesn't help

Thank you,
Deep S

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/623f7119-d398-4254-b80d-ead27d8f4fbe%40googlegroups.com.

Reply via email to