将空消息对象设置为字段
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
在 Google Ads API 中,某些消息字段被定义为空消息对象,例如 campaign.manual_cpm
,或者它们可能只有不需要设置的可选字段,例如 campaign.manual_cpc
。设置这些字段对于告知 API 要为指定广告系列使用哪种出价策略非常重要,但当消息为空时,这并不直观。
更新字符串类型的 campaign.name
字段时,我们直接更新该字段,就像更新普通的 Python 对象属性一样:
campaign.name = "Test campaign value"
campaign.manual_cpc
是一个嵌套字段,这意味着它包含另一个 protobuf 消息,而不是字符串等原始类型。您也可以直接更新其字段:
campaign.manual_cpc.enhanced_cpc_enabled = True
这将告知 API,相应广告系列的出价策略为 manual_cpc
,且已启用智能点击付费。
但如果您想使用空字符串 manual_cpm
,该怎么办?或者manual_cpc
不启用智能点击付费?为此,您需要将该类的单独空实例复制到广告系列中,例如:
client = GoogleAdsClient.load_from_storage()
empty_cpm = client.get_type('ManualCpm')
client.copy_from(campaign.manual_cpm, empty_cpm)
请注意如何为 campaign
对象指定 manual_cpm
:
name {
value: "Test campaign value"
}
manual_cpm {
}
manual_cpm
字段已设置,但其所有字段均没有值。向使用此模式的 API 发送请求时,您可以通过启用日志记录并检查请求载荷来验证是否正确设置了空消息对象。
最后,您需要手动将此字段添加到请求对象的 update_mask
中。字段掩码帮助程序没有机制来确定已明确设置为空对象的字段与未设置的字段之间的区别。
from google.api_core.protobuf_helpers import field_mask
campaign_operation.create = campaign
campaign_operation.update_mask = field_mask(None, campaign)
# Here we manually add the "manual_cpm" field
campaign_operation.update_mask.append("manual_cpm")
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-27。
[null,null,["最后更新时间 (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\")"]]