Created
May 27, 2020 11:54
-
-
Save maxwellcc/f0455d35b906bbfff5c41c94fee71f55 to your computer and use it in GitHub Desktop.
Django: definindo valor default para campos Foreign Key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_symbol_default(): | |
""" | |
Busca pelo ícone padrão do sistema | |
:return: retorna o valor default do | |
""" | |
try: | |
default_icon = Default.objects.first().get('default_icon') | |
except BaseException as error: | |
raise BaseException(_('Register a default icon.')) from error | |
return default_icon | |
class ModelName(models.Model): | |
(...) | |
symbol = models.ForeignKey( | |
Icon, | |
verbose_name=_('symbol'), | |
null=True, | |
on_delete=models.SET_NULL, | |
default=get_symbol_default, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment