#08-Django CRUD

Tahapan

  1. Buat Project myapp
  2. Buat App orders
  3. Buat Model order
  4. Buat View(Controller)
  5. Buat Url(Route)

Masuk ke PowerShell/cmd/shellscript, masuk ke folder project myapp

Windows Powershell

Buat App orders

python manage.py startapp orders
├── myapp
├── orders
│   └── migrations
│   ├── _init_.py
│   └── admin.py
│   └── apps.py
│   └── models.py
│   └── tests.py
│   └── urls.py
│   └── views.py

Buat Model

from django.db import models

class Customer(models.Model):
	fullname = models.CharField(max_length=200)
	created_date = models.DateTimeField('created at')
	updated_at = models.DateTimeField('updated at')
	def __str__(self):
		return self.customer_name
	
class Order(models.Model):
    customer_id = models.ForeignKey(Customer, on_delete=models.CASCADE)
    price = models.IntegerField(max_length=200)
    created_date = models.DateTimeField('created at')
    updated_at = models.DateTimeField('updated at')
    def __str__(self):
        return self.price

Aktivasi Model

#mysite/settings.py
INSTALLED_APPS = [
    'orders.apps.OrdersConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>