CẬP NHẬT 15.3.2017:
Tôi đã mở một vấn đề Django về vấn đề này và dường như nó được chấp nhận sơ bộ tại đây:
https://code.djangoproject.com/ticket/27825
Kinh nghiệm của tôi là khi sử dụng lớp Constructor( ORM) bằng các tham chiếu với Django, 1.10.5có thể có một số điểm không nhất quán trong dữ liệu (ví dụ: các thuộc tính của đối tượng được tạo có thể lấy loại dữ liệu đầu vào thay vì kiểu đúc của thuộc tính đối tượng ORM) :
models
class Payment(models.Model):
amount_cash = models.DecimalField()
some_test.py - object.create
Class SomeTestCase:
def generate_orm_obj(self, _constructor, base_data=None, modifiers=None):
objs = []
if not base_data:
base_data = {'amount_case': 123.00}
for modifier in modifiers:
actual_data = deepcopy(base_data)
actual_data.update(modifier)
# Hacky fix,
_obj = _constructor.objects.create(**actual_data)
print(type(_obj.amount_cash)) # Decimal
assert created
objs.append(_obj)
return objs
some_test.py - Constructor()
Class SomeTestCase:
def generate_orm_obj(self, _constructor, base_data=None, modifiers=None):
objs = []
if not base_data:
base_data = {'amount_case': 123.00}
for modifier in modifiers:
actual_data = deepcopy(base_data)
actual_data.update(modifier)
# Hacky fix,
_obj = _constructor(**actual_data)
print(type(_obj.amount_cash)) # Float
assert created
objs.append(_obj)
return objs