自定义Python模型构造函数

时间:2018-04-09 23:48:48

标签: python class object

我构建了一个自定义模型,允许我从MySQL查询中保存到Airtable。目标是从基本键和表名循环传递基本名称和键,并让模型使用在上次调整之前工作的保存功能。我无法弄清楚改变是什么,我已经评论了一切,但是:

sleep 1

我仍然收到错误。

daily_kpi=Daily_KPI(base_key=base['base_key'], table_name=base['table_name'])

当我尝试使用该模型时,

class Daily_KPI(at.AT):
    base_key=None
    table_name='Daily'

    #Fields

    @property
    def ID(self):
        dt=datetime.strptime(self.date, '%Y-%m-%d').strftime('%m/%d/%y')
        return str(dt)+" "+self.sku+" "+self.account

    date=at.ATField(field_name="date", at_field_name="Date")
    asin=at.ATField(field_name="asin", at_field_name="ASIN")
    account=at.ATField(field_name="account", at_field_name="Account")
    # country=at.ATField(field_name="country", at_field_name="Country")
    sessions=at.ATField(field_name="sessions", at_field_name="Sessions")
    session_pct=at.ATField(field_name="session_pct", at_field_name="Session Pct")
    page_views=at.ATField(field_name="page_views", at_field_name="Page Views")
    page_view=at.ATField(field_name="page_view", at_field_name="Page View")
    buy_box=at.ATField(field_name="buy_box", at_field_name="Buy Box")
    units_ordered=at.ATField(field_name="units_ordered", at_field_name="Units Ordered")
    units_ord_b2b=at.ATField(field_name="units_ord_b2b", at_field_name="Units Ord B2B")
    unit_session=at.ATField(field_name="unit_session", at_field_name="Unit Session")
    ord_prod=at.ATField(field_name="ord_prod", at_field_name="Ord Prod")
    ord_prod_b2b=at.ATField(field_name="ord_prod_b2b", at_field_name="Ord Prod B2B")
    tot_ord_items=at.ATField(field_name="tot_ord_items", at_field_name="Tot Ord Items")
    tot_ord_b2b=at.ATField(field_name="tot_ord_b2b", at_field_name="Tot Ord B2B")
    bsr=at.ATField(field_name="bsr", at_field_name="B.S.R")
    actual_sales=at.ATField(field_name="actual_sales", at_field_name="Actual Sales")
    selling_price=at.ATField(field_name="selling_price", at_field_name="Selling Price")
    notes=at.ATField(field_name="notes", at_field_name="Notes")
    unit_sess_b2b=at.ATField(field_name="unit_sess_b2b", at_field_name="Unit Sess B2B")
    sku=at.ATField(field_name="sku", at_field_name="SKU")

    def __init__(self, base_key, table_name):
        super(Daily_KPI, self).__init__(base_key=base_key, table_name=table_name)

我收到错误: def save_data(self): try: print ('hello') #wp_table=Airtable(BASE_KEY, 'Weekly Profitability Development copy', API_KEY) results = self.results print(len(results)) for base in settings.DAILY_AIRTABLE: for row in self.results: try: daily_kpi=Daily_KPI(base_key=base['base_key'], table_name=base['table_name']) daily_kpi.date=str(date.today()) # daily_kpi.sku = row['ItemSKU'] # daily_kpi.asin = row['ASIN'] # daily_kpi.account = row['Account'] # daily_kpi.sessions = row['Sessions'] # daily_kpi.session_pct = row['Session_Pct'] # daily_kpi.page_view = row['Page_Views'] # daily_kpi.page_view_pct = row['Page_Views_Pct'] # daily_kpi.buy_box = row['Buy_Box_Pct'] # daily_kpi.units_ordered = row['Units_Ordered'] # daily_kpi.units_ordered_b2b = row['Units_Ordered_B2B'] # daily_kpi.unit_session_pct=row['Unit_Session_Pct'] # daily_kpi.unit_session_pct_b2b= row['Unit_Session_Pct_B2B'] # daily_kpi.ord_prod=row['Ordered_Product_Sales'] # daily_kpi.ord_prod_b2b=row['Ordere_Product_Sales_B2B'] # daily_kpi.tot_ord_items=row['Total_Order_Items'] # daily_kpi.tot_ord_b2b=row['Total_Order_Items_B2B'] # daily_kpi.bsr=row['BSR'] # daily_kpi.actual_sales # daily_kpi.selling_price # daily_kpi.notes # daily_kpi.unit_sess_b2b # daily_kpi.sku daily_kpi.save() except Exception as e: print(e)

我不太清楚如何调整构造函数以便它接受参数并创建一个对象。我试过了

super() argument 1 must be type, not classobj

1 个答案:

答案 0 :(得分:0)

我制作了一个简单易用的Pytbon客户端。用你描述的东西很容易做到: https://github.com/gtalarico/airtable-python-wrapper