Can a method call inside save() display its return on template?

24 views
Skip to first unread message

mapapage

unread,
Jul 19, 2012, 3:31:23 AM7/19/12
to django...@googlegroups.com
In my models.py I override the save method like this:

def save(self, *args, **kwargs):
        self.directiondb = get_direction_db(self.nod_id1.id, self.nod_id2.id)
        get_direction_descr(self.nod_id1.id, self.nod_id2.id)
        get_vrd_code(self.nod_id1.id, self.nod_id2.id)
        self.vrd_id = get_vrd_id(self.nod_id1.id, self.nod_id2.id)
        super(Webrequests, self).save(*args, **kwargs)

and it works fine. 
Is it now possible what get_direction_descr() and get_vrd_code() return to be displayed on the template on the fly just after the user selects nod_id1 and nod_id2 from a dropdown list?
Or how I could accomplish sth like this?

Tomas Neme

unread,
Jul 19, 2012, 11:52:38 AM7/19/12
to django...@googlegroups.com
> In my models.py I override the save method like this:
>
> def save(self, *args, **kwargs):
> self.directiondb = get_direction_db(self.nod_id1.id,
> self.nod_id2.id)
> get_direction_descr(self.nod_id1.id, self.nod_id2.id)
> get_vrd_code(self.nod_id1.id, self.nod_id2.id)
> self.vrd_id = get_vrd_id(self.nod_id1.id, self.nod_id2.id)
> super(Webrequests, self).save(*args, **kwargs)
>
> and it works fine.
> Is it now possible what get_direction_descr() and get_vrd_code() return to

well, what does get_vrd_code do? why aren't you saving the value
anywhere? same question with get_direction_descr.

The easy answer will be:
self.vrd_code = get_vrd_code(self.nod_id1.id, self.nod_id2.id)

and then use that on the template, but that save() method seems
awfully redundant, so.. can you explain what your model does better?

--
"The whole of Japan is pure invention. There is no such country, there
are no such people" --Oscar Wilde

|_|0|_|
|_|_|0|
|0|0|0|

(\__/)
(='.'=)This is Bunny. Copy and paste bunny
(")_(") to help him gain world domination.

Marilena Papageorgiou

unread,
Jul 20, 2012, 2:53:20 AM7/20/12
to django...@googlegroups.com
get_direction_descr does something like this:
def get_direction_descr(nod_id1, nod_id2):
          cursor
= connection.cursor()
          cursor
.execute("SELECT GetDirectionDescr("+str(nod_id1)+","+str(nod_id2)+") from sys.dual")
          result
= cursor.fetchall()
         
return result[0][0]
and get_vrd_code something similar. I shouldn't save what is being to returned to the db compared to get_vrd_id and get_direction_db. The returns of the last two need to be calculated according to a user's choices(the args) and saved to fields of my model while not being displayed on the template. I don't know if I could do it somehow different. For get_vrd_code and get_direction_descr that have the same args with the previous mentioned, I just want the returns to be displayed on the template and since the args are passed dynamically I thought that I can maybe use the same way to get the returns since the args are available to that point in save(). But now even if I don't know how I can sense that I should use ajax for that.


Tomas Neme

unread,
Jul 20, 2012, 10:01:27 AM7/20/12
to django...@googlegroups.com
You can still do self.something = foobar even if "something" is not a
database Field.

What I mean is, doing self.vdr_code = get_vdr_code() will save it for
THIS INSTANCE and then you can use it in your template.

If you really need this to be only at .save() (instead of, say, at
__init__, because it feels like once the object's been saved this
could be get in object initialization), you just have to make sure
this is the same instance you pass to the template context.

Something like:

myobject.save()
.....
render_to_response(template, { 'object': myobject })

then in your template you can do {{ object.vdr_code }}

Although all in all, I *really* hope you have a good reason to use db
backend-specific SQL instead of making a django model of those
DirectionDescr whatever they are

---

Andy McKay

unread,
Jul 20, 2012, 12:46:46 PM7/20/12
to django...@googlegroups.com
> cursor.execute("SELECT
> GetDirectionDescr("+str(nod_id1)+","+str(nod_id2)+") from sys.dual")

Please note that code means that you might be open to SQL injection.
You should be using the parameters for SQL, see:

https://docs.djangoproject.com/en/dev/topics/db/sql/#passing-parameters-into-raw
Reply all
Reply to author
Forward
0 new messages