0

I consider it bad style to use try: except: for flow control, but I can't figure out how to write the following code to test if a DB field in Django exists. This is the "dirty" code which works:

@receiver(pre_save, sender=UserProfile)
def create_user_profile(sender, instance=None, **kwargs):

    try:
        print str(instance.bank_account)
    except:
        print 'No account'

I would rather wanna do something like this, but I get an exception when the if statement is run and the object does not exist:

@receiver(pre_save, sender=UserProfile)
def create_user_profile(sender, instance=None, **kwargs):

    if instance.bank_account is None:
        print 'No account'
    else:
        print str(instance.bank_account)
4
  • Why would the field ever not exist? Commented Mar 13, 2012 at 10:52
  • before it was created and saved for the first time Commented Mar 13, 2012 at 10:54
  • 2
    Out of curiosity: are you following a project guideline or is it a personal preference? I'd consider your unqualified except bad style, but not if you used except AttributeError. I'd much prefer your first example to your second then. Commented Mar 13, 2012 at 10:55
  • 1
    Python is a language that encourages the 'ask for forgiveness, not permission' mantra. Unless you think it's more likely that the account won't exist most of the time, just catch a (specific) exception. docs.python.org/glossary.html#term-eafp Commented Mar 13, 2012 at 11:16

3 Answers 3

3

I'm guessing you encounter a BankAccount.DoesNotExist error? If that's the case, you could reverse the relationship between UserProfile and BankAccount. Have a look at Django ticket #10227 to find out what's going on here.

That being said, if I needed to work with your code I'd rather see an explicit try...except instead of a workaround, unless that workaround actually makes sense.

Sign up to request clarification or add additional context in comments.

1 Comment

That ticket is describing my exact problem. Thx. I will use try: except DoesNotExist:
2

I much prefer your first example to your second, but perhaps you could use hasattr(instance,'bank_account') as a safeguard?

Comments

2

First of all, your first example is the preferred way of doing this (although, you might like to narrow down the except to just the exceptions you expect for this operation).

I imagine that your if statement is blowing up when instance is None; the solution is to add a test to your if statement, to test instance first:

if instance and instance.bank_account:
    print str(instance.bank_account)
else: print 'No account'

If you don't know if the field exists, you could replace instance.bank_account with getattr(instance, 'bank_account', None), which both retrieves the value, and deals with the possibility of the attribute not existing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.