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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:33:23+00:00 2026-06-13T16:33:23+00:00

I have subclassed a text-field form field in Django to create my own custom

  • 0

I have subclassed a text-field form field in Django to create my own custom widget for a field. I was wondering if it’s possible to check if all other fields of the form are valid (I want its server side behavior to vary based on the validation of other fields)

See comment

Something like:

class CustomField(TextInput):
  def __init__(self, *args, **kwargs):
    ...
    super(CustomField, self).__init__(*args, **kwargs)

  input_type = 'hidden'

  def value_from_datadict(self, data, files, name):
    aws_file_key = data.get(name, None)
    _media_bucket = boto.connect_s3(settings.AWS_ACCESS_KEY_ID,
                                       settings.AWS_SECRET_ACCESS_KEY)\
                            .lookup(settings.AWS_MEDIA_STORAGE_BUCKET_NAME)

    try:
      key = _media_bucket.get_key(aws_file_key)
    except:
      print 'Failed to get key.'
      key = None

    if key and aws_file_key:
      fh = tempfile.TemporaryFile()
      key.get_contents_to_file(fh)
      fh.seek(0)
      files = SimpleUploadedFile(key.name, fh.read())
      ### IF FORM IS VALID DELETE KEY, OTHERWISE, KEEP IT.
      if code_to_check_if_valid:
        _media_bucket.delete_key(key)
      fh.close()
      return files
      ...... etc.
  • 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-13T16:33:25+00:00Added an answer on June 13, 2026 at 4:33 pm

    If you want to validate a certain field depending on the values of other fields, you need to to it at the form level and overwrite the field’s clean method. Here’s the docs on the subject – they are very good.

    class CustomForm(forms.Form):
        custom_field = CustomField()
    
        def clean(self):
            cleaned_data = super(CustomForm, self).clean()
            custom_field = cleaned_data.get("custom_field")
            ...
    

    If you look at the flow of how forms are validated, you will see that the clean method is run if all the other fields validate independently, so at this stage, the form can be considered valid:

    These methods are run in the order given above, one field at a time. That is, for each field in the form (in the order they are declared in the form definition), the Field.clean() method (or its override) is run, then clean_<fieldname>(). Finally, once those two methods are run for every field, the Form.clean() method, or its override, is executed.

    The final clean method is actually run regardless of if there’s an error so you have to iterate through the cleaned_data to make sure there are no errors

    The clean() method for the Form class or subclass is always run. If that method raises a ValidationError, cleaned_data will be an empty dictionary.

    The previous paragraph means that if you are overriding Form.clean(), you should iterate through self.cleaned_data.items(), possibly considering the _errors dictionary attribute on the form as well. In this way, you will already know which fields have passed their individual validation requirements.

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

Sidebar

Related Questions

I have subclassed a control in C# WinForms, and am custom drawing text in
I have a UIView subclass that I assign to a text field as follows:
I have several text fields which compromise a registration form . When the user
I have a custom UITableViewCell created in IB. All my cells will have this
I have text fields inside a custom view inside an NSOutlineView . Editing one
I have subclassed UIViewController into a new class, PageViewController (I'm writing a simple book
We have subclassed the Silverlight Application class to add some additional functionality and then
I have subclassed UIView object inside a uiscrollview which displays a pdf page. I
I have subclassed a UITableViewCell and within this class I want to get it's
I have subclassed a UIView . I am adding it as a subview of

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.