微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我可以使用swagger创建的服务器存根模型作为sqlAlchemy的模型吗?

如何解决我可以使用swagger创建的服务器存根模型作为sqlAlchemy的模型吗?

所以这可能很简单,由于我是python的新手,我想念一些东西,我在swagger 3.0中为python flask创建了一个服务器存根,并根据我的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,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,bills: AccountingBills=None):  # noqa: E501
    self.Accounts_receivable = accounts_receivable
    self.Accounts_payable = accounts_payable
    .....

有没有办法在def init 中使用带有昂扬气势的模型和Alchemy模型,所以我不会写两次代码了? 如果是这样,我该怎么办?
这是要尝试做的正确事情,还是我会以错误的方式处理所有这一切?
任何指针或帮助,将不胜感激。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?