• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Django tutorials assistance on objects

 
Ranch Hand
Posts: 96
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 3
Python Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try Giving __unicode__() instead of just unicode().


refer djangoproject
 
Samuel Bird
Ranch Hand
Posts: 96
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is _unicode_
 
Samuel Bird
Ranch Hand
Posts: 96
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any replies?
 
Bartender
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Samuel Bird
Ranch Hand
Posts: 96
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 96
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, i see. Ill give that a try.
 
Samuel Bird
Ranch Hand
Posts: 96
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 96
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 96
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, i didn't spot that and i wont make that mistake again.
 
chris webster
Bartender
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic