我现在正在测试我的数据库架构。我的ORM是django==1.10.6
的要求:
customer
可以包含多个license_plate
个
license_plate
可以属于多个customers
例如:
一个家庭有5个男人,他们是爸爸,妈妈,儿子和女儿。假设这个家庭有2辆车。因此m2m
是要走的路
问题:
Django ORM似乎没有删除LicensePlateCustomerInformation
经理中的实例关系
cust_info.license_plates.count()
返回2
因此这条线路将失败
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')
以下是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()
以下是我的模特
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'))