1👍
Without changing the models i found the answer!
views.py
def method(request):
""""""""""
typeList = Types.objects.filter(user=user, is_active=True, parent_type_id=None)
list = []
for type in typeList:
if not type.parent_type_id:
list.append(type)
subtype = Types.objects.filter(parent_type_id=type.id, is_active=True)
for subtypes in subtype:
list.append(subtypes)
""""""""
return render(request, 'some.html',
{'typeList':list,})
template.html
{% for type in typeList%}
{% if type.parent_type_id == None %}
<h1>{{type.title}}</h1>
{% else %}
<label><input type="checkbox" {% if type.id in selection %}checked="True" {% endif %} value="{{ type.id }}" name="type_key">{{ type.title }}</label><br />
{% endif %}
{% endfor %}
0👍
Can you post the view from where you are passing the type and typelist so that we can what are you passing exactly and how are both related to each other.
- [Answer]-Django and Apache / mod_wsgi not rendering css
- [Answer]-Deploy Django Project On Apache Server with Cpanel
- [Answer]-Update permission when user field changes in Django
0👍
try this, I think you should actually have the parent_type_id be a foreign key, but it may still work.
{% for type in types %} <h1>{{type.title}}</h1>
{% for field in typelist %}
{% if field.parent_type_id == type.id %}
<label><input type="checkbox" name="{{field}}">{{ field }}</label><br />
{% endif %}
{% endfor %}
{% endfor %} <br />
I think for the above to work, your model is going to need to change to something like this:
class Types(models.Model):
user = models.ForeignKey(User, null=True)
title = models.CharField('Incident Type', max_length=200)
parent_type_id = models.ForeignKey('self', null=True, blank=True)
is_active = models.BooleanField('Is Active', default=True)
- [Answer]-HTML Nav isn't working as expected
- [Answer]-Django – counting objects in database inside model declaraton
0👍
A Null-able
CharField can not be None
unless you set it specifically! If you set values through admin or any TextInput, python can not distinct empty-string from None
, so it will set it as empty-string. Documentation is here
In your model you define as:
parent_type_id = models.CharField('Parent Type', max_length=100, null=True, blank=True)
And in your views, following will always return an empty QuerySet. So it never executes the for
loop in your template
types = Types.objects.filter(user=user.id, parent_type_id=None)
It is better to use Q
in here
from django.db.models import Q
types = Types.objects.filter(user=user.id, Q(parent_type_id=None|parent_type_id=''))
- [Answer]-Permission denied on trying to write file
- [Answer]-Scrappy log message in log file
- [Answer]-Django Admin change_list forcing search_form to reject given seach keywords
- [Answer]-Nginx redirects www. to wrong Vhost