TIL #2. 2020-01-08

정은경·2020년 1월 8일
0

🎸 Play the Django 

목록 보기
21/57
(wehome_conda) marie@MarieJungui-MacBookPro:~/django_model_practice_20200107/dmp_jek » tree
.
├── __pycache__
│   └── my_settings.cpython-37.pyc
├── dmp_jek
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-37.pyc
│   │   ├── settings.cpython-37.pyc
│   │   └── urls.cpython-37.pyc
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── jek.py
├── jek1.csv
├── manage.py
├── my_settings.py
└── sample
    ├── __init__.py
    ├── __pycache__
    │   ├── __init__.cpython-37.pyc
    │   ├── admin.cpython-37.pyc
    │   └── models.cpython-37.pyc
    ├── admin.py
    ├── apps.py
    ├── migrations
    │   ├── 0001_initial.py
    │   ├── __init__.py
    │   └── __pycache__
    │       ├── 0001_initial.cpython-37.pyc
    │       └── __init__.cpython-37.pyc
    ├── models.py
    ├── tests.py
    └── views.py

7 directories, 26 files
(wehome_conda) marie@MarieJungui-MacBookPro:~/django_model_practice_20200107/dmp_jek »
(wehome_conda) marie@MarieJungui-MacBookPro:~/django_model_practice_20200107/dmp_jek » cat ./sample/models.py
from django.db import models

# Create your models here.
class Product(models.Model):
    name         = models.CharField(max_length = 300)
    image        = models.CharField(max_length = 300)
    brand_id     = models.ForeignKey('Brand', on_delete = models.SET_NULL, null=True)
    product_like = models.ManyToManyField('User', through = 'ProductLike')

    class Meta:
        db_table = 'products'


class Brand(models.Model):
    name = models.CharField(max_length = 300)
    logo = models.CharField(max_length = 300)

    class Meta:
        db_table = 'brands'


class User(models.Model):
    name = models.CharField(max_length = 300)
    age  = models.PositiveIntegerField(default=10)

    class Meta:
        db_table = 'users'


class ProductLike(models.Model):
    product_id = models.ForeignKey('Product', on_delete = models.SET_NULL, null=True)
    user_id   = models.ForeignKey('User', on_delete = models.SET_NULL, null=True)
    like       = models.BooleanField(default=False)

    class Meta:
        db_table = 'product_likes'






(wehome_conda) marie@MarieJungui-MacBookPro:~/django_model_practice_20200107/dmp_jek »
(wehome_conda) marie@MarieJungui-MacBookPro:~/django_model_practice_20200107/dmp_jek » cat jek.py
import csv
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dmp_jek.settings")
import django
django.setup()

from sample.models import *

CSV_PATH = 'jek1.csv'
with open(CSV_PATH, newline='') as csvfile:
	data_reader = csv.DictReader(csvfile)
	for row in data_reader:
		print(row)
		User.objects.create(
			name = row['name'],
                        age = row['age']
		)
(wehome_conda) marie@MarieJungui-MacBookPro:~/django_model_practice_20200107/dmp_jek »
(wehome_conda) marie@MarieJungui-MacBookPro:~/django_model_practice_20200107/dmp_jek » python jek.py
OrderedDict([('name', 'jek101'), ('age', '101')])
OrderedDict([('name', 'jek102'), ('age', '101')])
OrderedDict([('name', 'jek103'), ('age', '101')])
OrderedDict([('name', 'jek104'), ('age', '101')])
OrderedDict([('name', 'jek105'), ('age', '101')])
OrderedDict([('name', 'jek106'), ('age', '101')])
OrderedDict([('name', 'jek107'), ('age', '101')])
OrderedDict([('name', 'jek108'), ('age', '101')])
OrderedDict([('name', 'jek109'), ('age', '101')])
(wehome_conda) marie@MarieJungui-MacBookPro:~/django_model_practice_20200107/dmp_jek »
profile
#의식의흐름 #순간순간 #생각의스냅샷

0개의 댓글