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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:53:28+00:00 2026-05-26T14:53:28+00:00

As a project to figure out Django I’m trying to build a small game.

  • 0

As a project to figure out Django I’m trying to build a small game.

A player has a base. A base has several type of items it can harbor. (Vehicle, Defense, Building).

I have 3 static tables which contain information for the first level of each item (in the game these values are used in formulas to calculate stuff for upgrades). I’ve used a sequence to insert all these items in these different tables so the ID’s are unique across tables.

To keep track of what items the player has per base I have a table ‘Property’. I want to use a single field as a reference to the ID of an item and trying to get this done with the Django models.

Warning: my knowledge about Django models are pretty limited and I’ve been stuck with this a few days now.

Is this possible and if so how can it be done?

I tried using annotations on the save method to change the value of a field by overwriting the field with the id of that object before trying to query the object by id when trying to ‘get’ the object, however I can’t get past the obvious restriction of the model when defining that field as an Integer – I hoped it wouldn’t validate until I called save()

def getPropertyItemID(func):
    """
    This method sets the referral ID to an item to the actual ID.
    """

    def decoratedFunction(*args):
        # Grab a reference to the data object we want to update.
        data_object=args[0]

        # Set the ID if item is not empty.
        if data_object.item is not None:
            data_object.item=data_object.item.id

        # Execute the function we're decorating
        return func(*args)

    return decoratedFunction

class Property(models.Model):
    """
    This class represents items that a user has per base.
    """

    user=models.ForeignKey(User)
    base=models.ForeignKey(Base)
    item=models.IntegerField()
    amount=models.IntegerField(default=0)
    level=models.SmallIntegerField(default=0)

    class Meta:
        db_table='property'

    @getPropertyItemID
    def save(self):
        # Now actually save the object
        super(Property, self).save()

I hope you can help me here. The end result I’d like to be able to put to use would be something like:

    # Adding - automatically saving the ID of item regardless of the class 
    # of item
    item = Property(user=user, base=base, item=building)
    item.save()

    # Retrieving - automatically create an instance of an object based on the ID 
    # of item, regardless of the table this ID is found in.
    building = Property.objects.all().distinct(True).get(base=base, item=Building.objects.all().distinct(True).get(name='Tower'))
    # At this point building should be an instance of the Building model

If I’m completely off and I can achieve this differently I’m all ears 🙂

  • 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-05-26T14:53:29+00:00Added an answer on May 26, 2026 at 2:53 pm

    I think you are looking for a Generic Relationship:

    class Property(models.Model):
        user=models.ForeignKey(User)
        base=models.ForeignKey(Base)
        content_type = models.ForeignKey(ContentType) # Which model is `item` representing?
        object_id = models.PositiveIntegerField() # What is its primary key?
        item=generic.GenericForeignKey('content_type', 'object_id') # Easy way to access it.
        amount=models.IntegerField(default=0)
        level=models.SmallIntegerField(default=0)
    

    This lets you create items as you mentioned, however you would probably need to look at a different way of filtering those items out.

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

Sidebar

Related Questions

For a small/medium sized project I'm trying to figure out what is the 'ideal'
I'm trying to figure out how to use https://github.com/azicchetti/jquerymobile-router in my Spine project. I'm
I have a new asp.net mvc project and i am trying to figure out
Working on a project using Entity Framework (4.3.1.0). I'm trying to figure out how
I am trying to figure out what changed within a project that now causes
I'm using CodeSmith on my current project and I'm trying to figure out an
I'm starting a new project and I'm trying to figure out what technologies I
I'm roughly 10% into my website project and I'm trying to figure out what
I am trying to figure out general guidelines for arranging views/templates in a django
I'm trying to figure out how to configure my project such that JPA will

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.