1. Nick Efford
  2. ypy-django

Commits

Nick Efford  committed 2641b6e

Added slides on data handling.

  • Participants
  • Parent commits 4b92979
  • Branches default

Comments (0)

Files changed (1)

File slides/slides.tex

View file
  • Ignore whitespace
   \end{visibleenv}
 \end{frame}
 
-\note{
+\note[fragile]{
 Third-party modules for DB2 \& Firebird, also MSSQL Server?
+
+Make these changes now in |settings.py| \ldots
 }
 
 
 \subsection{Data Handling}
 
 
-\begin{frame}{Data Models}
+\begin{frame}[fragile]{The Data Model}
+  \begin{itemize}
+    \item A way of describing database layout in Python code (a class)
+    \item Normally represents a single database table
+    \item Has \textbf{fields} that map onto columns of that table
+    \item Many built-in field types (also classes)
+      \begin{itemize2}
+        \item |CharField|, |TextField|
+        \item |IntegerField|, |FloatField|, |DecimalField|
+        \item |DateField|, |DateTimeField|, |TimeField|
+        \item |EmailField|, |URLField|
+        \item |ForeignKey| \ldots
+      \end{itemize2}
+  \end{itemize}
 \end{frame}
 
+\note[fragile]{
+Since it is a class it can have methods to represent business logic \ldots
+
+Difference between |CharField| and |TextField| is one of size; latter is
+intended for larger amounts of text (multiple lines), and will have a
+multi-line representation in the admin interface and forms.
+}
+
 \begin{frame}[fragile]{Example}
   \begin{Verbatim}[gobble=2,frame=single,fontsize=\relsize{-1}]
   from django.db import models
   \end{Verbatim}
 \end{frame}
 
+\note[fragile]{
+Note ability to constraint field size.
+
+Note also the use of an \textbf{inner class} to provide metadata.
+
+String representations of models are provide via |__unicode__| special
+method.
+
+Edit |models.py| in the |club| app now and add this code \ldots
+}
+
 \begin{frame}[fragile]{Creating The Database}
   \begin{enumerate}
     \item Optionally check table creation SQL:
   \end{enumerate}
 \end{frame}
 
-\begin{frame}{Creating \& Saving Objects}
+\begin{frame}[fragile]{Creating \& Saving Objects}
+  Invoke constructor and call |save| method:
+  \begin{Verbatim}[gobble=4,frame=single,fontsize=\relsize{-1}]
+    club = Club(name='Arsenal', year_established=1886,
+                ground='Emirates Stadium')
+
+    club.save()
+  \end{Verbatim}
+  \vspace{0.5\baselineskip}
+  \ldots or call |create| method of |Club| model manager:
+  \begin{Verbatim}[gobble=4,frame=single,fontsize=\relsize{-1}]
+    Club.objects.create(name='Arsenal', year_established=1886,
+                        ground='Emirates Stadium')
+  \end{Verbatim}
 \end{frame}
 
-\begin{frame}{Retrieving Objects}
+\begin{frame}[fragile]{Retrieving Objects}
+  Retrieve a single object with |get|:
+  \begin{Verbatim}[gobble=4,frame=single,fontsize=\relsize{-1}]
+    club = Club.objects.get(name='Liverpool')
+  \end{Verbatim}
+  \vspace{0.5\baselineskip}
+  Retrieve \textbf{querysets} with |all|, |filter|, etc:
+  \begin{Verbatim}[gobble=4,frame=single,fontsize=\relsize{-1}]
+    for club in Club.objects.all():
+        print club
+  \end{Verbatim}
+  \begin{Verbatim}[gobble=4,frame=single,fontsize=\relsize{-1}]
+    for club in Club.objects.filter(year_established=1878):
+        print club
+  \end{Verbatim}
 \end{frame}
 
 
 \subsection{The Admin Interface}
 
 
+\begin{frame}[fragile]{Registering Models}
+  In |admin.py| in an app:
+  \begin{Verbatim}[gobble=4,frame=single,fontsize=\relsize{-1}]
+    from django.contrib import admin
+    from football.club.models import Club
+
+    admin.site.register(Club)
+  \end{Verbatim}
+  \vspace{0.5\baselineskip}
+  In top-level |urls.py|:
+  \begin{Verbatim}[gobble=4,frame=single,fontsize=\relsize{-1}]
+    from django.contrib import admin
+    admin.autodiscover()
+
+    urlpatterns = patterns('',
+      ...
+      (r'^admin/', include(admin.site.urls))
+    )
+  \end{Verbatim}
+\end{frame}
+
 \begin{frame}{The Admin Interface}
   \begin{center}
     \setlength{\fboxsep}{0pt}