On Mon, Jul 25, 2011 at 2:50 PM, webcomm <rya...@gmail.com> wrote: > try: > self.id > except NameError: > # run this code if it is a new item > else: > # run this code if this is a previously saved item
works, but it's nicer to use "if hasattr(obj,field): ...." > More generally, what do you think of this statement: "If there are > any cases where you don't absolutely know if a variable is defined, > you have a broken design." it's generally reasonable; but in this case it's not about an undefined variable; it's an attribute. Since the ID value is defined only by the database, it's reasonable to leave it undefined until it's defined :-) -- Javier -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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.