try this get_form:

def get_form(self, request, obj=None, **kwargs):
if obj.readiness:
self.exclude = ("readinessDescription", )
return super(RequestForChangeAdmin, self).get_form(request, obj, **kwargs)


I have never used 'exclude' but it looks like yours should work.


On Fri, May 30, 2014 at 9:18 AM, Hilde Rafaelsen <[email protected]>
wrote:

> Thanx,
>
> I now changed my if test to just if obj.readines but then I get the error
> message 'NoneType' object has no attribute 'readiness'
>
>
> I have tried with several fields from my model, but I always get this
> error. I guess it must be something essential I am doing wrong here, since
> my obj seems to be none always...
>
> I have get_form inside my modelAdmin class.
>
>
> On Friday, May 30, 2014 2:06:59 PM UTC+2, Timothy W. Cook wrote:
>
>> What do you mean by tri-state?
>>
>> You are using a field that allows three states: TRUE, FALSE and NULL.
>>
>> If I understand the model, readiness would either be TRUE or FALSE.  But
>> if you do not want to change your field type to:
>> readiness = models.BooleanField("Readiness", blank=True)
>>
>> then try changing:
>> if obj.readiness == True:
>>
>> to:
>>
>> if obj.readiness and obj.readiness == True:
>>
>> or just:
>> if obj.readiness:
>>
>> Will give you the same result.
>>
>> This way if it doesn't exist it will not fail on the  '== TRUE' test.
>>
>>
>>
>> On Fri, May 30, 2014 at 8:49 AM, Hilde Rafaelsen <[email protected]>
>> wrote:
>>
>>> Hi,
>>>
>>>
>>>
>>> Thanks for the response.
>>>
>>>
>>>
>>> What do you mean by tri-state?
>>>
>>>
>>>
>>> I changed my get_form to this:
>>>
>>> def get_form(self, request, obj=None, **kwargs):
>>>
>>>                                form = super(RequestForChangeAdmin,
>>> self).get_form(request, obj, **kwargs)
>>>
>>>                                if obj:
>>>
>>>                                                if obj.readiness == True:
>>>
>>>
>>> self.exclude = ("readinessDescription", )
>>>
>>>                                                                form =
>>> super(RequestForChangeAdmin, self).get_form(request, obj, **kwargs)
>>>
>>>                                return form
>>>
>>>
>>>
>>> No my code run without errors, but nothing happpends though ☺
>>>
>>>
>>>
>>> I have tried to change the readiness filds to yes and no, but the field
>>> readiness descriptions is always displayed. Maybe it is not possibly to get
>>> a fields attribute value from admin before the admin form is posted or
>>> stored in database? Do you know?
>>>
>>>
>>>
>>> Regards,
>>>
>>> Hilde from Norway
>>>
>>> On Friday, May 30, 2014 12:57:19 PM UTC+2, Hilde Rafaelsen wrote:
>>>>
>>>> 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 [email protected].
>>> To post to this group, send email to [email protected].
>>>
>>> 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/5bc768df-3a1c-4b53-bbbb-2ec19b9c05f0%
>>> 40googlegroups.com
>>> <https://groups.google.com/d/msgid/django-users/5bc768df-3a1c-4b53-bbbb-2ec19b9c05f0%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>
>>
>> --
>>
>> ============================================
>> Timothy Cook
>> LinkedIn Profile:http://www.linkedin.com/in/timothywaynecook
>> MLHIM http://www.mlhim.org
>>
>>   --
> 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 [email protected].
> To post to this group, send email to [email protected].
> 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/cd42fea2-88f0-4f0f-b98e-985f7c464189%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/cd42fea2-88f0-4f0f-b98e-985f7c464189%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>



-- 

============================================
Timothy Cook
LinkedIn Profile:http://www.linkedin.com/in/timothywaynecook
MLHIM http://www.mlhim.org

-- 
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 [email protected].
To post to this group, send email to [email protected].
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/CA%2B%3DOU3WWVa0L5jJkY4xrQxE0-ZUbr2_MEcJrSq%3DmT2J8qZ_-EA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to