Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8405671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:54:02+00:00 2026-06-09T22:54:02+00:00

I have the following model classes: class Collection(db.Model): name = db.StringProperty() text_keys = db.ListProperty(db.Key)

  • 0

I have the following model classes:

class Collection(db.Model):
  name = db.StringProperty()
  text_keys = db.ListProperty(db.Key)

class Text(db.Model):
  name = db.StringProperty()
  content = db.StringProperty()

and I’m trying to do the following:

class Collections(webapp.RequestHandler):
  def get(self):
    collections = model.Collection.all() # works fine

    for c in collections:
      c.number_of_texts = len(c.text_keys) # does not work

    template_values = {
      'collections': collections,
    }

I’m certainly no python-expert, but shouldn’t this work?

UPDATE:

By does not work i mean that the variable number_of_texts is not added to the model object.

In my django-template, the following code generates nothing, except for the collection name:

{% for c in collections %}
<p>{{c.name}}, number of texts: {{c.number_of_texts}}</p>
{% endfor %}

SOLUTION:

Thanks to RocketDonkey for pointing out that this can be done in a more elgant fashion using django formatting:

{% for c in collections %}
<p>{{c.name}}, number of texts: {{c.text_keys|length}}</p>
{% endfor %}

or by passing a separate dictionary with the names and lenghts to the template, if a similar problem with no good formatting solution should occur.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:54:03+00:00Added an answer on June 9, 2026 at 10:54 pm

    So it appears that you are trying to write to the number_of_texts property of the Collection model (which does not exist 🙂 ). If you just need to get the number of items in that list element, you’ll need to store it in a separate variable not tied to c:

    for c in collections:
      number_of_texts = len(c.text_keys)
    

    In order to add the length of the list to your document (assuming you don’t need it anywhere else), try using the length function in your template:

    {% for c in collections %}
        <p>{{c.name}}, number of texts: {{c.text_keys|length}}</p>
    {% endfor %}
    

    This may not work depending on your templating engine (I have only used one so I’m far from an expert), but it will hopefully get you what you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following model classes: @Entity @Table(name = title) public final class Title
Let's say I have the following classes: class Votable(models.Model): name = ... class Vote(models.Model):
I have the following classes in my Model: public abstract class Entity : IEntity
I have the following classes in my models file class HardwareNode(models.Model): ip_address = models.CharField(max_length=15)
I have the following model: public class Contact { public Contact() { Name =
I have the following model structure: class Container(models.Model): pass class Generic(models.Model): name = models.CharacterField(unique=True)
I have the following model and instance: class Bashable(models.Model): name = models.CharField(max_length=100) >>> foo
I’m developing MVC application and I have following classes in my model: public class
I have the following model classes declared by SQLAlchemy: class User(Base): id = Column(Integer,
I have the following zendframework model classes class Application_Model_DbTable_Mobile extends Zend_Db_Table_Abstract { protected $_name

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.