Taille: 1660
Commentaire:
|
← Version 13 à la date du 2013-09-06 13:09:42 ⇥
Taille: 6026
Commentaire:
|
Texte supprimé. | Texte ajouté. |
Ligne 1: | Ligne 1: |
Chargement de l'environnement virtuel ===================================== |
## page was renamed from Projet/SemaineTech/2013/Ateliers/DjangoInspectdbSouth/support = Django : SGBD inspectDb et South = |
Ligne 4: | Ligne 4: |
([[/Projet/SemaineTech/2013/Ateliers/DjangoInspectdbSouth|Voir les prérequis de l'atelier]]) {{{ source atelier/bin/activtate }}} |
<<TableOfContents(3)>> |
Ligne 7: | Ligne 6: |
Création d'un projet Django =========================== |
Légende: Les X-( sont les liens directs vers le code source, si vous voulez voir une solution qui marche |
Ligne 10: | Ligne 8: |
Boostrap -------- |
== Chargement de l'environnement virtuel == ([[Projet/SemaineTech/2013/Ateliers/DjangoInspectdbSouth|Voir les prérequis de l'atelier]]) |
Ligne 14: | Ligne 14: |
python manage.py startproject foad | source atelier/bin/activate }}} == Création d'un projet Django == === Boostrap === {{{ django-admin.py startproject foad |
Ligne 29: | Ligne 38: |
Base de données MySQL --------------------- |
=== Base de données MySQL === |
Ligne 37: | Ligne 45: |
* {{{ python manage.py syncdb }}}(créer le superuser) | {{{ python manage.py syncdb }}}(créer le superuser) |
Ligne 39: | Ligne 49: |
Activation de l'admin --------------------- |
=== Activation de l'admin === |
Ligne 57: | Ligne 66: |
Exploiter l'existant ==================== |
== Exploiter l'existant == Création de l'application {{{ django-admin.py startapp ancien }}} === Câblage db === * dans 'foad/settings.py': * variable DATABASES, ajouter une nouvelle entrée au dictionnaire avec comme clef 'consultation-ancien' * variable INSTALLED_APPS, ajouter votre nouvelle application: 'ancien' * python manage.py inspectdb --database=consultation-ancien * python manage.py inspectdb --database=consultation-ancien > ancien/models.py * ouvrir le fichier ancien/models.py et ne garder que le modèle 'Atelier' * ajouter une méthode __unicode__() pour afficher l'intitulé quand on inspecte un objet X-( https://github.com/olarcheveque/atelier-south-inspectdb/blob/master/ancien/models.py * cabler le router de base de données (défini la politque d'accès aux bds) * X-( [[https://github.com/olarcheveque/atelier-south-inspectdb/blob/master/foad/db.py]] * X-( [[https://github.com/olarcheveque/atelier-south-inspectdb/blob/master/foad/settings.py#L30]] * python manage.py shell {{{ from ancien.models import Atelier; Atelier.objects.all() }}} === Câblage de l'ancien dans l'admin === * création d'un fichier ancien/admin.py X-( [[ https://github.com/olarcheveque/atelier-south-inspectdb/blob/7b48f15e6ab64589bc39e24fcb2e00b5d5541cdd/ancien/admin.py ]] == Nouveau système == {{{ python manage.py startapp catalogue }}} * dans 'foad/settings.py': * variable INSTALLED_APPS, ajouter votre nouvelle application: 'catalogue' === initialisation du framework de migration pour l'app === {{{ python manage.py schemamigration catalogue --initial python manage.py migrate catalogue }}} === Création du script de modification de BD selon le modèle === * création du modèle Formation dans catalogue/models.py X-( [[https://github.com/olarcheveque/atelier-south-inspectdb/blob/6d153c1c3c4329720fe70a4dc5de6df5a91d41fd/catalogue/models.py ]] {{{ python manage.py schemamigration catalogue --auto python manage.py migrate catalogue }}} * création d'un fichier catalogue/admin.py X-( [[ https://github.com/olarcheveque/atelier-south-inspectdb/blob/6d153c1c3c4329720fe70a4dc5de6df5a91d41fd/catalogue/admin.py ]] === Itération 1 (création des formations à partir de l'existant) === {{{ python manage.py datamigration catalogue recuperation_id_intitule --freeze ancien }}} X-( [[https://github.com/olarcheveque/atelier-south-inspectdb/blob/6d153c1c3c4329720fe70a4dc5de6df5a91d41fd/catalogue/migrations/0003_recuperation_id_intitule.py#L9 ]] === Itération 2 (création des universités à partir de l'existant) === * ajout du modèle Universite dans catalogue/models.py {{{ python manage.py schemamigration catalogue --auto python manage.py migrate catalogue }}} {{{ python manage.py datamigration catalogue creation_universite --freeze ancien }}} X-( [[https://github.com/olarcheveque/atelier-south-inspectdb/blob/cd81764142cce07ee2fb050e237a73f2815eb17c/catalogue/models.py]] {{{ python manage.py migrate catalogue }}} X-( [[https://github.com/olarcheveque/atelier-south-inspectdb/blob/master/catalogue/migrations/0005_creation_universite.py#L9]] === Itération 3 (création des domaines à partir de l'existant) === * ajout du modèle Universite dans catalogue/models.py X-( [[https://github.com/olarcheveque/atelier-south-inspectdb/blob/master/catalogue/models.py#L20]] {{{ python manage.py schemamigration catalogue --auto python manage.py migrate catalogue }}} {{{ python manage.py datamigration catalogue creation_domaine --freeze ancien }}} X-( [[https://github.com/olarcheveque/atelier-south-inspectdb/blob/master/catalogue/migrations/0007_creation_domaine.py#L9]] {{{ python manage.py migrate catalogue }}} == Commandes utiles == En cas, d'une migration échouée, comme les modifications ne sont pas transactionnelles, il est bon resetter l'application dans un état stable, corrélé avec les modèles. {{{ python manage.py sqlclear catalogue > del_catalogue mysql -u <user> -p --database=atelier < del_catalogue python manage.py dbshell - delete from south_migrationhistory where app_name = 'catalogue'; }}} |
Django : SGBD inspectDb et South
Sommaire
Légende: Les sont les liens directs vers le code source, si vous voulez voir une solution qui marche
Chargement de l'environnement virtuel
(Voir les prérequis de l'atelier)
source atelier/bin/activate
Création d'un projet Django
Boostrap
django-admin.py startproject foad cd foad python manage.py runserver (CTRL+C) echo "*.pyc" > .gitignore git init git add . git commit -m 'nouveau projet'
à chaque modification correcte au cours de l'atelier, commiter-les, vous pourrez plus facilement voir les modifications de fichiers, revenir en arrière, etc...
Base de données MySQL
- dans 'foad/settings.py':
- variable DATABASES, configurer le 'default' avec une bd vierge nommée 'atelier'
- variable INSTALLED_APPS, ajouter 'south'
https://github.com/olarcheveque/atelier-south-inspectdb/blob/master/foad/settings.py
python manage.py syncdb
(créer le superuser)
Activation de l'admin
- dans 'foad/settings.py':
- variable INSTALLED_APPS, décommenter 'django.contrib.admin'
https://github.com/olarcheveque/atelier-south-inspectdb/blob/master/foad/settings.py
- dans 'foad/urls.py',décommenter:
from django.contrib import admin admin.autodiscover() url(r'^admin/', include(admin.site.urls)),
https://github.com/olarcheveque/atelier-south-inspectdb/blob/master/foad/urls.py
python manage.py runserver (127.0.0.1:8000/admin)
Exploiter l'existant
Création de l'application
django-admin.py startapp ancien
Câblage db
- dans 'foad/settings.py':
- variable DATABASES, ajouter une nouvelle entrée au dictionnaire avec comme clef 'consultation-ancien'
- variable INSTALLED_APPS, ajouter votre nouvelle application: 'ancien'
- python manage.py inspectdb --database=consultation-ancien
python manage.py inspectdb --database=consultation-ancien > ancien/models.py
- ouvrir le fichier ancien/models.py et ne garder que le modèle 'Atelier'
ajouter une méthode unicode() pour afficher l'intitulé quand on inspecte un objet
https://github.com/olarcheveque/atelier-south-inspectdb/blob/master/ancien/models.py
- cabler le router de base de données (défini la politque d'accès aux bds)
- python manage.py shell
from ancien.models import Atelier; Atelier.objects.all()
Câblage de l'ancien dans l'admin
- création d'un fichier ancien/admin.py
Nouveau système
python manage.py startapp catalogue
- dans 'foad/settings.py':
- variable INSTALLED_APPS, ajouter votre nouvelle application: 'catalogue'
initialisation du framework de migration pour l'app
python manage.py schemamigration catalogue --initial python manage.py migrate catalogue
Création du script de modification de BD selon le modèle
* création du modèle Formation dans catalogue/models.py
python manage.py schemamigration catalogue --auto python manage.py migrate catalogue
* création d'un fichier catalogue/admin.py https://github.com/olarcheveque/atelier-south-inspectdb/blob/6d153c1c3c4329720fe70a4dc5de6df5a91d41fd/catalogue/admin.py
Itération 1 (création des formations à partir de l'existant)
python manage.py datamigration catalogue recuperation_id_intitule --freeze ancien
Itération 2 (création des universités à partir de l'existant)
* ajout du modèle Universite dans catalogue/models.py
python manage.py schemamigration catalogue --auto python manage.py migrate catalogue
python manage.py datamigration catalogue creation_universite --freeze ancien
python manage.py migrate catalogue
Itération 3 (création des domaines à partir de l'existant)
* ajout du modèle Universite dans catalogue/models.py https://github.com/olarcheveque/atelier-south-inspectdb/blob/master/catalogue/models.py#L20
python manage.py schemamigration catalogue --auto python manage.py migrate catalogue
python manage.py datamigration catalogue creation_domaine --freeze ancien
python manage.py migrate catalogue
Commandes utiles
En cas, d'une migration échouée, comme les modifications ne sont pas transactionnelles, il est bon resetter l'application dans un état stable, corrélé avec les modèles.
python manage.py sqlclear catalogue > del_catalogue mysql -u <user> -p --database=atelier < del_catalogue python manage.py dbshell - delete from south_migrationhistory where app_name = 'catalogue';