views:

459

answers:

2

This is using Google App Engine. I am not sure if this is applicable to just normal Django development or if Google App Engine will play a part. If it does, would you let me know so I can update the description of this problem.

class MessageModel(db.Model):
    to_user_id = db.IntegerProperty()
    to_user = db.StringProperty(multiline=False)
    message = db.StringProperty(multiline=False)
    date_created = db.DateTimeProperty(auto_now_add=True)

Now when I do a query a get a list of "MessageModel" and send it to the template.html to bind against, I would like to include a few more properties such as the "since_date_created" to output how long ago since the last output, potentially play around with the message property and add other parameters that will help with the layout such as "highlight" , "background-color" etc...

The only way I thought of is to loop through the initial Query Object and create a new list where I would add the property values and then append it back to a list.

    for msg in messagesSQL:
 msg.lalaland = "test"
            msg.since_created_time = 321932
            msglist.append(msg)

Then instead of passing the template.html messagesSQL, I will now pass it msglist.

+5  A: 

You should still be able to send it messagesSQL to the template after you've added elements to it via the for loop. Python allows that sort of thing.

Something else that might make sense in some cases would be to give your MessageModel methods. For instance, if you have a

def since_date_created(self):
    '''Compute the time since creation time based on self.date_created.'''

Then (assuming you have "messagesSQL" in the template), you can use the function as

{% for msg in messagesSQL %}
    {{ msg.since_date_created }}
{% endfor %}

Basically, you can call any method in the model as long as you it needs no arguments passed to it.

uzi
+3  A: 

You can obtain that by defining methods in the model like

class MessageModel(db.Model):
    # Definition
    def since_date_created(self):
        # ...

Now in the template, you can use it like

Time since created {{ message.since_date_created }}
abhinavg
Thanks a lot for the answer, I marked the first answer as the correct answer since it was first, but I like to give my appreciation to you as well.
TimLeung