Code

Opened 6 years ago

Closed 6 years ago

Last modified 3 weeks ago

#6457 closed Uncategorized (worksforme)

Tutorial sample code: change max_length to maxlength

Reported by: musner@… Owned by: nobody
Component: *.djangoproject.com Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

While working through the tutorial, I reached the section where you modify the polls/models.py file.
I got a max_length error (running development code (rev 7028)):

>python manage.py sql polls

mysite.polls: __init__() got an unexpected keyword argument 'max_length'
1 error found.
Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_manager(settings)
  File "c:\Python25\lib\site-packages\django\core\management.py", line 1672, in
execute_manager
    execute_from_command_line(action_mapping, argv)
  File "c:\Python25\lib\site-packages\django\core\management.py", line 1620, in
execute_from_command_line
    mod_list = [models.get_app(app_label) for app_label in args[1:]]
  File "c:\Python25\lib\site-packages\django\db\models\loading.py", line 40, in
get_app
    mod = load_app(app_name)
  File "c:\Python25\lib\site-packages\django\db\models\loading.py", line 51, in
load_app
    mod = __import__(app_name, {}, {}, ['models'])
  File "C:\Documents and Settings\usner\Desktop\django\mysite\..\mysite\polls\mo
dels.py", line 3, in <module>
    class Poll(models.Model):
  File "C:\Documents and Settings\usner\Desktop\django\mysite\..\mysite\polls\mo
dels.py", line 4, in Poll
    question = models.CharField(max_length=200)
TypeError: __init__() got an unexpected keyword argument 'max_length'

After removing 'max_length=' from the code to see what happened, I received the following error:

>python manage.py sql polls

polls.poll: "question": CharFields require a "maxlength" attribute.
polls.choice: "choice": CharFields require a "maxlength" attribute.
2 errors found.

The tutorial still has the underscore in max_length in the sample code.
After removing it, it worked.

Solution:
Make the following change to the sample code (max_length -> maxlength):

from django.db import models

class Poll(models.Model):
    question = models.CharField(maxlength=200)
    pub_date = models.DateTimeField('date published')

class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice = models.CharField(maxlength=200)
    votes = models.IntegerField()

Thanks,
Mike

Attachments (0)

Change History (3)

comment:1 Changed 6 years ago by ramiro

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Are you sure the Django version you are using (i.e. the one at c:\Python25\lib\site-packages\django\) is actually SVN r7028 and not an older one? the conversion from maxlength to max_length was commited on [5803]

comment:2 Changed 6 years ago by ubernostrum

  • Resolution set to worksforme
  • Status changed from new to closed

You are not actually using revision 7028; if you were you would not be receiving this error. Remove any older versions of Django you might have had installed and try again; if you continue to experience problems post a question to the django-suers mailing lists, which provides support for these sorts of issues.

comment:3 Changed 3 weeks ago by anonymous

  • Easy pickings unset
  • Severity set to Normal
  • Type set to Uncategorized
  • UI/UX unset

none of them seems to work for me! django 1.6 and python 2.7.5

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.