# How can I control fields to include or exclude based on model's boolean field?

**URL:** <https://forum.djangoproject.com/t/how-can-i-control-fields-to-include-or-exclude-based-on-models-boolean-field/6658>\
**Category:** Using Django\
**Created:** [February 18, 2021, 2:50pm UTC](https://forum.djangoproject.com/t/how-can-i-control-fields-to-include-or-exclude-based-on-models-boolean-field/6658 "2021-02-18T14:50:55Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![devspectre](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/devspectre/32/3559_2.png) [@devspectre](https://forum.djangoproject.com/u/devspectre)\
**Post date:** [February 18, 2021, 2:50pm UTC](https://forum.djangoproject.com/t/how-can-i-control-fields-to-include-or-exclude-based-on-models-boolean-field/6658/1 "2021-02-18T14:50:55Z")

</div>

I’ve got a model which has nearly ten fields and one of them is Boolean field.

class Person(models.Model):  
first\_name = models.CharField(max\_length=30)  
last\_name = models.CharField(max\_length=30)  
nick\_name = models.CharField(max\_length=30)  
be\_anonymous = models.BooleanField(default=False)  
character = models.ChoiceField(…)  
…

I’ve got a ViewSet and serializers for this model and there’s an endpoint to get list of all Persons.  
For some persons, be\_anonymous is set to True and for those persons, I want to hide first\_name and last\_name from API response and instead include nick\_name.

How can I hide/show those fields based on that Boolean field?

Any advice will be appreciated

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 18, 2021, 3:07pm UTC](https://forum.djangoproject.com/t/how-can-i-control-fields-to-include-or-exclude-based-on-models-boolean-field/6658/2 "2021-02-18T15:07:32Z")

</div>

My initial reaction would be that you could put that logic in your serializer. See [Overriding serialization and deserialization behavior](https://www.django-rest-framework.org/api-guide/serializers/#overriding-serialization-and-deserialization-behavior) - the `to_representation()` method.

---

<div class="post-metadata">

**Author:** ![devspectre](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/devspectre/32/3559_2.png) [@devspectre](https://forum.djangoproject.com/u/devspectre)\
**Post date:** [February 18, 2021, 4:33pm UTC](https://forum.djangoproject.com/t/how-can-i-control-fields-to-include-or-exclude-based-on-models-boolean-field/6658/3 "2021-02-18T16:33:16Z")

</div>

Hi, Ken!

Thanks for your answer!  
I think that’s a correct approach, however, I am still curious to know how I can exclude those fields.  
I can check out if the boolean field is set to True or False on to\_representation method.  
What is the best practice to exclude fields?

Thanks

---

<div class="post-metadata">

**Author:** ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)\
**Post date:** [February 18, 2021, 5:10pm UTC](https://forum.djangoproject.com/t/how-can-i-control-fields-to-include-or-exclude-based-on-models-boolean-field/6658/4 "2021-02-18T17:10:50Z")

</div>

I’d entertain the idea of creating a serializer that has all three fields, `first_name`, `last_name`, `nick_name`, then using [`serializers.SerializerMethodField`](https://www.django-rest-framework.org/api-guide/fields/#serializermethodfield) for them to identify whether to return the value or `None`.

I like this approach because API has the same shape for all cases.

---

<div class="post-metadata">

**Author:** ![devspectre](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/devspectre/32/3559_2.png) [@devspectre](https://forum.djangoproject.com/u/devspectre)\
**Post date:** [February 18, 2021, 5:12pm UTC](https://forum.djangoproject.com/t/how-can-i-control-fields-to-include-or-exclude-based-on-models-boolean-field/6658/5 "2021-02-18T17:12:10Z")

</div>

That sounds interesting but the api response will still have those keys with empty values, right?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 18, 2021, 5:12pm UTC](https://forum.djangoproject.com/t/how-can-i-control-fields-to-include-or-exclude-based-on-models-boolean-field/6658/6 "2021-02-18T17:12:17Z")

</div>

Don’t look to make this more complicated than necessary.  
The internal representation is probably going to be a Python dict (unless you’re doing something explicitly different) - so I would just check the boolean field and change the dict as desired. I wouldn’t worry about what someone considers “best practices” for this - at this layer of the code, no reasonable approach is going to be significantly different than any other.

---

<div class="post-metadata">

**Author:** ![devspectre](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/devspectre/32/3559_2.png) [@devspectre](https://forum.djangoproject.com/u/devspectre)\
**Post date:** [February 18, 2021, 5:13pm UTC](https://forum.djangoproject.com/t/how-can-i-control-fields-to-include-or-exclude-based-on-models-boolean-field/6658/7 "2021-02-18T17:13:13Z")

</div>

I will try that Ken, 😃

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 18, 2021, 5:14pm UTC](https://forum.djangoproject.com/t/how-can-i-control-fields-to-include-or-exclude-based-on-models-boolean-field/6658/8 "2021-02-18T17:14:14Z")

</div>

That is true - and there’s a lot to be said for doing it that way. It really depends upon what the client is going to expect from your response - either the field is missing or is present with a null value.

---

<div class="post-metadata">

**Author:** ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)\
**Post date:** [February 18, 2021, 5:14pm UTC](https://forum.djangoproject.com/t/how-can-i-control-fields-to-include-or-exclude-based-on-models-boolean-field/6658/9 "2021-02-18T17:14:23Z")

</div>

Correct. That’s what I meant by “shape”. It depends on your clients’ expectations whether this would even be a viable approach.

---

<div class="post-metadata">

**Author:** ![devspectre](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/devspectre/32/3559_2.png) [@devspectre](https://forum.djangoproject.com/u/devspectre)\
**Post date:** [February 18, 2021, 5:15pm UTC](https://forum.djangoproject.com/t/how-can-i-control-fields-to-include-or-exclude-based-on-models-boolean-field/6658/10 "2021-02-18T17:15:02Z")

</div>

I got it, thank you. 🙂

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 18, 2021, 5:15pm UTC](https://forum.djangoproject.com/t/how-can-i-control-fields-to-include-or-exclude-based-on-models-boolean-field/6658/11 "2021-02-18T17:15:26Z")

</div>

GMTA! 😀(with padding to make this post 20 characters.)

---

<div class="post-metadata">

**Author:** ![devspectre](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/devspectre/32/3559_2.png) [@devspectre](https://forum.djangoproject.com/u/devspectre)\
**Post date:** [February 18, 2021, 5:34pm UTC](https://forum.djangoproject.com/t/how-can-i-control-fields-to-include-or-exclude-based-on-models-boolean-field/6658/12 "2021-02-18T17:34:33Z")

</div>

It works like a charm. Very nice! 👍
