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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:54:06+00:00 2026-05-29T21:54:06+00:00

I have the following models (simplified): class Location(models.Model): name = models.CharField(max_length=100) is_ok = models.BooleanField()

  • 0

I have the following models (simplified):

class Location(models.Model):
    name = models.CharField(max_length=100)
    is_ok = models.BooleanField()

class Profile(models.Model):
    name = models.CharField(max_length=100)
    location = models.ForeignKey(Location)

class AnotherThing(models.Model):
    name = models.CharField(max_length=100)
    location = models.ForeignKey(Location)

I use a modelform to allow the users to add/edit Profile and AnotherThing items in the database.
Simplified version:

class ProfileForm(ModelForm):
    class Meta:
        model = Profile
        widgets = {'location': CustomLocationWidget()}

class AnotherThingForm(ModelForm):
    class Meta:
        model = Profile
        widgets = {'location': CustomLocationWidget()}

The simplified code for CustomLocationWidget is something like this:

class CustomLocationWidget(Input):
    def __init__(self, *args, **kwargs):
        super(CustomLocationWidget, self).__init__(*args, **kwargs)

    def render(self, name, value, attrs = None):
        output = super(CustomLocationWidget).render(name, value, attrs)
        output += 'Hello there!'
        return mark_safe(output)

As another validation I need to check that the Location has is_ok == True before saving. I can easily do this in the ModelForm for each item but the code is identical in each case and breaks DRY. How can I add it to each form without writing the code for it twice? Is it possible to attach the validator to the widget?

I was looking at default_validators but I don’t know what other validators are used for ForeignKey fields and how to actually declare the validator.

  • 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-29T21:54:07+00:00Added an answer on May 29, 2026 at 9:54 pm

    Validation lives on fields, not widgets. If you need the same custom validation in a bunch of forms, define a custom field, add a clean method to that field containing that validation code, then define the field’s widget attribute to be your custom widget. Then you can simply reference that field in any of your forms.

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

Sidebar

Related Questions

I have the following models class Person(models.Model): name = models.CharField(max_length=100) class Employee(Person): job =
I have the following models: class City(models.Model): name = models.CharField(max_length=100) class Pizza(models.Model): name =
I have the following (simplified) models: class School(models.Model): name = models.CharField(max_length=128) class UserProfile(models.Model): school
i have the following models setup class Player(models.Model): #slug = models.slugField(max_length=200) Player_Name = models.CharField(max_length=100)
I have following models class Artist(models.Model): name = models.CharField(max_length=200, unique=True) photo = models.CharField(max_length=250) views
I have this simplified model: class Item(models.Model): name = models.CharField(max_length=120) class ItemImage(models.Model): image =
Consider I have defined the following models: class Tag(models.Model): name = models.CharField(max_length=20) class Entry(models.Model):
I have a following model: class Car(models.Model): make = models.CharField(max_length=40) mileage_limit = models.IntegerField() mileage
Suppose I have the following models: class User(models.Model): pass class A(models.Model): user = models.ForeignKey(User)
I have the following model class (simplified) class A{ private String name; private List<B>

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.