forward references

3 views
Skip to first unread message

Rock

unread,
Feb 18, 2006, 7:41:08 AM2/18/06
to Django users
Well this is more of a python question I guess, but here goes.
Let's say that I am using the example model from
<a
href="http://www.djangoproject.com/documentation/models/many_to_one/">this
page in the documentation</a>.

I decide to add a _pre_delete function to class Reporter that will
delete any articles for that Reporter before the Reporter is deleted.
Something like this...

def _pre_delete( self ):
articles = articles.get_list(reporter_id__exact=self.id)
for article in articles:
article.delete()

The problem is that this won't compile since articles is undefined.
What is the preferred way to deal with this type of forward reference
where the functions in a given class need to call functions defined for
a class that is itself defined later on in the same file?

Amit Upadhyay

unread,
Feb 18, 2006, 7:43:43 AM2/18/06
to django...@googlegroups.com
On 2/18/06, Rock <ro...@rockhoward.com> wrote:

    def _pre_delete( self ):
        articles = articles.get_list(reporter_id__exact=self.id)
        for article in articles:
            article.delete()

The problem is that this won't compile since articles is undefined.
What is the preferred way to deal with this type of forward reference
where the functions in a given class need to call functions defined for
a class that is itself defined later on in the same file?

Postition in a file is irrevalant, your just have to make sure that Article class has been defined before your _pre_delete is called at runtime. In this case it will be, just import articles, and you should be fine.


--
Amit Upadhyay
Blog: http://www.rootshell.be/~upadhyay
+91-9867-359-701

Rock

unread,
Feb 18, 2006, 7:50:04 AM2/18/06
to Django users
You would think so, but somehow that didn't work...

Precisely what import statement would you use.

Amit Upadhyay

unread,
Feb 18, 2006, 8:03:56 AM2/18/06
to django...@googlegroups.com
from django.models.myapp import articles

Luke Plant

unread,
Feb 18, 2006, 8:12:27 AM2/18/06
to django...@googlegroups.com
On Saturday 18 February 2006 13:03, Amit Upadhyay wrote:
> from django.models.myapp import articles

You will have to put thus inside the _pre_delete function, not at the
top of the module.

Luke

--
"My capacity for happiness you could fit into a matchbox without taking
out the matches first." (Marvin the paranoid android)

Luke Plant || L.Plant.98 (at) cantab.net || http://lukeplant.me.uk/

Rock

unread,
Feb 18, 2006, 8:22:14 AM2/18/06
to Django users
Ahhh. That makes sense. Thanks!

fre...@pythonware.com

unread,
Feb 18, 2006, 11:18:38 AM2/18/06
to Django users
Rock wrote:

> Ahhh. That makes sense. Thanks!

footnote: also note that all uses of "articles" in the function refers
to the
same object, so your first line

articles = articles.get_list(reporter_id__exact=self.id)

replaces the module with a sequence. it doesn't matter in this
simple case, but may cause problems in larger examples. I'd
recommend something like:

article_list = articles.get_list(reporter_id__exact=self.id)

</F>

Rock

unread,
Feb 19, 2006, 12:46:12 AM2/19/06
to Django users
Good catch, but this was just an example to show the poblem I was
facing.

FWIW, if someone really wanted to implement _pre_delete for Reporter as
discussed, they should add this version of the function to class
Reporter:

def _pre_delete( self ):
for article in self.get_article_list():
article.delete()

Reply all
Reply to author
Forward
0 new messages