0

I have a table Documents and it has a field named type and the type has two data's namely - type1 & type2

Now my requirements is - i have a bootstrap tabs type1 & type2, and i need to display data accordingly on the template

Which is efficient way to do this ?

Using two variables

data = Documents.objects.filter(id=pk)
type1 = data.filter(type="type1")
type2 = data.filter(type="type2")

and then pass it to context 
context = { "type1":type1,"type2":type2 }

Is there any other best way to do this ?

sixovov947
  • 205
  • 1
  • 7

1 Answers1

0

Seems like they really have to be different querysets since they are different sets of data to be rendered. Also assuming id is your primary key (which looks like it is), note that the usage of Documents.objects.filter(id=pk).filter(type=...) would just consider that 1 object with that pk, you might want to change it to Documents.objects.filter(type=...). Then you can make it a bit more dynamic by listing the possible types and then passing the filtered data to the template where the key is the type.

doc_types = ("type1", "type2")
context = {
    doc_type: Documents.objects.filter(type=doc_type)
    for doc_type in doc_types
}