fromgoogle.api_core.protobuf_helpersimportfield_maskcampaign_operation.create=campaigncampaign_operation.update_mask=field_mask(None,campaign)# Here we manually add the "manual_cpm" fieldcampaign_operation.update_mask.append("manual_cpm")
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-27。"],[[["\u003cp\u003eSome Google Ads API message fields are defined as empty or only have optional fields, requiring specific handling to indicate the intended bidding strategy.\u003c/p\u003e\n"],["\u003cp\u003eTo set a bidding strategy using an empty message field (like \u003ccode\u003emanual_cpm\u003c/code\u003e), you need to copy a separate empty instance of the corresponding class onto the campaign object.\u003c/p\u003e\n"],["\u003cp\u003eNested fields within messages (like \u003ccode\u003ecampaign.manual_cpc.enhanced_cpc_enabled\u003c/code\u003e) can be updated directly like normal Python object attributes.\u003c/p\u003e\n"],["\u003cp\u003eWhen using empty message objects, ensure the field is added to the request's \u003ccode\u003eupdate_mask\u003c/code\u003e manually, as the field mask helper cannot automatically detect this.\u003c/p\u003e\n"]]],[],null,["# Setting Empty Message Objects as Fields\n\nIn the Google Ads API some message fields are defined as empty message objects,\nsuch as [`campaign.manual_cpm`](/google-ads/api/fields/v21/campaign#campaign.manual_cpm),\nor they may only have optional fields that don't need to be set, for example\n[`campaign.manual_cpc`](/google-ads/api/fields/v21/campaign#campaign.manual_cpc.enhanced_cpc_enabled).\nSetting these fields is important to tell the API which bidding strategy to use\nfor the given Campaign, but it's not intuitive when the messages are empty.\n\nWhen updating the `campaign.name` field, which is a string, we set the field\nby updating it directly as if it were a normal Python object attribute: \n\n campaign.name = \"Test campaign value\"\n\n`campaign.manual_cpc` is a nested field, meaning it contains\nanother protobuf message and not a primitive type, like a string. You\ncan update its fields directly as well: \n\n campaign.manual_cpc.enhanced_cpc_enabled = True\n\nThis will tell the API that this Campaign has a bidding strategy of `manual_cpc`\nwith enhanced CPC enabled.\n\nBut what if you want to use `manual_cpm`, which is empty? Or `manual_cpc`\nwithout enabling enhanced cpc? To do this you will need to copy a separate\nempty instance of the class onto the campaign, for example: \n\n client = GoogleAdsClient.load_from_storage()\n\n empty_cpm = client.get_type('ManualCpm')\n client.copy_from(campaign.manual_cpm, empty_cpm)\n\nNote how `manual_cpm` is specified for the `campaign` object: \n\n name {\n value: \"Test campaign value\"\n }\n manual_cpm {\n }\n\nThe `manual_cpm` field is set, but none of its fields have values. When sending\nrequest to the API that use this pattern, you can verify that you're setting the\nempty message object correctly by enabling [logging](/google-ads/api/docs/client-libs/python/logging) and inspecting the\nrequest payload.\n\nLastly, you'll need to manually add this field to the request object's\n`update_mask`. The field mask helper has no mechanism to determine the\ndifference between a field that's been explicitly set to an empty object, and a\nfield that hasn't been set. \n\n from google.api_core.protobuf_helpers import field_mask\n\n campaign_operation.create = campaign\n campaign_operation.update_mask = field_mask(None, campaign)\n # Here we manually add the \"manual_cpm\" field\n campaign_operation.update_mask.append(\"manual_cpm\")"]]