We will need this reference, where you could find all possible kind of fields we could use to create our models.
All models in django extends from models.Model:
from django.db import models
We will start defining a class model as:
Class New(models.Model):
What we need to do next? define all fields.
By default django models provide a default key if you don't define one explicitly.
Before start defining our fields, our application needs to use internationalization so we will use:
from django.utils.translation import ugettext_lazy as _
this means that we will use _('text to translate') for every text we want to have a translation, so we will use it for the labels of our fields.
title = models.CharField(_('title'), max_length=45)
The CharField needs to have a max_length defined it.
date_published = models.DateTimeField(_('date published'))
content= models.TextField(_('content'))
mp3file = models.FileField(upload_to="mettings/low/", blank=True, null=True, help_text=_("This should be a low quality version (preferably 18kbps)"))
largemp3file = models.FileField(upload_to="mettings/high/",blank=True, null=True, help_text=_("Optional. Preferably a 64kps version."))
By default all fields are required, if you want to change this we will use blank=True and null=True.
Now we have our first model. Now we have to sync with the database:
./manage.py syncdb
*Before you do syncdb you have to add your application to the settings.py.
There is a funtion that it is important to define inside our model:
def __unicode__(self):
return self.title
and also the inner class Meta:
class Meta:
ordering = ('date_published',)
verbose_name = _('metting')
verbose_name_plural = _('mettings')
Verbose names are used as a labels for our News objects on our admin interface.
Ordering is the default order used in all our queries.
There is another usefull method called get_abosulte_url, this method return the url for each object in our model, it is used in addition to generic views, so we will discuss about this method in later.
There is also the alternative to override the save method, for example if you have to fields like:
created = models.DateField(default=datetime.datetime.now)
last_updated=models.DateField(default=datetime.datetime.now)
the date created it is saved once time, however date updated needs to be updated every time the user make a change in our model. Both of this fields are not managed by the user, the systems do all the work, so we nned to override the default save method as:
def save(self):
if self.pk:
self.last_updated = datetime.now()
if not self.created:
self.created = datetime.now()
super(MyModel, self).save()
Code for Begginners
lunes, 6 de octubre de 2008
Suscribirse a:
Entradas (Atom)
Etiquetas
- prototype (3)
- DJango (2)
- PeriodicalExecuter (1)
- Ubuntu 8.04 (1)
- git (1)
- github (1)
- models (1)
- open source tools (1)
- ubiquity (1)
Archivo del Blog
-
▼
2008
(7)
- ► 09/14 - 09/21 (1)
- ► 08/31 - 09/07 (1)
- ► 08/24 - 08/31 (3)
- ► 07/27 - 08/03 (1)
-
►
2007
(3)
- ► 12/09 - 12/16 (1)
- ► 12/02 - 12/09 (2)
