Creating custom field for concatenation of string

26 views
Skip to first unread message

jeff k

unread,
Jul 2, 2021, 10:39:18 AM7/2/21
to peewee-orm
Hi All,

I have being trying to have a custom field (I don't know if this is the correct terminology,  but I want to concatenation strings in a function)



class savedOptionSymbols(peewee.Model):
    equity_symbol = peewee.ForeignKeyField(savedEquitySymbols)
    strike_price = peewee.FloatField(default=0)
    type = peewee.CharField(max_length=1, default='C') # C-call   P-Put
    expiration = peewee.DateField()
    active = peewee.BooleanField(default=True)

    def option_symbol(self): # AMZN_07-16-21_C_3500
        return str(self.type  + '_' + self.strike_price)

    class Meta:
        database = db
        db_table = 'savedOptionSymbols'
savedOptionSymbols.create_table()




### this is a separate file that i want to do all of my saving, and searching on

from DB_creation import db,savedEquitySymbols,savedOptionSymbols,savedHistoricalEquityPrices,savedHistoricalOptionPrices
from datetime import datetime

#db.connect()

# AMZN_07-16-21_C_3500  #<-- in the end I want something like this

ff = savedOptionSymbols.get_or_create(
    equity_symbol = 'AMZN',
    strike_price = 3500,
    type = 'C', # C-call   P-Put
    expiration = datetime(2020, 5, 17),
    )

for option in savedOptionSymbols.select().where(savedOptionSymbols.active == 1):
    print(option.expiration)
    print(option.strike_price)
    print(option.option_symbol) #<-- this is from the custom field

db.close()

And that file outputs 
2020-05-17
3500.0
<bound method savedOptionSymbols.option_symbol of <savedOptionSymbols: 1>>


I don't want a bound method I want the actual string. How do I get that?

-Jeff


Charles Leifer

unread,
Jul 2, 2021, 10:48:36 AM7/2/21
to peewe...@googlegroups.com
Honestly, I would probably do something like that with a TRIGGER, which you'll need to write yourself and configure for your table. Here's the sqlite docs for the syntax: https://sqlite.org/lang_createtrigger.html

--
You received this message because you are subscribed to the Google Groups "peewee-orm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to peewee-orm+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/peewee-orm/859f1f27-0dcb-4b91-b5e2-ed79dfe2e422n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages