Supposons le cas suivant :
J’ai des pages et des catégories. Une page peut avoir une catégorie.
Nous avons donc les modèles suivants :
class Category(models.Model):
name = models.CharField(max_length=200)
slug = models.SlugField(max_length=200,unique=True)
class Page(models.Model):
title = models.CharField(_('title'), max_length=200)
content = models.TextField(_('content'))
category = models.ForeignKey(Category, related_name = 'cat')
Maintenant supposons que à cela, nous désirions ajouter, pour une catégorie, une page principale.
Dans mon cas, cela me permet, si j’ai une page principale, d’afficher le contenu de celle-ci lorsque l’on charge la page de la catégorie.
Mais après, à vous de voir ce que vous désirez faire
Dans notre modèle de catégorie, nous allons donc ajouter une relation vers le modèle Page. Logique !
Cependant, page = models.ForeignKey(Page,blank=True,null=True)
Ne fonctionnera pas. En effet la classe Page n’est pas encore définie lorsque vous définissez la classe Category.
La solution est cependant très simple. Il suffit de ne pas faire un appel direct à la classe Page. Mais de mentionner son nom dans une chaine de caractères :
page = models.ForeignKey('Page', blank=True,null=True)
Ainsi vous ne faites pas d’appel à la classe Page lors de l’instanciation de Category. Et lorsque Django initialize vos modèles et qu’il cherche la classe Page, celle-ci a déjà été déclarée.
Vous avez défini votre relation entre modèles à deux sens.

This is the first article of a serie of comparisons between PHP and Ruby.
Count the number of words in a text is much more complex than what you should think first.
Because a space doesn’t mean a new word everytime. There can be interrogation points with spaces before and after for example. And an interrogation point isn’t a word.
Here, we’ll take the following definition for « word » :
Two or more characters with at least one alphabetical character..
In other words : at leat two characters with at least one letter of the alphabet.
Let’s count our words in Ruby :
class String
def nb_words
self.split(/[a-z]+/).size
end
end
To count the number of words, we only have to do :
puts "my string ! Great no ?".nb_words
Which will then give us 4 and not 6. « ! » and « ? » aren’t words.
Let’s now do the same thing in PHP.
Even though I don’t think it’s a good idea as there’s the function str_word_count.
function nb_words($string) {
return count(preg_split('[a-z]+', $string));
}
To be used like the following :
echo nb_words('my string ! Great no ?');
It’s very easy, in Ruby, to count the number of days between two dates.
date1 = 1.month.ago
date2 = Date.today
puts date2-date1
However, you might need to calculate, not all the days between dates. But all the week days. For example if you’re working on an extranet and you want to count all the working days
Vamos ! Let’s put in your code (wherever you want. But put it somewhere logical please) :
class Date
def weekdays_until(date)
return 0 if date <= self
(self..date).select{|day| day.is_weekday?}.size
end
def is_weekday?
self.wday != 0 && self.wday != 6
end
end
And to use is :
1.month.ago.weekdays_until(Date.today)
Cool no ?
What are we doint ?
We overload the object Date by adding to it two new methods :
- is_weekday? which gives us true if the day is from monday to friday.
- weekdays_until which gives us the number of days from monday to friday only.
If course it doesn’t count the holidays
Note : the tricks used here, like 1.month.ago are only available if you’re in a Rails environment. Otherwise, you have to calculate this by hand.