File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Jython/Python and the fly likes Django tutorials assistance on objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Jython/Python
Bookmark "Django tutorials assistance on objects" Watch "Django tutorials assistance on objects" New topic
Author

Django tutorials assistance on objects

Samuel Bird
Ranch Hand

Joined: Jul 19, 2013
Posts: 96

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?

I appreciate all responses,

sam.
chandan uppuluri
Greenhorn

Joined: Oct 17, 2013
Posts: 3

Try Giving __unicode__() instead of just unicode().


refer djangoproject
Samuel Bird
Ranch Hand

Joined: Jul 19, 2013
Posts: 96

it is _unicode_
Samuel Bird
Ranch Hand

Joined: Jul 19, 2013
Posts: 96

Any replies?
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1771
    
  14

Hi Samuel

Don't know much about Django, so the only thing that occurs to me is that when I forget to call a method/function with the parens, Python returns an object reference to the function itself.

Is it possible you're doing something similar?


No more Blub for me, thank you, Vicar.
Samuel Bird
Ranch Hand

Joined: Jul 19, 2013
Posts: 96

Could you give me an example of what you mean?

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?

Any thoughts?
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1771
    
  14

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
Ranch Hand

Joined: Jul 19, 2013
Posts: 96

Ah, i see. Ill give that a try.
Samuel Bird
Ranch Hand

Joined: Jul 19, 2013
Posts: 96

I tried writing this:



However, i'm not sure how i can get this to work. If i find out ill post how but, in the meantime, id appreciate advice.
Samuel Bird
Ranch Hand

Joined: Jul 19, 2013
Posts: 96

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.
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1771
    
  14

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__().
Samuel Bird
Ranch Hand

Joined: Jul 19, 2013
Posts: 96

Thank you, i didn't spot that and i wont make that mistake again.
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1771
    
  14

Samuel Bird wrote:Thank you, i didn't spot that and i wont make that mistake again.

Yes you will - I do regularly!
Glad we got there in the end.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Django tutorials assistance on objects