Django:如何正确使用 ManyToManyField 与 Factory Boy 工厂和序列化程序?
Django: How to Properly Use ManyToManyField with Factory Boy Factories & Serializers?
问题
我使用的模型 class Event
包含 可选 ManyToManyField 到另一个模型 class, User
(不同的事件可以有不同的用户),工厂 class EventFactory
(使用 Factory Boy 库)和序列化程序 EventSerializer
。我相信我已经按照文档进行工厂制作和序列化,但收到错误消息:
ValueError: "< Event: Test Event >" needs to have a value for field "id"
before this many-to-many relationship can be used.
我知道在链接它们之前必须在 ManyToMany 中创建两个模型实例,但我什至没有看到添加发生在哪里!
问题
有人可以阐明如何以我尚未采用的方式使用模型、工厂男孩和序列化程序正确使用 ManyToManyField 吗?
设置
这是我的代码:
models.py
@python_2_unicode_compatible
class Event(CommonInfoModel):
users = models.ManyToManyField(User, blank=True, related_name='events')
# other basic fields...
factories.py
class EventFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.Event
@factory.post_generation
def users(self, create, extracted, **kwargs):
if not create:
# Simple build, do nothing.
return
if extracted:
# A list of users were passed in, use them
# NOTE: This does not seem to be the problem. Setting a breakpoint
# here, this part never even fires
for users in extracted:
self.users.add(users)
serializers.py
class EventSerializer(BaseModelSerializer):
serialization_title = "Event"
# UserSerializer is a very basic serializer, with no nested
# serializers
users = UserSerializer(required=False, many=True)
class Meta:
model = Event
exclude = ('id',)
test.py
class EventTest(APITestCase):
@classmethod
def setUpTestData(cls):
cls.user = User.objects.create_user(email='test@gmail.com',
password='password')
def test_post_create_event(self):
factory = factories.EventFactory.build()
serializer = serializers.EventSerializer(factory)
# IMPORTANT: Calling 'serializer.data' is the exact place the error occurs!
# This error does not occur when I remove the ManyToManyField
res = self.post_api_call('/event/', serializer.data)
版本信息
- Django 1.11
- Python 2.7.10
感谢您提供的任何帮助!
关于错误:
似乎缺少 id
是由于使用 .build()
而不是 .create()
(或只是 EventFactory()
)。前者不保存模型,因此不会得到id
值,而后者会(参考factory docs and model docs)。
我怀疑序列化程序仍然期望对象具有 id
,即使多对多关系是可选的,因为它无法在没有 id
的情况下强制执行潜在关系。
但是,实际任务可能有更简单的解决方案。上面的方法是生成传递给post_api_call()
的POST数据的一种方式。如果此数据是手动创建的,那么工厂和序列化程序都变得不必要了。从测试的角度来看,显式数据方法甚至可能更好,因为您现在可以看到必须产生预期结果的确切数据。而对于工厂和序列化程序方法,它更隐含在测试中实际使用的内容中。
问题
我使用的模型 class Event
包含 可选 ManyToManyField 到另一个模型 class, User
(不同的事件可以有不同的用户),工厂 class EventFactory
(使用 Factory Boy 库)和序列化程序 EventSerializer
。我相信我已经按照文档进行工厂制作和序列化,但收到错误消息:
ValueError: "< Event: Test Event >" needs to have a value for field "id" before this many-to-many relationship can be used.
我知道在链接它们之前必须在 ManyToMany 中创建两个模型实例,但我什至没有看到添加发生在哪里!
问题
有人可以阐明如何以我尚未采用的方式使用模型、工厂男孩和序列化程序正确使用 ManyToManyField 吗?
设置
这是我的代码:
models.py
@python_2_unicode_compatible
class Event(CommonInfoModel):
users = models.ManyToManyField(User, blank=True, related_name='events')
# other basic fields...
factories.py
class EventFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.Event
@factory.post_generation
def users(self, create, extracted, **kwargs):
if not create:
# Simple build, do nothing.
return
if extracted:
# A list of users were passed in, use them
# NOTE: This does not seem to be the problem. Setting a breakpoint
# here, this part never even fires
for users in extracted:
self.users.add(users)
serializers.py
class EventSerializer(BaseModelSerializer):
serialization_title = "Event"
# UserSerializer is a very basic serializer, with no nested
# serializers
users = UserSerializer(required=False, many=True)
class Meta:
model = Event
exclude = ('id',)
test.py
class EventTest(APITestCase):
@classmethod
def setUpTestData(cls):
cls.user = User.objects.create_user(email='test@gmail.com',
password='password')
def test_post_create_event(self):
factory = factories.EventFactory.build()
serializer = serializers.EventSerializer(factory)
# IMPORTANT: Calling 'serializer.data' is the exact place the error occurs!
# This error does not occur when I remove the ManyToManyField
res = self.post_api_call('/event/', serializer.data)
版本信息
- Django 1.11
- Python 2.7.10
感谢您提供的任何帮助!
关于错误:
似乎缺少 id
是由于使用 .build()
而不是 .create()
(或只是 EventFactory()
)。前者不保存模型,因此不会得到id
值,而后者会(参考factory docs and model docs)。
我怀疑序列化程序仍然期望对象具有 id
,即使多对多关系是可选的,因为它无法在没有 id
的情况下强制执行潜在关系。
但是,实际任务可能有更简单的解决方案。上面的方法是生成传递给post_api_call()
的POST数据的一种方式。如果此数据是手动创建的,那么工厂和序列化程序都变得不必要了。从测试的角度来看,显式数据方法甚至可能更好,因为您现在可以看到必须产生预期结果的确切数据。而对于工厂和序列化程序方法,它更隐含在测试中实际使用的内容中。