JustADudeTryingToCode
JustADudeTryingToCode

Reputation: 117

How to loop over multiple fields in Django template?

I have models that inherit from an abstract model like this:

class ImprovementAbstraction(models.Model):
    needsImprovement = models.BooleanField()
    hasFallacies = models.BooleanField()
    hasEmotionalDeficiency = models.BooleanField()
    isNecessaryToObtain = models.BooleanField()
    doesAReallyCauseB = models.BooleanField()
    areAllStepsPresent = models.BooleanField()
    isCauseSufficient = models.BooleanField()
    areAllClausesIdentified = models.BooleanField()
    isCausalityCertain = models.BooleanField()
    languageIsEnglish = models.BooleanField()
    isTautologyPresent = models.BooleanField()

    class Meta:
        abstract = True

class Assumption(MainAbstractModel, ImprovementAbstraction):
    need = models.ForeignKey(Need, on_delete=models.SET_NULL, null=True)
    assumption = models.CharField(max_length=500, default="", null=True)

    def __str__(self):
        return self.assumption

In the template I would like to display all of the "ToImprovementAbstraction" Model fields associated with the Assumption model. Is there a way to loop over all the fields in the template, something like Assumption.ImprovementAbstractionFields.all() (made up code)?

Upvotes: 1

Views: 978

Answers (3)

Tospaa
Tospaa

Reputation: 31

I use the built-in vars() method for that.

For example, you have an Assumption object:

assumptionObject = .models.Assumption.objects.get(pk=1)

If you use vars() method with that query object like this:

vars(assumptionObject)

it will return a dictionary containing all the field names and values as a Python dictionary.

If you only want the field names you can use it like this:

vars(assumptionObject).keys()

EDIT: I should warn you that, if you use vars() on a query object, the returned dictionary will contain a django.db.models.base.ModelState object stored in a key called _state. If you're going to use the values in a for loop or something, you should put an exception for that.

Upvotes: 1

ngawang13
ngawang13

Reputation: 673

get the assumption object in view and render the ImprovementAbstraction through foreign key. for example:

def get_item():
  queryset = Assumption.objects.all()

return render(request, 'template.html', {'queryset': queryset})

Now in your template you can access the data this way

{% for query in queryset %}
  {{query.need.needImprovement}}
  {{query.need.hasFallacies}}
  {{...}}

 {% endfor %}

This way you can display everything in one loop. Hope this gets you some idea.

Upvotes: 0

JSRB
JSRB

Reputation: 2613

Not exactly sure what your desired outcome is, but this is the basic approach to query data from the database and render the html:

You will first have to query the data from the database like so:

Views.py

def search(request):
    queryset = ToImprovementAbstraction.objects.all()
    context = {
        'queryset': queryset
    }
    return render(request, 'your_template.html', context)

Then you can use the data to render your template like so:

your_template.html

{% for item in queryset %}

<p>{{ item.needsImprovement }}</p>
[...]
[...]

{% endfor %}

If oyu have multiple models you can make multiple queries to use one/many-to-many fields in your models to link them straight away.

Upvotes: 0

Related Questions