2012-07-14 13 views

risposta

14

È possibile utilizzare la funzione hasattr:

if hasattr(settings, 'name_of_setting'): 
    # the setting exists 
else: 
    # the setting does not exist 
+0

Um ho ottenuto eccitato circa la vostra risposta. ma fallisce ancora. >>> se getattr (impostazioni, 'junk'): ... print 'Ciao' ... Traceback (chiamata più recente scorso): file "", linea 1, in File "/ Utenti /ben/Envs/menus_proto/lib/python2.7/site-packages/django/utils/functional.py", la linea 277, in __getattr__ getattr ritorno (self._wrapped, nome) AttributeError: oggetto 'Impostazioni' non ha alcuna attributo 'JUNK' –

+3

È necessario fornire un valore predefinito come terzo argomento, ovvero 'getattr (impostazioni, 'nome', Nessuno)'. Oppure, dato che non hai bisogno del valore attuale, usa 'hasattr (settings, 'name')' che controlla solo il nome e non ne ottiene il valore. –

+0

Ho aggiornato la risposta. –

Problemi correlati