These are chat archives for django/django

19th
Jun 2017
Anish Shrestha
@annshress
Jun 19 2017 03:20
if you are not comfortable with it, change the parent folder name.
@pydo
John
@flyboy1565
Jun 19 2017 03:28
@pydo , you can even create you own template...
Kyle Mulka
@mulka
Jun 19 2017 06:32
@pydo not sure where you are getting import errors. the default setup works ok for me. how are you running django?
:]
@elcolie
Jun 19 2017 06:56

Hi. AttributeError: Cannot use remove() on a ManyToManyField which specifies an intermediary model

class CustomerInformation(AbstractSoftModelController, AbstractAddress, AbstractImport):
            license_plates = models.ManyToManyField(LicensePlate,
                                            through='LicensePlateCustomerInformation',
                                            blank=True)
class LicensePlate(models.Model):
            .... plain primitive fields
class LicensePlateCustomerInformation(AbstractSoftModelController):
    license_plate = models.ForeignKey(LicensePlate, verbose_name=_('License plate'))
    customer_info = models.ForeignKey(CustomerInformation, verbose_name=_('Customer information'))

My unittest

    def test_remove_license_plates(self):
        """No way to do by endpoint. Use `through` feature in m2m"""
        mommy.make(LicensePlate, _quantity=1, number='333')
        mommy.make(LicensePlate, _quantity=1, number='222')
        mommy.make(CustomerInformation, dealer=Dealer.objects.get(name='jjj'))
        cust_info = CustomerInformation.objects.first()
        plate1 = LicensePlate.objects.get(number='333')
        plate2 = LicensePlate.objects.get(number='222')
        LicensePlateCustomerInformation.objects.bulk_create([
            LicensePlateCustomerInformation(
                license_plate=plate1, customer_info=cust_info,
                created_user=User.objects.first(), updated_user=User.objects.first()),
            LicensePlateCustomerInformation(
                license_plate=plate2, customer_info=cust_info,
                created_user=User.objects.first(), updated_user=User.objects.first()),
        ])
        import pdb;
        pdb.set_trace()
        assert 2 == cust_info.license_plates.count()
        cust_info.license_plates.remove(plate1)
        assert 1 == LicensePlateCustomerInformation.objects.count()

        cust_info = CustomerInformation.objects.first()

        assert 1 == cust_info.license_plates.count()

Where am I wrong?

Kyle Mulka
@mulka
Jun 19 2017 06:59
@elcolie Do you need LicensePlateCustomerInformation? I wonder if removing it would help
Alternatively, you can probably delete the LicensePlateCustomerInformation object that you want to delete from the database instead of trying to do it through the cust_info
:]
@elcolie
Jun 19 2017 07:01
One moment.
Creating test database for alias 'default'...
F
======================================================================
FAIL: test_remove_license_plates (eneos.apps.customers.tests.CustomerInformationAPITestCase)
No way to do by endpoint. Use `through` feature in m2m
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/el/Code/eneos-pos-web/eneos/apps/customers/tests.py", line 241, in test_remove_license_plates
    assert 1 == LicensePlateCustomerInformation.objects.count()
AssertionError

----------------------------------------------------------------------
Ran 1 test in 0.096s

FAILED (failures=1)
Destroying test database for alias 'default'...
Kyle Mulka
@mulka
Jun 19 2017 07:04
maybe replace the remove line with something like this: LicensePlateCustomerInformation.objects.get(customer_info=cust_info, license_plate=plate1).delete()
:]
@elcolie
Jun 19 2017 07:04
OK. One moment
LicensePlateCustomerInformation.objects.get(license_plate=plate2, customer_info=cust_info).delete()
Here is my error
Creating test database for alias 'default'...
F
======================================================================
FAIL: test_remove_license_plates (eneos.apps.customers.tests.CustomerInformationAPITestCase)
No way to do by endpoint. Use `through` feature in m2m
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/el/Code/eneos-pos-web/eneos/apps/customers/tests.py", line 244, in test_remove_license_plates
    assert 1 == cust_info.license_plates.count()
AssertionError

----------------------------------------------------------------------
Ran 1 test in 0.096s

