Write the first Django app, part three--the API to play the model layer

Source: Internet
Author: User
Tags datetime

To play the API of the model layer

Now let's go into the python shell and play around with the free APIs that we've provided for our Django. Run the Python shell using the following command:

Python manage.py Shell

Now that you've entered the shell, look at the API for these databases:

>>> from mysite.polls.models import Poll, Choice # Import the model classes we just wrote.
# Now it's not in the system. There are polls.
>>> Poll.objects.all ()
[]
# Creates a new Poll.
>>> import datetime
>>> p = Poll (question= "What ' Up?", Pub_date=datetime.datetime.now ())
# To save this object in the database. You need to explicitly call the Save () method.
>>> P.save ()
# Now it has an ID. It could be "1L" or "1",
# It all depends on the database you're using. It's not a big problem;
# It's just a number that returns a Python long integer object.
>>> p.id
1
# accesses the database columns through the Python property values.
>>> p.question
"What ' s up?"
>>> p.pub_date
Datetime.datetime (2007, 7, each)
# modifies the value of the database through the Python property value and calls the Save () method.
>>> p.pub_date = Datetime.datetime (2007, 4, 1, 0, 0)
>>> p.save ()
# Objects.all () displays all p in the database Olls.
>>> Poll.objects.all ()
[<poll:poll Object>]

Wait. <poll:poll Object> can't help us describe this object at all. Let's edit the polls model to fix it (in the polls/models.py file) and add a __unicode__ () method to each poll and choice method.

class Poll(models.Model):
   #
   def __unicode__(self):
     return self.question
class Choice(models.Model):
   #
   def __unicode__(self):
     return self.choice

If __unicode__ () does not appear to be executing

If you add the __unicode__ () method to your models and do not see any changes in their description, you are probably using an older version of Django. (This tutorial is written for Django's latest development edition.) The previous version uses the __str__ () method.

Adding the __unicode__ () method to your models is important, not just to get smart tips in the Hint window, but also because the description of the object is used in the Django auto-generated admin.

Why is __unicode__ () rather than django.db.models.model.__str__ ()?

If you are familiar with Python, you may be used to adding a django.db.models.model.__str__ () method to your class instead of a __unicode__ () method. The reason we use __unicode__ here is because Django models handles Unicode by default. All data saved in your database is converted to Unicode when it is returned.

The Django models has a default method, Django.db.models.model.__str__ (), which invokes the __unicode__ () method and returns a UTF-8-formatted string. That is, Unicode (p) Returns a Unicode string, and STR (p) returns an ordinary string whose characters are encoded as UTF-8.

If you don't understand it, just remember to add the __unicode__ () method to your models. If you don't have luck, things will work out the way you want them to.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.