Friday, May 30, 2014

Problem hiding fields from admin page based on another fields value

Hello,

In my django admin page I want to hide some filelds from users if a special field value is set. I have tried different solutions to solve my problem but is stucked and hope someone can help me,

If you look at my model I want to hide the field readinessDescription if readiness value is False (no), when the user log into admin page. 

In model.py:
class RequestForChange (models.Model):
rfc = models.AutoField (primary_key=True, help_text="This is the grey text")
heading = models.CharField("Heading", max_length=50)
       readiness = models.NullBooleanField("Readiness", blank=True, null=True)
       readinessDescription = models.TextField("Description of readiness", max_length=250, blank=True, null=True)


I found some hints on the web about using get_form from my RequestForChangeAdmin page, but it won't work because i get this error 'NoneType' object has no attribute 'readiness'


Here is what I have in admin.py:

class RequestForChangeAdmin(admin.ModelAdmin):
formfield_overrides = {
models.ManyToManyField: {'widget': CheckboxSelectMultiple},
}
list_display = ('rfc', 'heading', 'enviroment', 'status')
search_fields = ('changeId',)
list_filter = ('enviroment', 'acceptAT', 'status')
date_hierarchy = 'criticalDate'
inline = [RequestForChangeInline]
def get_form(self, request, obj=None, **kwargs):
form = super(RequestForChangeAdmin, self).get_form(request, obj, **kwargs)
print obj
if obj.readiness == True:
self.exclude = ("readinessDescription", )
form = super(RequestForChangeAdmin, self).get_form(request, obj, **kwargs)
return form

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2c1d74b1-c03c-4c2b-b8db-7d44a06531c4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment