Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: unbound method Date() must be called with DateTime instance as first argument (got int instance instead)

Okay, I'm working on a library PLOW website (which is a form website for the state of Iowa's library systems). I can't figure out how to format the date overrides field so that when someone tries to submit a form, it will not give them the message "Date must be a future date". There may be something else blocking the form from going through instead, but I think it's this.

(Please note that I'm not a programmer and know little about HTML, I've just had a couple of quick tutorials.)

This is the error message I'm getting: TypeError: unbound method Date() must be called with DateTime instance as first argument (got int instance instead)

This is what I'm trying to do: When I go under "Overrides" there are three bold headings to choose from: "Default Expression", "Custom Validator", and Enabling Expression". All of them are blank except for "Custom Validator" which says this: python: test(value < DateTime(),False,'Date must be a future date'). This brings up the error message "Date must be a future date" for any date that it is given, regardless of how far in the future it is.

I was confused as to why this wasn't working, and after doing some Google searches I came up with this: python: test(value < DateTime.Date(2013, 10, 23),False,'Date must be a future date') and that is where I'm getting this error message from:

TypeError: unbound method Date() must be called with DateTime instance as first argument (got int instance instead)

Help? What I want is for all future dates to work, was it fine before and the site just has a different problem that I can't identify?

Note: there are spaces around the "less than" symbol which are not originally there, but otherwise your site thought I was telling it a code.

like image 654
AssistLibrarianLCPL Avatar asked Jun 10 '26 13:06

AssistLibrarianLCPL


1 Answers

This error:

TypeError: unbound method Date() must be called with DateTime instance as first argument (got int instance instead)

… is what you get when you try to call an normal instance method on the class itself, instead of an instance of the class.* (There are other cases where it can come up, but this is by far the most common.) And your code is indeed calling a method on the class itself:

DateTime.Date(2013, 10, 23)

I don't know what this DateTime class is, or its Date method, but if they're anything like the standard library's datetime.datetime class and its date method, you're doing it wrong.

The datetime module can sometimes cause confusion, because the module and one of the classes in it have the same name. And, to add to the confusion, some of the methods of that class have the same name as other classes or functions in the module. So, this is a perfectly valid way to construct a date object:

import datetime
d = datetime.date(2013, 10, 23)

… and this constructs the same object:

import datetime
d = datetime.datetime(2013, 10, 23).date()

… and so does this:

from datetime import datetime
d = datetime(2013, 10, 23).date()

… but this is an attempt to call the date instance method on the datetime class, which is nonsense and will raise the same kind of exception you saw.

from datetime import datetime
d = datetime.date(2013, 10, 23)

* If you want to know why you get this error and exactly what this means, you have to understand about bound vs. unbound methods, how to call bound methods, etc. This blog post attempts to explain it. But you probably don't need to know all of that to fix your problem.

like image 140
abarnert Avatar answered Jun 13 '26 04:06

abarnert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!