所以这可能很简单,我错过了一些东西,因为我对 python 完全陌生,我在 swagger 3.0 中为 python 烧瓶创建了一个服务器存根,这基于我的 swagger 文档创建了一堆模型类,它还使用连接示例:
# coding: utf-8
from __future__ import absolute_import
from datetime import date, datetime # noqa: F401
from typing import List, Dict # noqa: F401
from src.models.base_model_ import Model
from src.models.accounting_bills import AccountingBills # noqa: F401,E501
from src.models.accounting_payments_made import AccountingPaymentsMade # noqa: F401,E501
from src.models.accounting_payments_pending import AccountingPaymentsPending # noqa: F401,E501
from src.models.accounts_invoice import AccountsInvoice # noqa: F401,E501
from src.models.accounts_payable import AccountsPayable # noqa: F401,E501
from src.models.accounts_receivable import AccountsReceivable # noqa: F401,E501
from src import util
class Accounting(Model):
"""NOTE: This class is auto generated by the swagger code generator program.
Do not edit the class manually.
"""
def __init__(self, accounts_receivable: AccountsReceivable=None, accounts_payable: AccountsPayable=None, invoices: AccountsInvoice=None, payments_pending: AccountingPaymentsPending=None, payments_made: AccountingPaymentsMade=None, bills: AccountingBills=None): # noqa: E501
"""Accounting - a model defined in Swagger
:param accounts_receivable: The accounts_receivable of this Accounting. # noqa: E501
:type accounts_receivable: AccountsReceivable
:param accounts_payable: The accounts_payable of this Accounting. # noqa: E501
:type accounts_payable: AccountsPayable
:param invoices: The invoices of this Accounting. # noqa: E501
:type invoices: AccountsInvoice
:param payments_pending: The payments_pending of this Accounting. # noqa: E501
:type payments_pending: AccountingPaymentsPending
:param payments_made: The payments_made of this Accounting. # noqa: E501
:type payments_made: AccountingPaymentsMade
:param bills: The bills of this Accounting. # noqa: E501
:type bills: AccountingBills
"""
self.swagger_types = {
'accounts_receivable': AccountsReceivable,
'accounts_payable': AccountsPayable,
'invoices': AccountsInvoice,
'payments_pending': AccountingPaymentsPending,
'payments_made': AccountingPaymentsMade,
'bills': AccountingBills
}
self.attribute_map = {
'accounts_receivable': 'Accounts receivable',
'accounts_payable': 'Accounts payable',
'invoices': 'Invoices',
'payments_pending': 'Payments Pending',
'payments_made': 'Payments Made',
'bills': 'Bills'
}
self._accounts_receivable = accounts_receivable
self._accounts_payable = accounts_payable
self._invoices = invoices
self._payments_pending = payments_pending
self._payments_made = payments_made
self._bills = bills
@classmethod
def from_dict(cls, dikt) -> 'Accounting':
"""Returns the dict as a model
:param dikt: A dict.
:type: dict
:return: The Accounting of this Accounting. # noqa: E501
:rtype: Accounting
"""
return util.deserialize_model(dikt, cls)
@property
def accounts_receivable(self) -> AccountsReceivable:
"""Gets the accounts_receivable of this Accounting.
:return: The accounts_receivable of this Accounting.
:rtype: AccountsReceivable
"""
return self._accounts_receivable
@accounts_receivable.setter
def accounts_receivable(self, accounts_receivable: AccountsReceivable):
"""Sets the accounts_receivable of this Accounting.
:param accounts_receivable: The accounts_receivable of this Accounting.
:type accounts_receivable: AccountsReceivable
"""
self._accounts_receivable = accounts_receivable
@property
def accounts_payable(self) -> AccountsPayable:
"""Gets the accounts_payable of this Accounting.
:return: The accounts_payable of this Accounting.
:rtype: AccountsPayable
"""
return self._accounts_payable
@accounts_payable.setter
def accounts_payable(self, accounts_payable: AccountsPayable):
"""Sets the accounts_payable of this Accounting.
:param accounts_payable: The accounts_payable of this Accounting.
:type accounts_payable: AccountsPayable
"""
self._accounts_payable = accounts_payable
@property
def invoices(self) -> AccountsInvoice:
"""Gets the invoices of this Accounting.
:return: The invoices of this Accounting.
:rtype: AccountsInvoice
"""
return self._invoices
@invoices.setter
def invoices(self, invoices: AccountsInvoice):
"""Sets the invoices of this Accounting.
:param invoices: The invoices of this Accounting.
:type invoices: AccountsInvoice
"""
self._invoices = invoices
@property
def payments_pending(self) -> AccountingPaymentsPending:
"""Gets the payments_pending of this Accounting.
:return: The payments_pending of this Accounting.
:rtype: AccountingPaymentsPending
"""
return self._payments_pending
@payments_pending.setter
def payments_pending(self, payments_pending: AccountingPaymentsPending):
"""Sets the payments_pending of this Accounting.
:param payments_pending: The payments_pending of this Accounting.
:type payments_pending: AccountingPaymentsPending
"""
self._payments_pending = payments_pending
@property
def payments_made(self) -> AccountingPaymentsMade:
"""Gets the payments_made of this Accounting.
:return: The payments_made of this Accounting.
:rtype: AccountingPaymentsMade
"""
return self._payments_made
@payments_made.setter
def payments_made(self, payments_made: AccountingPaymentsMade):
"""Sets the payments_made of this Accounting.
:param payments_made: The payments_made of this Accounting.
:type payments_made: AccountingPaymentsMade
"""
self._payments_made = payments_made
@property
def bills(self) -> AccountingBills:
"""Gets the bills of this Accounting.
:return: The bills of this Accounting.
:rtype: AccountingBills
"""
return self._bills
@bills.setter
def bills(self, bills: AccountingBills):
"""Sets the bills of this Accounting.
:param bills: The bills of this Accounting.
:type bills: AccountingBills
"""
self._bills = bills
我可以在另一个类中使用这个模型,如下所示:
def accounts_receivable(user_id): # noqa: E501
"""Get accounts receivable for the given user by id
# noqa: E501
:param user_id: The id of Alitus Users
:type user_id: int
:rtype: the accounts receivable information
"""
accounts = []
for i in range(10):
accounts.append(AccountsReceivable(1+i, user_id, 10+i, "harry",
"simple terms"+str(i), get_timestamp(), 2345+i, 45667+i, get_timestamp(), 456+i, 23+i, 4+i))
return accounts
我使用了一个预先存在的数据库并创建了一个 models.py 类来尝试使用 sqlPython(对此也是新的):
from sqlalchemy import BigInteger, Column, Date, DateTime, Integer, JSON, Table, Text
from sqlalchemy.dialects.mysql import ENUM, TINYINT
from src.core.database import Base
metadata = Base.metadata
class Accounting(Base):
__tablename__ = 'accounting'
__table_args__ = {'comment': 'Alitus users Accounting information'}
Id = Column(Integer, primary_key=True)
Accounts_receivable = Column('Accounts receivable', Text(collation='utf8mb4_unicode_ci'))
Accounts_payable = Column('Accounts payable', Text(collation='utf8mb4_unicode_ci'))
Invoices = Column(Text(collation='utf8mb4_unicode_ci'))
Payments_Pending = Column('Payments Pending', Text(collation='utf8mb4_unicode_ci'))
Payments_Made = Column('Payments Made', Text(collation='utf8mb4_unicode_ci'))
Bills = Column(Text(collation='utf8mb4_unicode_ci'))
def __repr__(self):
return "<User(Accounts_receivable='%s', Accounts_payable='%s', Invoices='%s')>" % (
self.Accounts_receivable, self.Accounts_payable, self.Invoices)
def __init__(self, accounts_receivable: AccountsReceivable=None, accounts_payable: AccountsPayable=None, invoices: AccountsInvoice=None, payments_pending: AccountingPaymentsPending=None, payments_made: AccountingPaymentsMade=None, bills: AccountingBills=None): # noqa: E501
self.Accounts_receivable = accounts_receivable
self.Accounts_payable = accounts_payable
.....
有没有一种方法可以在 def init中使用大摇大摆生成的模型和炼金术模型,这样我就不用两次编写代码了?如果是这样,我会怎么做?
这是要尝试做的正确事情还是我以错误的方式去做这一切?
任何指示或帮助将不胜感激。