Adl
Adl

Reputation: 1

How to show data from django models whose boolean field is true?

verified = models.BooleanField(default=False)

I want to show only that objects in frontend whose verified field is true in Django models.

Upvotes: 0

Views: 1255

Answers (3)

Shima Fallah
Shima Fallah

Reputation: 66

There are many ways

  1. you can handle this on your views

in views.py

modelList = modelname.objects.filter(verified=True)
  1. also you can handle it on HTML

in views.py

modelList = modelname.objects.all()

in html

{% for models in modelList %}

    {% if models.verified == True %}
       # Your Code
    {% endif %}

{% endfor %}

Upvotes: 1

Shreyash mishra
Shreyash mishra

Reputation: 790

you have to ways to achive that that either it with your views or html

first views you can filter your model to return only object which is verfied like this

name = modelname.objects.filter(verified=True)

second way or you can pass in html while you are requesting all object of that field in views in views

name = modelname.objects.all() 

then in html while fetching data

{% for name in models %}
{% if name.verified == True %}
then pass the object which are verified 
{% else %}
pass another data
{% endif %}
{% endfor %}

i hope now you got my point tell me if you got any error while implementing any of these code

Upvotes: 0

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477814

You filter the items with:

MyModel.objects.filter(verified=True)

with MyModel the model that contains the verified field.

Upvotes: 0

Related Questions