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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:58:42+00:00 2026-05-23T07:58:42+00:00

I am trying to create a django model which has as one of its

  • 0

I am trying to create a django model which has as one of its fields a reference to some sort of python type, which could be either a integer, string, date, or decimal.

class MyTag(models.Model):
    name = models.CharField(max_length=50)
    object = (what goes here??)

I know that if I want a foreign key to any other model, I can use GenericForeignKeys and content_types. How can I have a model field that references any python type? The only idea I have come up with so far is to create models that are simple wrappers on objects and use GenericForeignKeys.

Is there any way to do this?

  • 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-23T07:58:43+00:00Added an answer on May 23, 2026 at 7:58 am

    Since you want to filter, you would need some kind of DB support for your field and json field won’t be that valuable for you. You can use some different solution with different
    complication levels according to your actual need. One suggestion is to serialize your data to string. Pad with enough zeros for string/integer/float sorting. Now you can filter all your stuff as string (make sure you pad the value you are filtering by as well). Add a data_type column for fetching the right python object.

    TYPES = [(int, 1), (Decimal, 2),(date, 3), (str, 4)]
    class MyTag(models.Model):
        name = models.CharField(max_length=50)
        data_type = models.IntegetField(choices=TYPES)
        value = models.CharField(max_length=100)
    
        def set_the_value(self, value):
            choices = dict(TYPES)
            self.data_type = choices[type(value)]
            if self.data_type == int:
                self.value = "%010d" % value
            # else... repeat for other data types
    
        def get_the_value(self):
            choices = dict([(y,x) for x,y in TYPES])
            return choices[self.data_type](self.value)
    

    (Disclaimer: this is a hack, but probably not the worst one).

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

Sidebar

Related Questions

I'm trying to create a Django model that handles the following: An Item can
I am trying to create an external python client to access a django app.
I am trying to create a simple view in Django & GAE, which will
In Django, I need to create a model in which an instance of that
I have created a blog app which its model has an author field like
I'm learning Python and django at the same time. I'm trying to create an
Basically I'm trying to save a Django model which contains an ImageField , and
I have recently being trying to create a project which has several levels of
I am trying to use Django to create a recursive relationship, which gives users
Hello and thank you in advance, I am trying to create a django form

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.