Sunday, August 14, 2011

Re: How to store variable timedeltas in a Django model?

One of the reasons that I migrated to django is because of the
underlying functionality.. With that said I would use a separate table
or file, whichever you are comfortable with , to store the changing
time delta variable.

You could dynamically retrieve this variable in the view or, the
example that django provides is to create a function in the models.py
and call that function from the model that you wanted to use the time
delta

https://docs.djangoproject.com/en/1.3/topics/db/managers/

The code on this page that I am referring to is:

class PollManager(models.Manager):
def with_counts(self):
from django.db import connection
cursor = connection.cursor()
cursor.execute("""
SELECT p.id, p.question, p.poll_date, COUNT(*)
FROM polls_opinionpoll p, polls_response r
WHERE p.id = r.poll_id
GROUP BY 1, 2, 3
ORDER BY 3 DESC""")
result_list = []
for row in cursor.fetchall():
p = self.model(id=row[0], question=row[1],
poll_date=row[2])
p.num_responses = row[3]
result_list.append(p)
return result_list

class OpinionPoll(models.Model):
question = models.CharField(max_length=200)
poll_date = models.DateField()
objects = PollManager()
******************************************

The function that you want would be repalced with the much simpler sql

select timedelta from time_delta ##table


On Aug 14, 1:10 am, Karen Rustad <karen.rus...@gmail.com> wrote:
> Hello all,
>
> In one of my models, I want to store a timedelta--a length of time not
> tied to a particular start and end date. Although timedeltas are built
> into Python, tragically there's no TimeDeltaField in Django.
>
> Extra complications:
>
> * The timedelta can be quite large -- on the scale of one week to
> several months.
> * The timedelta has a default value, but it can be changed by the
> administrator.
> * I want the default timedelta length to be one month (which, of
> course, has a variable number of days!)
>
> It seems like *someone* has to have been able to do this, if only as a
> custom model field. But I wasn't able to find anything in the archives
> on this list and the #django IRC logger down so I couldn't search
> that. :/
>
> I did read the docs on how to write a custom model, but it looks
> terribly confusing--it's not clear which or how many of the attributes
> mentioned in the docs I would need to customize, given that it *is* a
> built-in Python variable type, and I have exactly zero experience
> dealing with databases directly.
>
> If anyone's got a hack around this limitation to recommend, or even
> some more relevant sample code than what's in the custom model docs,
> that would be super helpful!
>
> -- Karen

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

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate