Skip to content

Instantly share code, notes, and snippets.

@guilhermecarvalhocarneiro
Created May 20, 2014 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guilhermecarvalhocarneiro/f00a8b38b51a797f810f to your computer and use it in GitHub Desktop.
Save guilhermecarvalhocarneiro/f00a8b38b51a797f810f to your computer and use it in GitHub Desktop.
Ocultando campos no get_form Django
def get_form(self, request, obj=None, **kwargs):
self.exclude = []
usuario = Usuario(user=request.user).get_user()
try:
if not request.user.is_superuser:
if usuario.estabelecimento:
self.fields["cidade"].queryset = Cidade.objects.filter(pk=usuario.cidade.pk)
self.readonly_fields = ('atrativo', 'cidade', 'estabelecimento')
# Verificar como setar os valores para os campos cidade e estabelecimento
else:
self.readonly_fields = ('', )
except Exception, e:
print e
finally:
return super(EventoAdmin, self).get_form(request, obj, **kwargs)
def formfield_for_foreignkey(self, db_field, request, **kwargs):
usuario = Usuario(user=request.user).get_user()
try:
if request.user.is_superuser is False:
if db_field.name == "cidade":
kwargs["queryset"] = Cidade.objects.filter(pk=usuario.cidade.pk)
if db_field.name == "estabelecimento" and usuario.estabelecimento:
kwargs["queryset"] = Estabelecimento.objects.filter(pk=usuario.estabelecimento.pk)
finally:
return super(EventoAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment