Hi,

Thanks for the reply. This definitely looks good but my problem is i
want to save the data into the database as a column and I need to show
this message to user. I'm using formalchemy to show the data and this
way, formalchemy grid view is throwing Attribute error on expiry_code.
May be I'm doing something wrong, I'm very new to sqlalchemy.

On Mar 1, 12:46 pm, GHZ <geraint.willi...@gmail.com> wrote:
> Hi,
>
> Do you need to store expiry_code? seeing as it is a function of
> last_con and the current date.
>
> class PhNumber(Base):
>   __tablename__ = 'ph_numbers'
>   ph_no = Column(Integer, nullable=False)
>   last_con = Column(DateTime, nullable=False)
>
>   @property
>   def expiry_code(self):
>     msg = 'Expired 3 months'
>     now = datetime.datetime.now()
>     if now > (self.last_con  - 90):
>         return msg
>     return 'Not Applicable'
>
> If the column needs to be queried from outside sqlalchemy, then you
> could put the logic in a database function (depending upon what
> database you are using).
>
> On Mar 1, 12:52 pm, dalia <dalia....@gmail.com> wrote:
>
> > Hi,
>
> > I have a table of phone numbers which has 3 columns named -
>
> > 1. ph_no  Integer not null
> > 2. last_contacted Datetime not null
> > 3. expiry_code Text()
>
> > The behaviour of the table should be - When the last_contacted column
> > has a date which is 3 months older, the expiry_code column should have
> > the value 'number expired'. I'm not sure how this can be done using
> > declarative method. I did the following -
>
> > class PhNumber(Base):
> >     __tablename__ = 'ph_numbers'
> >     ph_no = Column(Integer, nullable=False)
> >     last_con = Column(DateTime, nullable=False)
> >     expiry_code = Column(Text(), default=mydefault,
> > onupdate=mydefault)
>
> > def mydefault(context):
> >     msg = 'Expired 3 months'
> >     now = datetime.datetime.now()
> >     if now > (context.current_parameters['last_con']  - 90):
> >         return msg
> >     return 'Not Applicable'
>
> > mydefault function calculates if the value in last_con column is
> > greater than 3 months of today's date, it stores 'Expired 3 months' in
> > expiry_code. But this happens only when a new insert or update occurs
> > in this table.
>
> > I want the value in expiry_code to be changed even without any update/
> > insert operations on the table. Whenever the table is selected, the
> > updated value should be shown. Is this possible in SQLAlchemy? Please
> > let me know.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to