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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:08:50+00:00 2026-06-04T17:08:50+00:00

In Django I have a parent abstract class with a few common fields, and

  • 0

In Django I have a parent abstract class with a few common fields, and some subclasses that add more fields. In some of these subclasses I would like to add custom validators that validate fields on the parent class.

class Template(models.Model):
  text = models.CharField(max_length=200)

class GlobalTemplate(Template):
  function = models.ManyToManyField(Function, null=True, blank=True)

I can easily add them on the field in the parent class like this:

class Template(models.Model):
  text = models.CharField(max_length=200, validators=[validate_custom])

But in this case I want to add the validator to my child class GlobalTemplate, but have it attached to the text field.

Is this possible in Django?

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-06-04T17:08:55+00:00Added an answer on June 4, 2026 at 5:08 pm

    Field validators are stored in field.validators, it’s a list, so you basically need to append your validators there.

    To access the field instance, you’ll have to play a little bit with the _meta attribute of your object (note that you’re not supposed to play with this attribute, so when you update django, you’ll have to check that it has not changed). Here’s how you could do it:

    def get_fields_dict(self):
        return dict((field.name, field) for field in self._meta.fields)
    
    def __init__(self, *args, **kwargs):
        super(GlobalTeplate, self).__init__(*args, **kwargs)
        text_field = self.get_fields_dict()['text']
        text_field.validators.append(validate_custom)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Django, when you have a parent class and multiple child classes that inherit
Introspection tells me that django fields have a hidden_widget attribute. Maybe its not this
I have a base class that is a Django form. The child class then
I have these models set up in Django: class SourceBusiness(models.Model): source = models.CharField(max_length=100) ...(other
So I have some Django 1.3 models like this: class Type(models.Model): is_bulk = models.BooleanField()
I have some Django models with a structure similar to this: class Grandparent(models.Model): name
I have these Django models: class Group(models.Model): name = models.CharField(max_length=100) parent_group = models.ManyToManyField("self", blank=True)
I have a Django model which looks like this: class MyModel(models.Model): parent = models.ForeignKey(ParentModel)
I have two django sites that use the same database and share some of
I have a Django model (schedule) with the class of entity, that is 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.