在将一些项目添加到“国家” ManytomanyField后,我在返回对象时遇到问题。我可以看到我的数据已保存
class CompanyProfileManager(models.Manager):
@transaction.atomic
def register_company(self, name, description, tag_name, email, is_private, uses_credits, pays_subscription, pool,
facebook, twitter, web, country, videocall_doctor_to_patient, micro_type, can_move, fixed_percentage, fixed_price, fixed_tariff, instagram, subdomain, banner_description, banner_title, countries, **kwargs):
tag = Tag(name=tag_name, description=tag_name, is_active=True)
tag.save()
company = self.create_instance(name, description, email, is_private, uses_credits, pays_subscription, pool,
facebook, twitter, web, country, videocall_doctor_to_patient, micro_type, can_move, fixed_percentage, fixed_price, fixed_tariff, instagram, subdomain, banner_description, banner_title)
company.tag = tag
company.save()
for item in countries:
company.countries.add(item)
return company #Using Debug Mode, My project breaks right here
我真的迷路了,到目前为止,与我的问题有关的唯一事情是使用.all()作为查询集获取
答案 0 :(得分:0)
ManyToMany属性(在您的情况下为countries
)是Manager
,更确切地说是ManyRelatedManager
,而不是QuerySet
。与objects
中的QuerySet.objects
有点类似,因此如果要遍历所有国家/地区,则需要使用.all()
:
for item in countries.all():
company.countries.add(item)
您还可以使用其他.filters()
,.select_related()
等奇特方法...
编辑
由于变量countries
不是查询集而是列表,因此我怀疑错误不是由这段代码生成的,可能是在尝试遍历company.countries
而不是{ {1}}