"Projeto" é como Django chama a base do sistema, que não passa de um conjunto de apps.
Criado com django-admin startproject [PROJECTNAME] .
.
project/
├── manage.py
└── project
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
$ python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
October 24, 2016 - 23:04:50
Django version 1.10.2, using settings 'project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying sessions.0001_initial... OK
App é como o Django chama as pequenas partes que compõem o sistema.
Uma app pode ser entendida como uma tabela, ou um conjunto de tabelas que trabalham em conjunto ou conjunto de funções auxiliares.
django-admin startapp customers
project/
├── customers
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── db.sqlite3
├── manage.py
└── project
├── __init__.py
├── __init__.pyc
├── settings.py
├── settings.pyc
├── urls.py
├── urls.pyc
└── wsgi.py
from __future__ import unicode_literals
from django.db import models
class Customer(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
$ python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
October 25, 2016 - 21:01:40
Django version 1.10.2, using settings 'project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'customers'
]
$ python manage.py makemigrations
Migrations for 'customers':
customers/migrations/0001_initial.py:
- Create model Customer
$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, customers, sessions
Running migrations:
Applying customers.0001_initial... OK
from django.contrib import admin
from customers.models import Customer
class CustomerAdmin(admin.ModelAdmin):
pass
admin.site.register(Customer, CustomerAdmin)
from __future__ import unicode_literals
from django.db import models
from customers.models import Customer
class Order(models.Model):
customer = models.ForeignKey(Customer)
date = models.DateField(auto_now_add=True)
def __str__(self):
return '{name} - {items} item'.format(
name=self.customer.name,
items=self.item_set.count())
class Item(models.Model):
order = models.ForeignKey(Order)
item = models.CharField(max_length=30)
qtd = models.PositiveIntegerField()
price = models.DecimalField(max_digits=8, decimal_places=2)
@property
def total(self):
return self.qtd * self.price
from django.contrib import admin
from orders.models import Order
from orders.models import Item
class Itemadmin(admin.TabularInline):
model = Item
class OrderAdmin(admin.ModelAdmin):
inlines = [Itemadmin]
admin.site.register(Order, OrderAdmin)