Created
May 20, 2014 14:19
-
-
Save guilhermecarvalhocarneiro/f00a8b38b51a797f810f to your computer and use it in GitHub Desktop.
Ocultando campos no get_form Django
This file contains hidden or 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_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