Opened 5 years ago

Closed 5 years ago

#27720 closed Cleanup/optimization (needsinfo)

Confusing error message when creating a model with unspecified attributes that have no defaults

Reported by: sanaani Owned by: nobody
Component: Database layer (models, ORM) Version: 1.10
Severity: Normal Keywords: model
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

Using the example project at https://docs.djangoproject.com/en/1.10/intro/tutorial02/.

The example project says to instantiate a model in this way:
q = Question(question_text="What's new?", pub_date=timezone.now())

However, if you do something silly like not assigning attributes:
q = Question("What's new?", timezone.now())

The model will still instantiate. What's super confusing is that if you forget to type in the attributes, then apply
q.save()

You get the bizarre error ValueError: invalid literal for int() with base 10: 'Question'

If a model object is created without an assigned attribute and the missing attribute has no default specified, then some type of warning should appear. I wasted about 3 hours today figuring out this dumb mistake; the error message isn't related to the initial error.

Change History (1)

comment:1 Changed 5 years ago by Tim Graham

Description: modified (diff)
Easy pickings: unset
Resolution: needsinfo
Status: newclosed
Type: UncategorizedCleanup/optimization

I'm not sure if a backwards-compatible solution is feasible. Objects created with the ORM assume the developer is doing things properly. If user input is incoming, then you can call Model.clean() to get a better error message. If someone has an idea of an improvement, feel free to reopen.

Note: See TracTickets for help on using tickets.
Back to Top