我一些麻煩此代碼是工作:Python - 如何連接Python中的字符串和整數?
count_bicycleadcategory = 0
for item_bicycleadcategory in some_list_with_integers:
exec 'model_bicycleadcategory_%s.bicycleadcategorytype = BicycleAdCategoryType.objects.get(pk=' + str(item_bicycleadcategory) + ')' % count_bicycleadcategory
count_bicycleadcategory = count_bicycleadcategory + 1
我得到一個錯誤:
Type Error, not all arguments converted during string formatting
我的問題是:我如何通過「item_bicycleadcategory」以任何線索exec表達式?
最好的問候,
這是錯誤的,錯誤的,錯誤的。你爲什麼認爲你需要動態變量名?你不。你需要一本字典。 –
這僅僅是一種非常不安全和晦澀的寫作方式(或者意味着):'values = {pk:BicycleAdCategoryType.objects.get(pk = pk)for pk in some_list_with_integers}'? –
或者多想一想...... BicycleAdCategoryType.objects.get(pk__in = some_list_with_integers)'? –