FAILED (failures=1)
Destroying test database for alias 'default'...
Kyle Mulka
@mulka
Jun 19 2017 07:10
maybe that query is getting cached
:]
@elcolie
Jun 19 2017 07:11
    def test_remove_license_plates(self):
        """No way to do by endpoint. Use `through` feature in m2m"""
        mommy.make(LicensePlate, _quantity=1, number='333')
        mommy.make(LicensePlate, _quantity=1, number='222')
        mommy.make(CustomerInformation, dealer=Dealer.objects.get(name='jjj'))
        cust_info = CustomerInformation.objects.first()
        plate1 = LicensePlate.objects.get(number='333')
        plate2 = LicensePlate.objects.get(number='222')
        LicensePlateCustomerInformation.objects.bulk_create([
            LicensePlateCustomerInformation(
                license_plate=plate1, customer_info=cust_info,
                created_user=User.objects.first(), updated_user=User.objects.first()),
            LicensePlateCustomerInformation(
                license_plate=plate2, customer_info=cust_info,
                created_user=User.objects.first(), updated_user=User.objects.first()),
        ])
        # Confirm LicensePlateCustomerInformation is working correctly
        assert 2 == cust_info.license_plates.count()

        # Remove plate from customer
        # LicensePlateCustomerInformation.objects.filter(license_plate=plate2, customer_info=cust_info).delete()
        LicensePlateCustomerInformation.objects.get(license_plate=plate2, customer_info=cust_info).delete()
        assert 1 == LicensePlateCustomerInformation.objects.count()
        cust_info = CustomerInformation.objects.first()
        assert 1 == cust_info.license_plates.count()
I hit it twice. The error still persist
cust_infor get from first() method
Kyle Mulka
@mulka
Jun 19 2017 07:24
what does tests.py look like now?
:]
@elcolie
Jun 19 2017 07:25
class CustomerInformationAPITestCase(APITestCase):
    def setUp(self):
        self.dealer = mommy.make(Dealer, name='jjj')
        license_plate = mommy.make(LicensePlate)
        self.license_plate_id = license_plate.id
        self.user = User.objects.create_user(username='test', password='test')
        mommy.make(UserProfile, user=self.user)
        self.client.force_authenticate(self.user)

        self.data = {
            'phone_number_1': 'number1',
            'phone_number_2': '',
            'address_line_1': '',
            'address_line_2': '',
            'prefecture': '',
            'town': '',
            'city': '',
            'building': '',
            'postal_code': '',
            'location_position': None,
            'is_imported': False,
            'email_notification': CustomerInformation.UNKNOWN,
            'letter_notification': CustomerInformation.UNKNOWN,
            'storefront_notification': CustomerInformation.UNKNOWN,
            'announcement_notification': CustomerInformation.UNKNOWN,
            'family_name': 'Lars Hawkins',
            'given_name': 'Reagan Ashley',
            'email': 'hilacog@yahoo.com',
            'profile_picture': None,
            'dealer': self.dealer.id,
            'license_plate_id': self.license_plate_id,
            'license_plates': []
        }
        self.full = {
            'phone_number_1': '1234567890',
            'phone_number_2': '0987654321',
            'address_line_1': 'bkk',
            'address_line_2': 'tokyo',
            'prefecture': 'bkk',
            'town': 'bkk',
            'city': 'bkk',
            'building': 'siam@siam building',
            'postal_code': '10330',
            'email_notification': CustomerInformation.UNKNOWN,
            'letter_notification': CustomerInformation.UNKNOWN,
            'storefront_notification': CustomerInformation.UNKNOWN,
            'announcement_notification': CustomerInformation.UNKNOWN,
            'family_name': 'Lars Hawkins',
            'given_name': 'Reagan Ashley',
            'email': 'hilacog@yahoo.com',
            'dealer': self.dealer.id,
            'license_plate_id': self.license_plate_id,
        }
        self.url = reverse_lazy('api:customer_info-list')
    def test_remove_license_plates(self):
        """No way to do by endpoint. Use `through` feature in m2m"""
        mommy.make(LicensePlate, _quantity=1, number='333')
        mommy.make(LicensePlate, _quantity=1, number='222')
        mommy.make(CustomerInformation, dealer=Dealer.objects.get(name='jjj'))
        cust_info = CustomerInformation.objects.first()
        plate1 = LicensePlate.objects.get(number='333')
        plate2 = LicensePlate.objects.get(number='222')
        LicensePlateCustomerInformation.objects.bulk_create([
            LicensePlateCustomerInformation(
                license_plate=plate1, customer_info=cust_info,
                created_user=User.objects.first(), updated_user=User.objects.first()),
            LicensePlateCustomerInformation(
                license_plate=plate2, customer_info=cust_info,
                created_user=User.objects.first(), updated_user=User.objects.first()),
        ])
        # Confirm LicensePlateCustomerInformation is working correctly
        assert 2 == cust_info.license_plates.count()
        assert 2 == LicensePlate.objects.count()

        # Remove plate from customer
        LicensePlateCustomerInformation.objects.filter(license_plate=plate2, customer_info=cust_info).delete()
        # LicensePlateCustomerInformation.objects.get(license_plate=plate2, customer_info=cust_info).delete()

        assert 1 == LicensePlateCustomerInformation.objects.count()
        cust_info = CustomerInformation.objects.first()
        assert 1 == cust_info.license_plates.count()
