Skip to content

Instantly share code, notes, and snippets.

@maxwellcc
Created May 27, 2020 11:54
Show Gist options
  • Save maxwellcc/f0455d35b906bbfff5c41c94fee71f55 to your computer and use it in GitHub Desktop.
Save maxwellcc/f0455d35b906bbfff5c41c94fee71f55 to your computer and use it in GitHub Desktop.
Django: definindo valor default para campos Foreign Key
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