I created a Django application, validated it for errors and ran syncdb
without any problems. I can click into the admin section but as soon
as I click "Add" for one of my models I get the error
AttributeError at /admin/gallery/gallery/add/
'str' object has no attribute 'creation_counter'
Request Method: GET
Request URL: http://localhost/admin/gallery/gallery/add/
Exception Type: AttributeError
Exception Value: 'str' object has no attribute 'creation_counter'
Exception Location:
C:\Python25\lib\site-packages\django\db\models\fields\__init__.py in
__cmp__, line 100
This line is:
def __cmp__(self, other):
# This is needed because bisect does not take a comparison function.
return cmp(self.creation_counter, other.creation_counter)
I'm having a hard time troubleshooting this. My models look fine to me.
I've tried deleting the database and re-syncing it. I've also tried
creating and saving the object in the python interpretor and that
seems to work as well.
Does anyone have any ideas?
WB
So you're quite familiar with your models and cannot see the problem. We
are not familiar with them, but only get shown the last line of the
traceback. It seems like one party has a lot more information than the
other here. :-)
If your model is short (and preferably, try to reduce your example to a
very short case that still fails), post the model.
Also, look at the full traceback in the debug screen, go to the last
line and click on the local variables button. You'll be able to see what
"self" and "other" are. One of them *won't* be a Field class, although
it should be (since creation_counter is an attribute on the Field
class). That might give you some clues as to what is going wrong.
Somehow you are managing to get a string into somewhere that a Field
class (or subclass) is expected.
Do you have a custom save() method involved here at all? It's going to
be something like that, since it's not the field definitions themselves
(the model validated).
Sorry I can't offer an immediate solution, but the lack of information
is hurting us a bit here.
Regards,
Malcolm
--
Save the whales. Collect the whole set.
http://www.pointy-stick.com/blog/
Heh. Sorry I didn't want to just throw all my code at someone. I was
hoping it was a common problem. While I was trying to put more
information into this email, I think I solved the problem.
I had a Model called Gallery, which had a ManyToManyField pointing to
a Model called Location. In location, I had set my __str__ method to:
return " / ".join(str(v) for v in self.location if not v == "")
Thinking that it would be handy to have these display in a default
format of Country / State / City. (location in the line above is a
list containing Country,State and City variables, any of which can be
blank)
I noticed that although Locations were saving, I was getting the same
error on the save page and a listing of (None) on the locations page.
So I switched the __str__ method in Location to return self.country
and everything seems to work.
What did I mess up by overriding the __str__ method originally?
I did have a custom save method in my Gallery model too.
Thanks
There are multiple problems here. Firstly, you want to be iterating over
self.location.all(), not self.location. The self.location attribute is a
ManyRelatedManager instance (which you really don't care about at all,
trust me -- the point is, it's not something iterable). You use
self.location as a queryset, essentially, so you can filter it, etc.
Secondly, 'v' will then be a Location instance. So comparing it to "" is
not going to do anything sensible. Possibly you want to be testing if
str(v) != "" instead (comparing apples to apples, rather than elephants
to apples).
I'm still not entirely sure why this problem led to the error you
reported, but it's not impossible to imagine. I would have expected you
to get some kind of error like "ManyRelatedManager is not iterable",
though. *shrug*
Regards,
Malcolm
--
No one is listening until you make a mistake.
http://www.pointy-stick.com/blog/
WB