0

Hi I want to create new field in sqlalchemy similar to FileField from Django

from sqlalchemy import String

class FileField(String):
"""A mixin that adds field for storing files"""

def __init__(self, length=None, filename='test'):
    super().__init__(length=length)
    self.filename = filename

def some_method(self):
    pass

Then in different file, I have imported FileField, and looks like this:

class NewModel():
"""A user of the app."""
__tablename__ = 'new_model'

logo = Column(FileField(30), unique=True, nullable=False)

def __init__(self, name: str, symbol: str, logo: FileField, **kwargs):
    db.Model.__init__(
        self,
        logo=logo,
        **kwargs,
    )

migration looks like:

def upgrade():
    op.create_table('new_table',
    sa.Column('logo', coin_portal.database.FileField(length=30), nullable=False),

But ! :( when using:

  • FileField is string and has no .filename property
  • Methods are not visible

So my question is how to create a new field which is not just string?

davidism
  • 121,510
  • 29
  • 395
  • 339
kpazik
  • 175
  • 2
  • 11
  • why are you trying to add a filename attribute to your custom type? from a database perspective this makes no sense. it sounds like maybe filename should be a separate column with the String type? – apteryx Feb 07 '18 at 17:28
  • Need to store URL of an image - it would be **VARCHAR** anyway, But just want to have dedicated field type like **FileField** from Django. Besides on this custom field I will have other methods as well – kpazik Feb 07 '18 at 17:39

0 Answers0