Django: ProgrammingError: Can’t Adapt

I find I get an error like this far too often, but not often enough to remember why I got the problem the previous time. For my own sanity, I’m blogging about it for reference. The error itself is not very descriptive:

Exception Type: ProgrammingError
Exception Value: can’t adapt

Unless there is something I’m forgetting, it seems 100% of the time it has been a simple error. I use the get_or_create function to retrieve a model, but I don’t account for the “created” variable. So then I try to use the tuple as a model object somewhere and it throws this error.

For example, here is the wrong way:

person = Person.objects.get_or_create(first_name="Dustin", last_name="Davis")
print person.first_name

And now for the correction:

person, created = Person.objects.get_or_create(first_name="Dustin", last_name="Davis")
print person.first_name

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

January 28, 2010 · Dustin · 12 Comments
Tags:  · Posted in: Programming & Internet

  • Eric

    In case anyone finds your post via search, “Can’t Adapt” exceptions are also very common when using PostgreSQL/pyscopg. For instance, if you have a table column of text, and you try to run a SQL statement that excepts it to be numeric (such as an average or sum aggregate) you’ll get Can’t Adapt exceptions.

    The easiest way to determine ‘why’ is to consult your PostgreSQL logs.

  • http://gu-at.com Brent

    Out of curiosity, have any idea why it throws “ProgrammingError” instead of “AttributeError”? The latter would at least give a clue of what’s wrong.

  • Jiri Barton

    For me, this has always meant I’m trying to save/create data which the database will not accept, such as None (NULL) into a non-NULL column.

  • http://trespams.com aaloy

    We had this problem on a server running mod_python with two different applications. With just one application there was no problem.

    We moved to a wsgi configuration and we haven’t received any “can’t adapt” problems.

  • http://www.bestonlinetattoodesigns.com tattooboy

    That’s the only way to go when you work on computers, I do research from time to time and my computer tends to get a lot of viruses when I go to sites not much travelled. To keep track of which web-pages have these viruses you need to write them down to avoid returning to them. In light of all this it’s also a good idea to keep your work saved as well including your research links.

  • http://bestbuylaptop.redtor.com/ suphancyber

    Wir hatten dieses Problem auf einem Server mit mod_python mit zwei verschiedenen Anwendungen. Mit nur einer Anwendung hatte keine Probleme

  • http://www.billigelaan.com/ Lån Penge

    Hi I think your error is due to some syntax error. I do not know much about computer programming but I think syntax error cam be. One of my friend has similar kind of error and he checked his syntax

  • http://heritageinstallationsltd.co.uk/double-glazing/addiscombe Double Glazing Addiscombe

    Synax you say, that would make sense LOL

  • http://www.retailmakeupstore.com Retail Makeup Store

    lol, I’ve had this issue a thousand times over where I’m programming and have an error I’ve seen a thousand times but it still takes me a short while to figure it out… good thinking on blogging about it

  • Harel Malka

    Its when you attempt to insert a dict/list/etc value into a varchar or int fields… It won’t str() the value for you so the db won’t be able to ‘adapt’ to the value you attempt to put in.

  • podja

    It is exactly what the blog says it is. A tuple is returned and you are trying to access an option not available. Adding ‘created’ (which basically returns true or false) before ‘person’ as done, solves the problem. Then you can access all the stuff from person.
    PS – syntax error was classic. Thank you.

  • Erik Näslund

    Just adding another possible cause for the “can’t adapt” error. I myself was running a way old psycopg2 (2.0.1) which caused this problem. Upgraded to 2.3.1 and the error went away.