Kyle Mulka
@mulka
Jun 19 2017 07:29
weird. almost the same code is working for me. what version of django are you using?
:]
@elcolie
Jun 19 2017 07:30
django==1.10.6
Kyle Mulka
@mulka
Jun 19 2017 07:34
code looks fine to me. not sure why it would still be giving you an error
why did you switch from get to filter?
:]
@elcolie
Jun 19 2017 07:40
Just did an experiment. But the code remains same error
Anish Shrestha
@annshress
Jun 19 2017 08:41
Anyone know how can i achieve this:
queryset.annotate(
    Case(
        When(field1 <= field2 <= field3, then=True),
        default=False
))
Roman Tolkachyov
@romantolkachyov
Jun 19 2017 08:45
@elcolie what actually LicensePlateCustomerInformation.objects.count() returns?
@annshress just use Q. From docs:
>>> # Complex conditions can be created using Q objects
>>> When(Q(name__startswith="John") | Q(name__startswith="Paul"),
...      then='name')
Anish Shrestha
@annshress
Jun 19 2017 08:47
@romantolkachyov Thanks man :clap:
:]
@elcolie
Jun 19 2017 08:49
@romantolkachyov LicensePlateCustomerInformation.objects.count() returns relational relation of LicensePlate VS Customer. Refer to my model
class CustomerInformation(AbstractSoftModelController, AbstractAddress, AbstractImport):
            license_plates = models.ManyToManyField(LicensePlate,
                                            through='LicensePlateCustomerInformation',
                                            blank=True)
Roman Tolkachyov
@romantolkachyov
Jun 19 2017 08:50
@elcolie No, it must return integer.
:]
@elcolie
Jun 19 2017 08:51
Ah yes. It returns counting of relation
Roman Tolkachyov
@romantolkachyov
Jun 19 2017 08:53
@elcolie and what is actual value when assertion fails? I mean there're may be records from other tests, for ex; or you don't cleanup your db between test runs.
:]
@elcolie
Jun 19 2017 08:55
assert 1 == cust_info.license_plates.count() This line failed.
It is 2 not 1 as I expected. It is unittesting the database is always clean up
@romantolkachyov
Roman Tolkachyov
@romantolkachyov
Jun 19 2017 08:56
@elcolie ok, and what delete returns (also integer)?
:]
@elcolie
Jun 19 2017 08:56
Anyway I will disable all the rest of the tests. I want to do experiment too. Please wait a moment
Roman Tolkachyov
@romantolkachyov
Jun 19 2017 08:58
@elcolie I think you use wrong conditions for filter in delete or you must filter before count the same way:
assert 1 == LicensePlateCustomerInformation.objects.filter(customer_info=cust_info).count()
@elcolie and I advise you to use factory_boy to simplify your tests.
:]
@elcolie
Jun 19 2017 09:03

