Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用SQL Alchemy和SQL Alchemy Utils URLType使其仅保存主机名?_Python_Sqlalchemy_Sqlalchemy Utils - Fatal编程技术网

Python 如何使用SQL Alchemy和SQL Alchemy Utils URLType使其仅保存主机名?

Python 如何使用SQL Alchemy和SQL Alchemy Utils URLType使其仅保存主机名?,python,sqlalchemy,sqlalchemy-utils,Python,Sqlalchemy,Sqlalchemy Utils,目前,我正在一个项目中使用SQL Alchemy和SQL Alchemy UTIL,我一直在尝试解决如何清理SQLAlchemy属性的输入,以便数据库中存储的唯一内容是furl对象的主机。目前,我通过在每次集合操作之前调用一个类方法解决了这个问题,如下所示: class Website(Base, Timestamp): __tablename__ = "websites" id = Column(Integer, primary_key=True) # Data

目前,我正在一个项目中使用SQL Alchemy和SQL Alchemy UTIL,我一直在尝试解决如何清理SQLAlchemy属性的输入,以便数据库中存储的唯一内容是furl对象的主机。目前,我通过在每次集合操作之前调用一个类方法解决了这个问题,如下所示:

class Website(Base, Timestamp):
    __tablename__ = "websites"

    id = Column(Integer, primary_key=True)

    # Data
    origin = Column(URLType, nullable=False, unique=True)

    # Functions
    @classmethod
    def prep_url(cls, url):
        return url.origin

x = furl('https://stackoverflow.com/questions/ask')
ws = Website(origin=Website.prep_url(x))
>>> ws.origin
stackoverflow.com
虽然我希望能够像这样使用它:

ws = Website(origin=x)
>>> ws.origin
stackoverflow.com
我想这可能就是我要找的,但我找不到它的文档。

使用


刚刚意识到我在这里给出的示例有点糟糕,因为我可以用自定义构造函数来解决它。这不是我想要的,我希望能够自己设置属性。
class Website(Base, Timestamp):
    __tablename__ = "websites"

    id = Column(Integer, primary_key=True)

    # Data
    origin_ = Column("origin", URLType, nullable=False, unique=True)

    @property
    def origin(self):
        return self.origin_

    @origin.setter
    def origin(self, url):
        self.origin_ = url.origin

x = furl('https://stackoverflow.com/questions/ask')
ws = Website(origin=x)
>>> ws.origin
stackoverflow.com