Hello folks,

I'm somewhat new to python and very new to Django.  I'm trying to do
things "the right way" and I've encountered a problem.

At this point early in my project, all of my work has been to
customize the admin interface.  For one of the databases, there is an
admin option to retrieve a new version of the data from an external
source.

In models.py, I added a custom exception to a new class ModelPlus, and
all models extend this class.  It mostly works, but this line:

self.error = "Retrieve Error: %s" % e

does not work, in that self.error ends up as "Retrieve Error: ".  If I
change "e" to "e.reason", it works fine.  Could someone tell me why e
does not give me the string as intended?

I've included a bit of the code below as a reference.

Thanks very much,
Mike.


In "models.py":


class ModelPlus(models.Model):
        """Add helper methods to all models."""

        class Meta:
                abstract = True

        def __unicode__(self):
                if hasattr(self, 'name'):
                        return self.name
                else:
                        return self.id

        class RetrieveError(Exception):
                def __init__(self, reason):
                        self.reason = reason
                def __unicode__(self):
                        return self.reason


class TopList(ModelPlus):
        """Grab "Top 10" type lists."""
        name = models.CharField(max_length=64)
        error = models.CharField(max_length=128, blank=True)
        [stuff deleted]

        def retrieve(self):
                """Retrieve top entries from list."""

                self.error = ""
                try:
                        response = urllib2.urlopen(self.url, timeout =
30)
                        html = response.read()
                        response.close()
                        if not html:
                                raise self.RetrieveError("Document
Empty")
                        [stuff deleted]

                # e is empty, but e.reason is correct
                except self.RetrieveError as e:
                        self.error = "Retrieve Error: %s" % e

                # this works as expected
                except urllib2.URLError as e:
                        self.error = "URL Error: %s" % e

                self.save()
                return not bool(self.error)

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

Reply via email to