I confirm the other tests disable and still failed at same line.

LicensePlateCustomerInformation.objects.filter(customer_info=cust_info).count()
Filter or not is not a concern. If the relation is removed it will deduce anyway

LicensePlateCustomerInformation.objects.get(license_plate=plate2, customer_info=cust_info).delete()
returns None
@romantolkachyov

factory_boy. I never heard this. Let me try it when I start new project. Thank you

@romantolkachyov
I change my teststo follow your suggestion. The problem still persist

    def test_remove_license_plates(self):
        """No way to do by endpoint. Use `through` feature in m2m"""
        mommy.make(LicensePlate, _quantity=1, number='333')
        mommy.make(LicensePlate, _quantity=1, number='222')
        mommy.make(CustomerInformation, dealer=Dealer.objects.get(name='jjj'))
        cust_info = CustomerInformation.objects.first()
        plate1 = LicensePlate.objects.get(number='333')
        plate2 = LicensePlate.objects.get(number='222')
        LicensePlateCustomerInformation.objects.bulk_create([
            LicensePlateCustomerInformation(
                license_plate=plate1, customer_info=cust_info,
                created_user=User.objects.first(), updated_user=User.objects.first()),
            LicensePlateCustomerInformation(
                license_plate=plate2, customer_info=cust_info,
                created_user=User.objects.first(), updated_user=User.objects.first()),
        ])
        # Confirm LicensePlateCustomerInformation is working correctly
        assert 2 == cust_info.license_plates.count()
        assert 3 == LicensePlate.objects.count()

        # Remove plate from customer
        ans = LicensePlateCustomerInformation.objects.get(license_plate=plate2, customer_info=cust_info).delete()
        import pdb; pdb.set_trace()

        assert 1 == LicensePlateCustomerInformation.objects.filter(customer_info=cust_info).count()
        cust_info = CustomerInformation.objects.first()
        assert 1 == cust_info.license_plates.count()

And here is my pdb.set_trace()

> /Users/el/Code/eneos-pos-web/eneos/apps/customers/tests.py(244)test_remove_license_plates()
-> assert 1 == LicensePlateCustomerInformation.objects.filter(customer_info=cust_info).count()
(Pdb) n
> /Users/el/Code/eneos-pos-web/eneos/apps/customers/tests.py(245)test_remove_license_plates()
-> cust_info = CustomerInformation.objects.first()
(Pdb) n
> /Users/el/Code/eneos-pos-web/eneos/apps/customers/tests.py(246)test_remove_license_plates()
-> assert 1 == cust_info.license_plates.count()
(Pdb) n
AssertionError
> /Users/el/Code/eneos-pos-web/eneos/apps/customers/tests.py(246)test_remove_license_plates()
-> assert 1 == cust_info.license_plates.count()
(Pdb) cust_info.license_plates.count()
2
Roman Tolkachyov
@romantolkachyov
Jun 19 2017 10:20
@elcolie this is good, we fail on another line, right?
:]
@elcolie
Jun 19 2017 10:21

@romantolkachyov
It fails at last line.

(Pdb) cust_info.license_plates.count()
2

I expects 1 not 2

Roman Tolkachyov
@romantolkachyov
Jun 19 2017 10:21

@elcolie remove last

cust_info = CustomerInformation.objects.first()

and try again.

@elcolie I mean

assert 1 == LicensePlateCustomerInformation.objects.filter(customer_info=cust_info).count()

is now True.

