Django API

Posted under » Django on 5 May 2021

From our previous lesson, let’s hop into the interactive Python shell and play around with the free API Django gives you. To invoke the Python shell, use this command:

$ python manage.py shell

>>> from polls.models import Choice, Question 
>>> Question.objects.all()
<QuerySet []>

>>> from django.utils import timezone
>>> q = Question(question_text="What's new?", pub_date=timezone.now())

>>> q.save()

>>> q.id
1

>>> q.question_text
"What's new?"
>>> q.pub_date
datetime.datetime(2021, 5, 26, 13, 0, 0, 775217, tzinfo=<UTC>)

>>> q.question_text = "What's up?"
>>> q.save()

# objects.all() displays all the questions in the database.
>>> Question.objects.all()
<QuerySet [<Question: Question object (1)>]>

For more info on django API.

isn’t a helpful representation of this object. Let’s fix that by editing the Question model (in the polls/models.py file) and adding a __str__() method to both Question and Choice. Note the addition of import datetime and from django.utils import timezone, to reference Python’s standard datetime module and Django’s time-zone-related utilities in django.utils.timezone. So we have this.

import datetime

from django.db import models
from django.utils import timezone

class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')

# ...
def __str__(self):
return self.question_text
# ...
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
 
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
# ...
def __str__(self):
return self.choice_text

Save these changes to the models.py and start a new Python interactive shell by running python manage.py shell again:

from polls.models import Choice, Question

# Make sure our __str__() addition worked.
>>> Question.objects.all()
<QuerySet [<Question: What's up?>]>

>>> Question.objects.filter(id=1)
<QuerySet [<Question: What's up?>]>
>>> Question.objects.filter(question_text__startswith='What')
<QuerySet [<Question: What's up?>]>

# Get the question that was published this year.
>>> from django.utils import timezone
>>> current_year = timezone.now().year
>>> Question.objects.get(pub_date__year=current_year)
<Question: What's up?>

# Request an ID that doesn't exist, this will raise an exception.

>>> Question.objects.get(id=2)
Traceback (most recent call last):

DoesNotExist: Question matching query does not exist.

# Lookup by a primary key is the most common case, so Django provides a
# shortcut for primary-key exact lookups.
# The following is identical to Question.objects.get(id=1).
>>> Question.objects.get(pk=1)
<Question: What's up?>

# Make sure our custom method worked.
>>> q = Question.objects.get(pk=1)
>>> q.was_published_recently()
True

# Give the Question a couple of Choices. The create call constructs a new
# Choice object, does the INSERT statement, adds the choice to the set
# of available choices and returns the new Choice object. Django creates
# a set to hold the "other side" of a ForeignKey relation
# (e.g. a question's choice) which can be accessed via the API.
>>> q = Question.objects.get(pk=1)

# Display any choices from the related object set -- none so far.
 >>> q.choice_set.all()
 <QuerySet []>

 # Create three choices.
 >>> q.choice_set.create(choice_text='Not much', votes=0)
 <Choice: Not much>
 >>> q.choice_set.create(choice_text='The sky', votes=0)
 <Choice: The sky>
 >>> c = q.choice_set.create(choice_text='Just hacking again', votes=0)

 # Choice objects have API access to their related Question objects.
 >>> c.question
 <Question: What's up?>

 # And vice versa: Question objects get access to Choice objects.
 >>> q.choice_set.all()
 <QuerySet [<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]>
 >>> q.choice_set.count()
 3

 # The API automatically follows relationships as far as you need.
 # Use double underscores to separate relationships.
 # This works as many levels deep as you want; there's no limit.
 # Find all Choices for any question whose pub_date is in this year
 # (reusing the 'current_year' variable we created above).
 >>> Choice.objects.filter(question__pub_date__year=current_year)
 <QuerySet [<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]>

 # Let's delete one of the choices. Use delete() for that.
 >>> c = q.choice_set.filter(choice_text__startswith='Just hacking')
 >>> c.delete()

Learn about Django filter.

Let's use the API for views.

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data