با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
در Google Ads API برخی از فیلدهای پیام به عنوان اشیاء پیام خالی تعریف می شوند، مانند campaign.manual_cpm ، یا ممکن است فقط دارای فیلدهای اختیاری باشند که نیازی به تنظیم ندارند، به عنوان مثال campaign.manual_cpc . تنظیم این فیلدها برای اینکه به API بگویید از کدام استراتژی پیشنهاد قیمت برای کمپین معین استفاده کند، مهم است، اما زمانی که پیامها خالی هستند، شهودی نیست.
هنگام به روز رسانی فیلد campaign.name که یک رشته است، فیلد را با به روز رسانی مستقیم آن طوری تنظیم می کنیم که گویی یک ویژگی شی معمولی پایتون است:
campaign.name="Test campaign value"
campaign.manual_cpc یک فیلد تودرتو است، به این معنی که حاوی پیام پروتوباف دیگری است و نه یک نوع اولیه، مانند یک رشته. همچنین می توانید فیلدهای آن را مستقیماً به روز کنید:
campaign.manual_cpc.enhanced_cpc_enabled=True
این به API میگوید که این کمپین دارای یک استراتژی پیشنهاد قیمت manual_cpc با فعال کردن CPC بهبودیافته است.
اما اگر بخواهید از manual_cpm استفاده کنید که خالی است چه؟ یا manual_cpc بدون فعال کردن cpc پیشرفته؟ برای انجام این کار باید یک نمونه خالی جداگانه از کلاس را در کمپین کپی کنید، به عنوان مثال:
توجه داشته باشید که manual_cpm چگونه برای شی campaign مشخص شده است:
name{value:"Test campaign value"}
manual_cpm{}
فیلد manual_cpm تنظیم شده است، اما هیچ کدام از فیلدهای آن مقداری ندارند. هنگام ارسال درخواست به APIهایی که از این الگو استفاده میکنند، میتوانید تأیید کنید که شیء پیام خالی را به درستی تنظیم کردهاید، با فعال کردن ورود به سیستم و بررسی بار درخواست.
در نهایت، باید این فیلد را به صورت دستی به update_mask شی درخواست اضافه کنید. کمک کننده فیلد ماسک مکانیزمی برای تعیین تفاوت بین فیلدی که به صراحت روی یک شی خالی تنظیم شده است و فیلدی که تنظیم نشده است ندارد.
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")
تاریخ آخرین بهروزرسانی 2025-09-03 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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"]],["تاریخ آخرین بهروزرسانی 2025-09-03 بهوقت ساعت هماهنگ جهانی."],[[["\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\")"]]