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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:09:03+00:00 2026-05-20T00:09:03+00:00

There is a Django field type for storing values of MyType . from django.core.exceptions

  • 0

There is a Django field type for storing values of MyType.

from django.core.exceptions import ValidationError
from django.db import models

class MyTypeField(models.Field):
    __metaclass__ = models.SubfieldBase

    def db_type(self, connection):
        return "text"

    def to_python(self, value):
        if isinstance(value, basestring):
            try:
                value = MyType.deserialize(value)
            except ParseError, e:
                raise ValidationError("Invalid format: "+str(e))
        assert isinstance(value, MyType)
        return value

    def get_prep_value(self, value):
        if not isinstance(value, MyType):
            raise ValidationError("Not MyType")
        return value.serialize()

I am trying to use fields of this type on an admin page of a model. Everything works nice if a user enters a valid value in the field. But if the entered value is invalid, the ValidationError is not caught (you get error 500, or a stack trace if debug is enabled)

Instead I want to see the form with a message “Invalid format” near the field (just like if you enter an invalid date or number). How to modify the field class to get validation errors in a correct place.

  • 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-20T00:09:03+00:00Added an answer on May 20, 2026 at 12:09 am

    Quoting from http://docs.djangoproject.com/en/1.2/howto/custom-model-fields/#modelforms-and-custom-fields

    If you use SubfieldBase, to_python()
    will be called every time an instance
    of the field is assigned a value. This
    means that whenever a value may be
    assigned to the field, you need to
    ensure that it will be of the correct
    datatype, or that you handle any
    exceptions.

    So a ValidationError will never be caught at this point. to_python is also called when populating a new instance of your model from the database, which is outside the form validation context.

    Therefore, you must ensure that the
    form field used to represent your
    custom field performs whatever input
    validation and data cleaning is
    necessary to convert user-provided
    form input into a
    to_python()-compatible model field
    value.
    This may require writing a
    custom form field, and/or implementing
    the formfield() method on your field
    to return a form field class whose
    to_python() returns the correct
    datatype.

    So you have to move your validation into a formfield.

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

Sidebar

Related Questions

If there were field.errors in django, how to to add css class to {{
In a Django ModelForm, you can change the widget type of a field like
Is there any way to set a foreign key in django to a field
Is there a good Django app out there that can take a list of
What options are there for installing Django such that multiple users (each with an
In Django templates, is there a variable in the context (e.g. {{ BASE\_URL }}
Are there any important features in Rails or Django which do not exist in
There's no much documentation on how to deploy a Django project with Spawning and
Is there explicit support for Single Table Inheritance in Django? Last I heard, the
Is there a way to specify a Model in Django such that is ensures

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.