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

  • Home
  • SEARCH
  • 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 6674045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:41:59+00:00 2026-05-26T03:41:59+00:00

Below is a stripped down model and associated method. I am looking for a

  • 0

Below is a stripped down model and associated method. I am looking for a simple way upon executing a query to get all of the needed information in a single answer without having to re-query everything. The challenge here is the value is dependent upon the signedness of value_id.

class Property(models.Model):
    property_definition = models.ForeignKey(PropertyDefinition)
    owner = models.IntegerField()
    value_id = models.IntegerField()

    def get_value(self):
        if self.value_id < 0: return PropertyLong.objects.get(id=-self.value_id)
        else: return PropertyShort.objects.get(id=self.value_id)

Right now to get the “value” I need to do this:

object = Property.objects.get(property_definition__name="foo")
print object.get_value()

Can someone provide a cleaner way to solve this or is it “good” enough? Ideally I would like to simply just do this.

object = Property.objects.get(property_definition__name="foo")
object.value

Thanks

  • 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-26T03:41:59+00:00Added an answer on May 26, 2026 at 3:41 am

    Given this is a bad design. You can use the builtin property decorator for your method to make it act as a property.

    class Property(models.Model):
        property_definition = models.ForeignKey(PropertyDefinition)
        owner = models.IntegerField()
        value_id = models.IntegerField()
    
        @property
        def value(self):
            if self.value_id < 0: return PropertyLong.objects.get(id=-self.value_id)
            else: return PropertyShort.objects.get(id=self.value_id)
    

    This would enable you to do what you’d ideally like to do: Property.objects.get(pk=1).value

    But I would go as far as to call this “cleaner”. 😉

    You could go further and write your own custom model field by extending django.models.Field to hide the nastiness in your schema behind an API. This would at least give you the API you want now, so you can migrate the nastiness out later.

    That or the Generic Keys mentioned by others. Choose your poison…

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

Sidebar

Related Questions

Below is a jquery script I am working on, I have stripped down all
The link below is an image URL where the extension has been stripped. I
Below I have a very simple example of what I'm trying to do. I
Below is the code of a simple html with a table layout. In FF
So I made this super stripped down version of a html css jquery navigation
Here is a stripped down version of what I use to authenticate users, it
I stripped this example to make it simple. I have a gridview with a
The code below does NOT work: Cause: I assume I tracked down the cause
Let's say I compiled the application below and stripped it's symbols. #include <stdio.h> int
I am having trouble with my jQuery script below, this is a basic stripped

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.