我有以下pynamodb模型:
from dynamo_to_AT.product import save as save_product_to_AT
from dynamo_to_AT.product import delete as delete_product_to_AT
class MasterList(Model):
class Meta:
region = 'us-east-1'
table_name = 'MasterList'
#host = "http://localhost:8000"
pk = UnicodeAttribute(hash_key=True)
asin = UnicodeAttribute(null=True)
country = UnicodeAttribute(null=True)
account = UnicodeAttribute(null=True)
seller_id = UnicodeAttribute(null=True)
market_id = UnicodeAttribute(null=True)
sku = UnicodeAttribute(null=True)
cogl = NumberAttribute(null=True)
cogs = NumberAttribute(null=True)
marketplace = UnicodeAttribute(null=True)
selling_price = NumberAttribute(null=True)
status = UnicodeAttribute(null=True)
launch_date = UnicodeAttribute(null=True)
rating = NumberAttribute(null=True)
review_count = NumberAttribute(null=True)
expected_fulfillment_fee_per_unit = NumberAttribute(null=True)
picture_count = NumberAttribute(null=True)
item_volume = NumberAttribute(null=True)
volume_rate = NumberAttribute(null=True)
item_package_weight = NumberAttribute(null=True)
unit_of_weight = UnicodeAttribute(null=True)
manager = UnicodeAttribute(null=True)
sub_category = UnicodeAttribute(null=True)
liquidating = UnicodeAttribute(null=True)
@staticmethod
def column_list():
return [
"asin", "country", "account", "seller_id", "market_id", "sku",
"cogl", "cogs", "marketplace", "selling_price", "status", "launch_date", "rating",
"review_count", "expected_fulfillment_fee_per_unit", "picture_count", "item_volume",
"volume_rate", "item_package_weight", "unit_of_weight", "manager", "sub_category", "liquidating",
]
def update(self, attributes=None, actions=None, condition=None, conditional_operator=None, **expected_values):
"""
Updates an item using the UpdateItem operation.
:param attributes: A dictionary of attributes to update in the following format
{
attr_name: {'value': 10, 'action': 'ADD'},
next_attr: {'value': True, 'action': 'PUT'},
}
"""
#print(attributes)
out={
'status':'success',
'dynamodb':'success',
'airtable':'success',
'msg':''
}
try:
super(MasterList, self).update(attributes, actions, condition, conditional_operator, **expected_values)
except Exception as ex:
out['status']='error'
out['dynamodb']='error'
out['msg']="Dynamodb: "+str(ex)+" \n "
try:
save_product_to_AT(self)
except Exception as ex:
out['status']='error'
out['airtable']='error'
out['msg']+= "Airtable: "+str(ex)+"\n"
return out
def delete(self, condition=None, conditional_operator=None, **expected_values):
"""
Deletes this object from dynamodb
"""
#print(attributes)
out={
'status':'success',
'dynamodb':'success' ,
'airtable':'success',
'msg':''
}
try:
super(MasterList, self).delete(condition, conditional_operator, **expected_values)
except Exception as ex:
out['status']='error'
out['dynamodb']='error'
out['msg']="Dynamodb: "+str(ex)+" \n "
try:
delete_product_to_AT(self)
except Exception as ex:
out['status']='error'
out['airtable']='error'
out['msg']+= "Airtable: "+str(ex)+"\n"
return out
更新方法有效,但是删除方法不允许我传递“自我” 在函数delete_prodcut_to_AT()上 当我在函数上打印它时,它=“ None”
我在更新时使用了非常相似的功能:save_product_to_AT(self)
我可以引用“自我”
dynamo_to_AT / product.py:
def save(ddb_product):
if ddb_product.country in US:
#update us tables
_save(ddb_product, AT_SETTINGS['embedded_weekly_usa'])
# add more bases as needed
pass
elif ddb_product.country in CA:
_save(ddb_product, AT_SETTINGS['embedded_weekly_can'])
#update ca tables
pass
def _save(ddb_product, settings):
product_AT=Product_AT(settings['base_key'], table_name=settings['product_tbl'])
product_AT.account=ddb_product.account
product_AT.sku=ddb_product.sku
product_AT.asin=ddb_product.asin
def delete(ddb_produc):
send_error_message(ddb_produc)
send_error_message('delete function object')
if ddb_product.country in US:
send_error_message('delete function us')
#update us tables
_delete(ddb_product, AT_SETTINGS['embedded_weekly_usa'])
# add more bases as needed
pass
elif ddb_product.country in CA:
send_error_message('delete function ca')
_delete(ddb_product, AT_SETTINGS['embedded_weekly_can'])
#update ca tables
pass
def _delete(ddb_product, settings):
try:
product_AT=Product_AT(settings['base_key'], table_name=settings['product_tbl'])
product_AT.account=ddb_product.account
product_AT.sku=ddb_product.sku
send_error_message( product_AT.delete_by_acct_sku())
如何将模型中的属性传递给'delete_prodcut_to_AT(self)',以便self具有模型的属性,就像save(ddb_product)在ddb_product中具有它们一样,来自:save_product_to_AT(self)