:]
@elcolie
Jun 19 2017 10:25
    def test_remove_license_plates(self):
        """No way to do by endpoint. Use `through` feature in m2m"""
        mommy.make(LicensePlate, _quantity=1, number='333')
        mommy.make(LicensePlate, _quantity=1, number='222')
        mommy.make(CustomerInformation, dealer=Dealer.objects.get(name='jjj'))
        cust_info = CustomerInformation.objects.first()
        plate1 = LicensePlate.objects.get(number='333')
        plate2 = LicensePlate.objects.get(number='222')
        LicensePlateCustomerInformation.objects.bulk_create([
            LicensePlateCustomerInformation(
                license_plate=plate1, customer_info=cust_info,
                created_user=User.objects.first(), updated_user=User.objects.first()),
            LicensePlateCustomerInformation(
                license_plate=plate2, customer_info=cust_info,
                created_user=User.objects.first(), updated_user=User.objects.first()),
        ])
        # Confirm LicensePlateCustomerInformation is working correctly
        assert 2 == cust_info.license_plates.count()
        assert 3 == LicensePlate.objects.count()

        # Remove plate from customer
        ans = LicensePlateCustomerInformation.objects.get(license_plate=plate2, customer_info=cust_info).delete()

        assert 1 == LicensePlateCustomerInformation.objects.filter(customer_info=cust_info).count()
        assert 1 == cust_info.license_plates.count()
Same error
Creating test database for alias 'default'...
F
======================================================================
FAIL: test_remove_license_plates (eneos.apps.customers.tests.CustomerInformationAPITestCase)
No way to do by endpoint. Use `through` feature in m2m
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/el/Code/eneos-pos-web/eneos/apps/customers/tests.py", line 244, in test_remove_license_plates
    assert 1 == cust_info.license_plates.count()
AssertionError

----------------------------------------------------------------------
Ran 1 test in 0.100s

FAILED (failures=1)
Destroying test database for alias 'default'...
@romantolkachyov
Roman Tolkachyov
@romantolkachyov
Jun 19 2017 10:40
@elcolie are you using mysql?
:]
@elcolie
Jun 19 2017 10:40
@romantolkachyov I use Postgres.
Roman Tolkachyov
@romantolkachyov
Jun 19 2017 10:44
@elcolie another solution :-) is to use all() to prevent cache:
assert 1 == cust_info.license_plates.all().count()
Anish Shrestha
@annshress
Jun 19 2017 10:46
Wow, I didn't know these things(caching) can happen.
Anish Shrestha
@annshress
Jun 19 2017 10:49
Hmm, thanks again. good to know.
:]
@elcolie
Jun 19 2017 10:51
assert 1 == cust_info.license_plates.all().count()
The error is:
Creating test database for alias 'default'...
F
======================================================================
FAIL: test_remove_license_plates (eneos.apps.customers.tests.CustomerInformationAPITestCase)
No way to do by endpoint. Use `through` feature in m2m
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/el/Code/eneos-pos-web/eneos/apps/customers/tests.py", line 244, in test_remove_license_plates
    assert 1 == cust_info.license_plates.all().count()
AssertionError

----------------------------------------------------------------------
Ran 1 test in 0.107s

FAILED (failures=1)
Destroying test database for alias 'default'...
Roman Tolkachyov
@romantolkachyov
Jun 19 2017 10:52
@elcolie you also can set _result_cache to None on your queryset to prevent count cache (look at link to django sources in my previouse message). But it is internal and it would be hard to maintain. Other solutions can be found here https://stackoverflow.com/questions/3346124/how-do-i-force-django-to-ignore-any-caches-and-reload-data
cust_info.license_plates._result_cache = None
@elcolie count cached exactly in this line:
assert 2 == cust_info.license_plates.count()
:]
@elcolie
Jun 19 2017 10:59
    def test_remove_license_plates(self):
        """No way to do by endpoint. Use `through` feature in m2m"""
        mommy.make(LicensePlate, _quantity=1, number='333')
        mommy.make(LicensePlate, _quantity=1, number='222')
        mommy.make(CustomerInformation, dealer=Dealer.objects.get(name='jjj'))
        cust_info = CustomerInformation.objects.first()
        plate1 = LicensePlate.objects.get(number='333')
        plate2 = LicensePlate.objects.get(number='222')
        LicensePlateCustomerInformation.objects.bulk_create([
            LicensePlateCustomerInformation(
                license_plate=plate1, customer_info=cust_info,
                created_user=User.objects.first(), updated_user=User.objects.first()),
            LicensePlateCustomerInformation(
                license_plate=plate2, customer_info=cust_info,
                created_user=User.objects.first(), updated_user=User.objects.first()),
        ])
        # Confirm LicensePlateCustomerInformation is working correctly
        assert 2 == cust_info.license_plates.count()
        assert 3 == LicensePlate.objects.count()

        # Remove plate from customer
        ans = LicensePlateCustomerInformation.objects.get(license_plate=plate2, customer_info=cust_info).delete()
        cust_info.license_plates._result_cache = None

        assert 1 == LicensePlateCustomerInformation.objects.filter(customer_info=cust_info).count()
        assert 1 == cust_info.license_plates.all().count()
