Use the `DateTypeModelClass.DoesNotExist` exception.
By the way, make sure you use `select_related()` if you want to use `__unicode__` on those instances or it will results in 1 + N * 6 queries where N = `Channel.objects.count()`.
Le vendredi 1 novembre 2013 12:46:21 UTC-4, Rick Shory a écrit :
-- By the way, make sure you use `select_related()` if you want to use `__unicode__` on those instances or it will results in 1 + N * 6 queries where N = `Channel.objects.count()`.
Le vendredi 1 novembre 2013 12:46:21 UTC-4, Rick Shory a écrit :
I have a model "Channel" with various foreign key fields (e.g. "data_type" below).I have a __unicode__ method to give intelligible information about the model instance by pulling in the foreign key info.For example:def __unicode__(self):...t = self.data_type...return u'%s,%s,%s,%s,%s,%s' % (c, l, s, t, u, o)(Each of the foreign key models have their own __unicode__ method.)This works fine, unless the instance is incomplete:>>> c = Channel(column = 3)>>> cTraceback (most recent call last):File "<console>", line 1, in <module>File "C:\Python27\lib\site-packages\django\db\models\ base.py", line 421, in __ repr__u = six.text_type(self)File "C:\...\models.py", line 42, in __unicode__t = self.data_typeFile "C:\Python27\lib\site-packages\django\db\models\ fields\related.py", line 389, in __get__raise self.field.rel.to.DoesNotExistDoesNotExistHow would I correctly write the error handling? This does not quite work:try:t = self.data_typeexcept self.data_type.DoesNotExist:t = ''I've tried various formats for that "except" line, such as:except DoesNotExist:except self.field.rel.to.DoesNotExist: except self.DoesNotExist:I can use the general:except:But I need to learn how to do this.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6cee38d8-506f-424d-a1dd-6a4c46c16ffe%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
No comments:
Post a Comment