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 975889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:39:46+00:00 2026-05-16T03:39:46+00:00

I was wondering if using indexes on a model was possible: class Buildings(models.Model): island

  • 0

I was wondering if using indexes on a model was possible:

class Buildings(models.Model):
    island = models.ForeignKey(Island)
    townhall = models.IntegerField(default=1)
    lumberjack = models.IntegerField(default=0)
    stonequarry = models.IntegerField(default=0)
    ironmine = models.IntegerField(default=0)
    [...]

a=Buildings.objects.get(somecondition)
print a.townhall # 1
a[1] +=1
print a.townhall # 2

Currently it throws

“TypeError: ‘Buildings’ object is
unindexable”

The reason why I’d like to do something like this is that using indexes would simplify parts of code like this:

if queue.kind_id == 0: buildings.townhall += 1
elif queue.kind_id == 1: buildings.lumberjack += 1
elif queue.kind_id == 2: buildings.stonequarry += 1
elif queue.kind_id == 3: buildings.ironmine += 1
elif queue.kind_id == 4: buildings.factory += 1
elif queue.kind_id == 5: buildings.shipyard += 1
elif queue.kind_id == 6: buildings.university += 1

to this:

buildings[queue.kind_id] +=1
  • 1 1 Answer
  • 3 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-05-16T03:39:46+00:00Added an answer on May 16, 2026 at 3:39 am

    The get() method doesn’t return a queryset, only a single instance/object of the model class. If you want want to retrieve more than one object (=a queryset) use filter() instead!

    a=Buildings.objects.filter(...)
    

    I am not sure what you are trying to use the lumberjack, townhall etc attributes for…
    I think you could do something like:

    buildings_list = ['townhall', 'lumberjack', ....]
    attr = buildings_list[queue.kind_id]
    setattr(buildings, attr, getattr(buildings, attr) + 1)
    

    But I am not sure what you are trying to do and if you are using django’s models in the way they are inteded to be used…

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

Sidebar

Related Questions

I was wondering if using HTML entities in meta tags (like keywords and description)
I'm wondering if using the following Javascript code is reliable: if (!document.cookie) { alert('Cookies
I've been wondering if using long descriptive variable names in WinForms C# matters for
I am wondering about using the abstraction in Zend Db RowSet instead of joins,
I was wondering if using a getter to get the same property over and
I'm just wondering when using CDATA in XML if i get the text content
I was wondering if using Lightbox all over the site would be a smart
I was wondering if when using the datagridview control you can remove the thing
I'm using TFS 2010 and am wondering whether there is an easy way, either
I'm using jQGrid and I'm wondering if there is a way to get the

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.