background preloader

Django_models

Facebook Twitter

Using South for schema and data migrations in Django. South is a schema and data migrations for Django. In an easy way you may update your database tables after you change your models. It also keeps all migrations in the codebase allowing to migrate backward if needed. In this article I'll show some standard South usage cases. Installing and configuring South The quickest way to install south is to use pipL pip install South Next we have to add south to INSTALLED_APPS in your Django project. Basic usage Without south you would use syncdb to create table of your new models. Python manage.py schemamigration APPLICATION_NAME --initial In the application folder a "migrations" subfolder will be created. Python manage.py migrate APPLICATION_NAME If we would have such simple model: from django.db import models class MyModel(models.Model): name = models.CharField(max_length=100) Then the initial migration would create the table.

Class MyModel(models.Model): name = models.CharField(max_length=100) description = models.TextField(blank=True) ? Data migrations. Models. The most important part of a model – and the only required part of a model – is the list of database fields it defines. Fields are specified by class attributes. Be careful not to choose field names that conflict with the models API like clean, save, or delete. Verbose field names Each field type, except for ForeignKey, ManyToManyField and OneToOneField, takes an optional first positional argument – a verbose name. If the verbose name isn’t given, Django will automatically create it using the field’s attribute name, converting underscores to spaces.

In this example, the verbose name is "person's first name": first_name = models.CharField("person's first name", max_length=30) In this example, the verbose name is "first name": first_name = models.CharField(max_length=30) ForeignKey, ManyToManyField and OneToOneField require the first argument to be a model class, so use the verbose_name keyword argument: The convention is not to capitalize the first letter of the verbose_name. Relationships Why? Django: newbie question on foreign key queries. Database design - designing model structure for django. Database design - Django model needs one each of two related fields: should I use oneToOneField.

Django accessing foreign key in model. Django database learn itself. Django - Which is better: Foreign Keys or Model Inheritance. How can I have two foreign keys to the same model in Django.