Same error
Creating test database for alias 'default'...
F
======================================================================
FAIL: test_remove_license_plates (eneos.apps.customers.tests.CustomerInformationAPITestCase)
No way to do by endpoint. Use `through` feature in m2m
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/el/Code/eneos-pos-web/eneos/apps/customers/tests.py", line 245, in test_remove_license_plates
    assert 1 == cust_info.license_plates.all().count()
AssertionError

----------------------------------------------------------------------
Ran 1 test in 0.101s

FAILED (failures=1)
Destroying test database for alias 'default'...

I started to think it is caching in the unittest

    def test_remove_license_plates(self):
        """No way to do by endpoint. Use `through` feature in m2m"""
        mommy.make(LicensePlate, _quantity=1, number='333')
        mommy.make(LicensePlate, _quantity=1, number='222')
        mommy.make(CustomerInformation, dealer=Dealer.objects.get(name='jjj'))
        cust_info = CustomerInformation.objects.first()
        plate1 = LicensePlate.objects.get(number='333')
        plate2 = LicensePlate.objects.get(number='222')
        LicensePlateCustomerInformation.objects.bulk_create([
            LicensePlateCustomerInformation(
                license_plate=plate1, customer_info=cust_info,
                created_user=User.objects.first(), updated_user=User.objects.first()),
            LicensePlateCustomerInformation(
                license_plate=plate2, customer_info=cust_info,
                created_user=User.objects.first(), updated_user=User.objects.first()),
        ])
        # Confirm LicensePlateCustomerInformation is working correctly
        assert 2 == cust_info.license_plates.count()
        assert 3 == LicensePlate.objects.count()

        # Remove plate from customer
        ans = LicensePlateCustomerInformation.objects.get(license_plate=plate2, customer_info=cust_info).delete()
        from django.core.cache import cache
        cache.clear()
        cust_info = CustomerInformation.objects.first()

        assert 1 == LicensePlateCustomerInformation.objects.filter(customer_info=cust_info).count()
        assert 1 == cust_info.license_plates.all().count()

Same error

Creating test database for alias 'default'...
F
======================================================================
FAIL: test_remove_license_plates (eneos.apps.customers.tests.CustomerInformationAPITestCase)
No way to do by endpoint. Use `through` feature in m2m
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/el/Code/eneos-pos-web/eneos/apps/customers/tests.py", line 247, in test_remove_license_plates
    assert 1 == cust_info.license_plates.all().count()
AssertionError

----------------------------------------------------------------------
Ran 1 test in 0.119s

FAILED (failures=1)
Destroying test database for alias 'default'...
Pavel Burns
@63phc
Jun 19 2017 11:15
hey , help me with memcached , im install python-memcached and write in settings CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 'LOCATION': '127.0.0.1:11211', } }
I'm looking in the debugger, it does not work, how to check or what I forgot
Anish Shrestha
@annshress
Jun 19 2017 11:19
@elcolie post your story when you're done :tongue: Will be a great story.
Roman Tolkachyov
@romantolkachyov
Jun 19 2017 11:20
@elcolie try to wrap it into transation and commit before check count. Cache may be from cacheops package (if you use it).
@elcolie also try to step into count call using pdb and check if _result_cache is really None.
djangoReactGuy
@SanskarSans
Jun 19 2017 15:41
I am learning django-rest-framework. Can anyone please help me on the best design practice, please? I need the suggestions on listing FK elements. How should i list it? Where should i list it? I have created a gist of the practice I am doing on rest_framework. Looking at the model regarding property_type and category, how should i arrange it? https://gist.github.com/SanskarSans/84c2ba3a5087f271a8273c8fdd5ed8f9