I would like to change my postal code to '' if its None but cannot access the country_code parameter properly to do it. What am I doing wrong?
class AddressSchema(Schema):
def _postal_check(self, postal):
allowed_countries = ["GE","BT","HK","MO"]
postal_code = postal.postalCode
country_code = postal.countryCode
if postal_code is None and country_code in allowed_countries:
postal_code = ''
return postal_code
countryCode = fields.Str(validate=Length(equal=2), required=True)
postalCode = fields.Method(serialize='_postal_check', allow_none=True, required=True)
There are several problems in your code: the variable you pass as a parameter to your function is not the one used, then the variable
allowed_countriesis not correctly declared.In addition, you use two different variables: the declared variable
countryCodeuses theCamelCasestyle while the variable you call uses thelower_case_with_underscores style:country_code. You must harmonize the names of your variables: