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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:01:45+00:00 2026-05-14T02:01:45+00:00

I wold like to have in my model a CharField with fixed length. In

  • 0

I wold like to have in my model a CharField with fixed length. In other words I want that only a specified length is valid.

I tried to do something like

volumenumber = models.CharField('Volume Number', max_length=4, min_length=4)

but it gives me an error (it seems that I can use both max_length and min_length at the same time).

Is there another quick way?

My model is this:

class Volume(models.Model):
    vid = models.AutoField(primary_key=True)
    jid = models.ForeignKey(Journals, db_column='jid', null=True, verbose_name = "Journal")
    volumenumber = models.CharField('Volume Number')
    date_publication = models.CharField('Date of Publication', max_length=6, blank=True)
    class Meta:
        db_table = u'volume'
        verbose_name = "Volume"
        ordering = ['jid', 'volumenumber']
        unique_together = ('jid', 'volumenumber')
    def __unicode__(self):
        return (str(self.jid) + ' - ' + str(self.volumenumber))

What I want is that the volumenumber must be exactly 4 characters.

I.E.
if someone insert ‘4b’ django gives an error because it expects a string of 4 characters.

So I tried with

volumenumber = models.CharField('Volume Number', max_length=4, min_length=4)

but it gives me this error:

Validating models...
Unhandled exception in thread started by <function inner_run at 0x70feb0>
Traceback (most recent call last):
  File "/Library/Python/2.5/site-packages/django/core/management/commands/runserver.py", line 48, in inner_run
    self.validate(display_num_errors=True)
  File "/Library/Python/2.5/site-packages/django/core/management/base.py", line 249, in validate
    num_errors = get_validation_errors(s, app)
  File "/Library/Python/2.5/site-packages/django/core/management/validation.py", line 28, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "/Library/Python/2.5/site-packages/django/db/models/loading.py", line 131, in get_app_errors
    self._populate()
  File "/Library/Python/2.5/site-packages/django/db/models/loading.py", line 58, in _populate
    self.load_app(app_name, True)
  File "/Library/Python/2.5/site-packages/django/db/models/loading.py", line 74, in load_app
    models = import_module('.models', app_name)
  File "/Library/Python/2.5/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/Users/Giovanni/src/djangoTestSite/../djangoTestSite/journaldb/models.py", line 120, in <module>
    class Volume(models.Model):
  File "/Users/Giovanni/src/djangoTestSite/../djangoTestSite/journaldb/models.py", line 123, in Volume
    volumenumber = models.CharField('Volume Number', max_length=4, min_length=4)
TypeError: __init__() got an unexpected keyword argument 'min_length'

That obviously doesn’t appear if I use only "max_length" OR "min_length".

I read the documentation on the django web site and it seems that I’m right (I cannot use both together) so I’m asking if there is another way to solve the problem.

  • 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-14T02:01:45+00:00Added an answer on May 14, 2026 at 2:01 am

    CharField database model field instances only have a max_length parameter, as indicated in the docs. This is probably because there is only a max character length contraint equivalent in SQL.

    Form Field CharField objects, on the other hand, do have a min_length parameter. So you’d have to write a custom ModelForm for this specific model and override the default admin model form with the custom one.

    Something like that:

    # admin.py
    
    from django import forms
    
    ...
    
    class VolumeForm(forms.ModelForm):
        volumenumber = forms.CharField(max_length=4, min_length=4)
    
        class Meta:
            model = Volume
    
    
    class VolumeAdmin(admin.ModelAdmin):
        form = VolumeForm
    
    ...
    
    admin.site.register(Volume, VolumeAdmin)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For me usable means that: it's being used in real-wold it has tools support.
I have this question: suppose that the size of char is one byte and
I wold like to create a progressbar for my application, im running i got
I have tables like Customer , Purchase etc which sometimes have associated documents with
I wold like to create this kind of URL for my users when they
I’m writing an CI application that has to have authentication and preferably different roles
I have create A communicator using the construct MPI_Comm_split() , And then I want
I want to upgrade my database version to 4. I have changed version code
I wold like to build a simple 3D game using Kinect for windows but
I wold like to make a really small CMS based on codeigniter. And i

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.