django,如何从get_or_create获取对象并将其用于其他get_or_create?

时间:2018-11-06 06:26:53

标签: django

代码示例:

keyword = Keyword.objects.get_or_create(text=text)

sub_keyword = SubKeyword.objects.get_or_create(keyword=keyword, user=request.user)

它返回:TypeError: int() argument must be a string, a bytes-like object or a number, not 'Keyword'

来自行:sub_keyword = SubKeyword.objects.get_or_create(keyword=keyword, user=request.user)

该怎么做?

1 个答案:

答案 0 :(得分:3)

keyword, created = Keyword.objects.get_or_create(text=text)

sub_keyword, created = SubKeyword.objects.get_or_create(keyword=keyword.id, user=request.user)

get_or_create返回类似(object, created)的元组。这就是为什么您有AttributeError的原因。请参阅get_or_create文档