Hello again. I have been set the challenge to learn some python and i thought id go for django. I don't mind python as i hear people are developing java apps and running them off a python programme which sounds interesting. Anyway:
have been following the format of the tutorials often known as the poll tutorials. However, when i have created the models and try to list the objects using ReptileSpecies.objects.all(). However, it shows the objects as being ReptileSpecies object rather than what i named them (corn snake). My model.py is as follows:
The indentations are correct even if the post shows them wrong. I read that on python 3 you should use _str_ but i'm on 2.7.5. I tried it anyway and it didn't work. What can i do to fix this?
Something i have been wondering is where exactly is ReptileSpecies.available defined as being the name of the ReptileSpecies rather than just being the feature it is. Is it just because it comes first? Or, am i missing something?
Like I say, I haven't done much with Django and I don't have it on my system right now to play with. Also I'm not sure what you are doing to display the Reptile objects. But according to the Django docs the __unicode__() method (two underscores on either side) is called:
"whenever you call unicode() on an object. Django uses unicode(obj) (or the related function, str(obj)) in a number of places. Most notably, to display an object in the Django admin site and as the value inserted into a template when it displays an object. Thus, you should always return a nice, human-readable representation of the model from the __unicode__() method."
My guess is that there is no magic going on here to identify "available" as the name of your ReptileSpecies; you have simply told the __unicode__() method to print the value of that attribute and Django expects to call __unicode__() as the default mechanism for displaying the object.
Incidentally, there are various other methods/variables in Python that start/end with two underscores and are typically used internally in Python (or frameworks like Django) like this, so be sure to count those underscores.
Meanwhile, if you try a dummy class (not based on a Django model) with a couple of member attributes and try to print them using the __unicode__() method, you get different results if you forget the parens on the method call. Calling it with parens tells Python you want to execute the method; calling it without parens tells Python you simply want a reference to the method (which is treated like any other member of the class):
The results are:Hope this helps.
PS: There are lots of good free materials for learning Python online e.g. you could look at the courses on Coursera. But if you're prepared to spend a few quid/dollars/your currency of choice on a book, I would recommend Head First Python as a great quick introduction to Python for total beginners, including a brief look at non-beginner's topics like Django and Google App engine.
Samuel Bird wrote:I have managed to fix it using:
instead. However, I cant see any difference. If you can please tell me.
EDIT: indentation isn't actually different it just appears that way.
1. Your previous code had only one underscore before/after "unicode" in the method name, but your new version has two, which is correct and is what Django expects.
2. Your code was still calling the method without the parentheses, so you were simply getting a reference to it, but not executing it. Call the method properly e.g. and see what happens.
3. You may need to explicitly print the value returned by __unicode__().