0

I am now testing on my database schema. My ORM is django==1.10.6

Requirements:
customer can has multiple license_plates
And license_plate can belongs to multiple customers
For example: One family has 5 men they are dad, mom, son, and daughter. Suppose this family has 2 cars. Therefore m2m is the way to go

Problem:
Django ORM seems to not remove the instance relation in the LicensePlateCustomerInformation manager
cust_info.license_plates.count() returns 2
Therefore this line will be failed
assert 1 == cust_info.license_plates.count()

tests.py

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')

Here is my problem on m2m

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
    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()

Here are my models

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'))
joe
  • 8,383
  • 13
  • 61
  • 109
  • this line does not work? `LicensePlateCustomerInformation.objects.get(license_plate=plate2, customer_info=cust_info).delete()` and you mean by instance is that you cannot delete `license_plate` and `customer_info `? – Shift 'n Tab Jun 20 '17 at 03:06
  • That line works. The problem is cust_info.license_plates.count() return `2` not `1` – joe Jun 20 '17 at 04:25
  • that line is working yes, but it wont delete the related instances to that model – Shift 'n Tab Jun 20 '17 at 04:38
  • This may be interesting to you https://stackoverflow.com/q/7230276/6143656 – Shift 'n Tab Jun 20 '17 at 04:50
  • Yeah I read it. But it is not my case. By that it means I have no choice except wipe out all the relation. Thank you. – joe Jun 20 '17 at 07:13

0 Answers0