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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:19:54+00:00 2026-06-05T05:19:54+00:00

I am trying to design an abstract model that contains a field. Subclassed models

  • 0

I am trying to design an abstract model that contains a field. Subclassed models will have this field, but they will be of various field types.

Example

class AbsModel(models.Model):
    data = models.??? #I want subclasses to choose this

    def __unicode__(self):
        return data.__str__()

    class Meta:
        abstract = True

class TimeModel(AbsModel):
    data = models.TimeField()
    ...

class CharModel(AbsModel):
    data = models.CharField(...)
    ...

I am looking for a way to enforce the existence of the data field so I can write unicode once for all objects.

If this isn’t possible, how can I refer to the “data” field of the subclass when calling the super class’s unicode

I have a feeling this second question has an obvious answer I am missing.

  • 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-05T05:19:56+00:00Added an answer on June 5, 2026 at 5:19 am

    It’s not possible to override a superclass field where the field is of type models.Field.

    https://docs.djangoproject.com/en/1.4/topics/db/models/#field-name-hiding-is-not-permitted

    You can get round this by defining a field of another type in the superclass, and then overriding it in the child (perhaps include a __str__() method just in case the data field isn’t overriden).

    from django.db import models
    
    
    class AbsDataField:
        def __str__(self):
            return "undefined"
    
    class AbsModel(models.Model):
        data = AbsDataField
    
        def __unicode__(self):
            return self.data.__str__()
    
        class Meta:
            abstract = True
    
    class TimeModel(AbsModel):
        data = models.TimeField()
        #...
    
    class CharModel(AbsModel):
        data = models.CharField(max_length=32)
        #...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to design a database model and I have a question. I need
I'm trying to design an entity model for an application that uses ASP.Net membership
I'm trying to design a couple of classes that inherit a partial function, but
I am trying to solve which design pattern I have to use for this
In C# I have an intrusive tree structure that looks like this: public abstract
I'm having troubles trying to design classes. I have abstract Chip class, STM8 class
Trying to design simple aspect,that will print word logg to console,when any of public
I have a class that inherits from an abstract base class. I am trying
I am a beginner in design patterns. I am trying to use Abstract Factory
I'm trying to design a calendar-type table that is built dynamically based on data

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.