Django for ISchoolers: Difference between revisions

no edit summary
imported>Aldeka
No edit summary
imported>Aldeka
No edit summary
Line 429:
 
== Databases and the ORM (or: finally something that HTML/CSS/JS couldn't do for you on its own) ==
 
Okay, being able to design your URLs however you want, without them having to correspond to your actual file structure, is pretty neat. But besides that, we haven't done anything yet that you couldn't do with just HTML, CSS, and JavaScript (or just HTML, as it happens!).
 
Time to fix that.
 
ORM = Object Relational Mapper/Mapping. Translating Python classes -- real object-oriented programming stuff -- into representations in a database on your server for you.
 
(models.py --> ORM --> DB chart)
 
== models.py ==
 
Let's make it so we can store some real live data in our application!
 
In our simple Q&A app, we’ll create two models: Questions and Answers. As per our spec from the customer:
 
* A question has:
** The text of the question
** A publication date.
* An answer has:
** The text of the answer
** A publication date.
 
Each Answer is associated with a Question and each Question has associated Answers. We will respesent these concepts with python classes derived from django.db.models.
 
Edit the qandabear/models.py file so it looks like this:
 
<code>from django.db import models
 
class Question(models.Model):
text = models.CharField(max_length=200)
pub_date = models.DateTimeField(auto_now_add=True)
 
class Answer(models.Model):
question = models.ForeignKey(Question)
answer = models.TextField()
pub_date = models.DateTimeField(auto_now_add=True)</code>
 
* Save the models.py file.
 
All models in Django code are represented by a class that subclasses django.db.models.Model. Each model has a number of class variables, each of which represents a database field in the model. (cf: [http://docs.djangoproject.com/en/dev/topics/db/models/ http://docs.djangoproject.com/en/dev/topics/db/models/])
 
Each field is represented by an instance of a Field class – e.g., CharField for character fields and DateTimeField for datetimes. This tells Django what type of data each field holds.
 
The name of each Field instance (e.g. answer or pub_date) is the field’s name, in machine-friendly format. You’ll use this value in your Python code, and your database will use it as the column name.
 
Some Field classes have required elements. CharField, for example, requires that you give it a max_length. That’s used not only in the database schema, but in validation as well.
 
Most Field classes also have optional elements that you can set. For example, setting auto_now_add to True on a datetime field means that by default, that field will be set to the date and time that the object is first created.
 
Finally, note a relationship is defined, using ForeignKey. That tells Django each Answer is related to a single Question. Django supports all the common database relationships: many-to-ones, many-to-manys and one-to-ones.
 
== Database migrations and South, part one ==
Anonymous user