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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:04:58+00:00 2026-05-13T22:04:58+00:00

We implemented a LowerCaseCharField. We would be happy to hear better implementation suggestions. from

  • 0

We implemented a LowerCaseCharField. We would be happy to hear better implementation suggestions.

from django.db.models.fields import CharField

class LowerCaseCharField(CharField):
    """
    Defines a charfield which automatically converts all inputs to
    lowercase and saves.
    """

    def pre_save(self, model_instance, add):
        """
        Converts the string to lowercase before saving.
        """
        current_value = getattr(model_instance, self.attname)
        setattr(model_instance, self.attname, current_value.lower())
        return getattr(model_instance, self.attname)

In fact we love to have is:

> modelinstance.field_name="TEST"
> print modelinstance.field_name
'test'

current implementation only converts to lowercase when it is saved.

  • 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-13T22:04:58+00:00Added an answer on May 13, 2026 at 10:04 pm

    You may wish to override to_python, which will allow you to compare non-lowercase strings when doing database lookups. The actual method is get_prep_value, but as that calls to_python for CharField, it’s more convenient to override that:

    def to_python(self, value):
        value = super(LowerCaseCharField, self).to_python(value)
        if isinstance(value, basestring):
            return value.lower()
        return value
    

    Now you can do queries like:

    MyModel.objects.filter(lccf="MiXeD")
    

    Edit:

    Rereading your question, it looks like you want the lowering to take effect immediately. To do this, you’ll need to create a descriptor (a new-style python object with __get__ and __set__ methods, see the python docs and the django code for related models) and override contribute_to_class in the field to set the model’s field to your descriptor.

    Here is a full example off the top of my head, which should be reusable for all fields that want to modify the value on setting.

    class ModifyingFieldDescriptor(object):
        """ Modifies a field when set using the field's (overriden) .to_python() method. """
        def __init__(self, field):  
            self.field = field  
        def __get__(self, instance, owner=None):
            if instance is None:
                raise AttributeError('Can only be accessed via an instance.')  
            return instance.__dict__[self.field.name]
        def __set__(self, instance, value):
            instance.__dict__[self.field.name] = self.field.to_python(value)
    
    class LowerCaseCharField(CharField):
        def to_python(self, value):
            value = super(LowerCaseCharField, self).to_python(value)
            if isinstance(value, basestring):
                return value.lower()
            return value
        def contribute_to_class(self, cls, name):
            super(LowerCaseCharField, self).contribute_to_class(cls, name)
            setattr(cls, self.name, ModifyingFieldDescriptor(self))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Implemented the Sink Class - to receive event notifications from COM Server Event Interface
I implemented class that serves as TcpCLient server. Looks like this: { [Export] public
I implemented a tree with an outputResource as content (see Downloading file from IceFaces
I implemented Solr SpellCheck Component based on the document from http://wiki.apache.org/solr/SpellCheckComponent , it works
I implemented a singleton class as a private class contained within my page. In
I implemented the IDropTarget interface and the drag & drop (file from explorer) works
I implemented the Singleton design pattern in my code. Suppose it is: class Singleton
I implemented an ExtensionMethod which basically works as ForEach-Loop, my Implementation looks like this:
I implemented/copied the wu line algorithm from pseudo-code on wiki-pedia and other places. When
I implemented the code from the question Ball to Ball Collision - Detection and

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.