python manage.py startapp 앱이름
from django.contrib import admin
from .models import Inbound
admin.site.register(Inbound)
python manage.py makemigrations
python manage.py migrate
<form method="POST" action="/inbound-history/">
{% csrf_token %}
<div style="margin-left: 80px">
<input type="hidden" name="product-code" , value="{{ item.code }}">
<label for="input-quantity">상품 입고:</label>
<input type="text" id="inbound_quantity" name="inbound_quantity" placeholder="입고 수량을 입력해주세요.">
<label for="input-price">입고 가격:</label>
<input type="text" id="inbound_price" name="inbound_price" placeholder="입고 가격을 입력해주세요.">
<input type="submit" value="입고 확인">
</div>
</form>
테플릿 문법
html코드내에 작성하면서, 어떤 조건을 걸 수 있는 편리한 기능
{% if,for %} {% endif,endfor %}등 반복문과 조건문 사용 가능
authenticated
{% if user.is_authenticated %}
<div class="username"> ID : {{ request.user }}</div>
{% endif %}
사용자가 로그인 상태인지, 아닌지 판별할 수 있는 Django의 지원 기능
{% for product in products %}
<tr>
<td>{{ product.author }}</td>
<td>{{ product.name }}</td>
<td>{{ product.code }}</td>
<td>{{ product.type }}</td>
<td>{{ product.size }}</td>
<td>{{ product.price }}</td>
{% if request.user == product.author %}
<td><a href="{% url 'delete_product' product.id %}">Delete</a></td>
{% endif %}
</tr>
{% endfor %}
딕셔너리 형태로 데이터를 사용하는등 여러 응용 방법이 있다.
for문안의 조건문에 if 분기문은 등록한 유저가 현재 접근한 유저와 같은지 체크하는 과정.
return render(request, 'user/signup.html')
return redirect('/')
render 메소드는, 사용자에게 html을 출력하기 위한 메소드
redirect 메소드는, 사용자를 어떠한 경로(url)로 안내하기 위한 메소드
from django.contrib.auth.decorators import login_required
@login_required
def logout(request):
auth.logout(request)
return redirect('/')
Django의 내장기능, 로그인한 유저만 접근할 수 있는 데코레이터를 연결할 수 있따.product = models.OneToOneField(Product,on_delete=models.CASCADE)
product = models.ForeignKey(Product,on_delete=models.CASCADE)
이번 과제는, 이러한 데이터 필드의 특성을 알아가기 위한것이 아니였나 싶다.
UserModel 과 Product
one - to - many
사용자는 한명이지만, 상품은 여러가지를 가질 수 있다.
Product 와 Inbound
one - to - many
제품은 하나지만, 입고 기록은 여러가지를 가질 수 있다.
Product와 OutBound
one - to - many
제품은 하나지만, 출고 기록은 여러가지를 가질 수 있다.
Product와 Inventory
제품은 하나고, 입고 기록, 출고 기록 합산 내용도 하나만을 가진다.
product = Product.objects.filter(code=search_code)
product = Product.objects.get(code=search_code)
위의 코드는 어떠한 검색값의 코드와 일치하는 제품을 탐색하는 코드다.
filter 는 탐색된 쿼리셋 (오브젝트의 집합) 을 반환한다.
get은 코드와 일치하는 오브젝트의 '코드 값'을 반환한다.
get을 사용할경우 단일 대상일 것이라는 확신이 있을때 사용한다.
filter().first
필터링하여 탐색한 오브젝트의 가장 첫번째 값을 반환한다.
context = { 'product':product }
context.update({ 'inbound':inbound })
context.update({'inventory': inventory_})
context.update({'product_info': product_info})
return render(request, 'inbound/inbound.html', context)
update를 활용해 딕셔너리 안에 딕셔너리를 추가로 또 넣어준 효과.
def test(request):
get_code = request.POST.('product-code')
get_product = Inbound.Object.get(code=get_code)
new_inbound = Inbound(product=get_product)
new_inbound.save()
reqeust로 POST형식으로 전달된 데이터의 코드값을 변수에 저장하고.
get을 이용하여 해당하는 탐색값을 조회하고
새로운 Inbound 모델의 데이터를 저장한 간단한 코드 예시
from inbound.models import Inbound
from outbound.models import OutBound
from base.models import Product
from .models import Inventory
from django.db.models import Sum, Value
from django.db.models.functions import Coalesce
def inventory(code):
product = Product.objects.get(code=code)
inventory_item = Inventory.objects.filter(product=product).first()
if inventory_item:
# 제품이 있으면 업데이트
# Coalesce 좌항의 값이 null이라면, 우항의 값을 대입한다.
inventory_item.total_inbound_price = Coalesce(Inbound.objects.filter(product__code=code).aggregate(Sum('inbound_price')).get('inbound_price__sum'), Value(0))
inventory_item.total_outbound_price = Coalesce(OutBound.objects.filter(product__code=code).aggregate(Sum('outbound_price')).get('outbound_price__sum'), Value(0))
inventory_item.total_inbound_quantity = Coalesce(Inbound.objects.filter(product__code=code).aggregate(Sum('inbound_quantity')).get('inbound_quantity__sum'), Value(0))
inventory_item.total_outbound_quantity = Coalesce(OutBound.objects.filter(product__code=code).aggregate(Sum('outbound_quantity')).get('outbound_quantity__sum'), Value(0))
inventory_item.save()
else:
new_inventory = Inventory(product=product,
total_inbound_price=Coalesce(Inbound.objects.filter(product__code=code).aggregate(Sum('inbound_price')).get('inbound_price__sum'), Value(0)),
total_outbound_price=Coalesce(OutBound.objects.filter(product__code=code).aggregate(Sum('outbound_price')).get('outbound_price__sum'), Value(0)),
total_inbound_quantity=Coalesce(Inbound.objects.filter(product__code=code).aggregate(Sum('inbound_quantity')).get('inbound_quantity__sum'), Value(0)),
total_outbound_quantity=Coalesce(OutBound.objects.filter(product__code=code).aggregate(Sum('outbound_quantity')).get('outbound_quantity__sum'), Value(0))
)
new_inventory.save()
더러운 코드의 향연이..
aggregate(Sum('필드')).get('필드__sum')
get함수로 필드의 합산값을 쿼리셋으로 구해오고.
aggregate함수로 쿼리셋의 합산값을 구한다.
Coalesce
합산값이 없는, 데이터가 없는 null값을 방지하기 위해 사용된 메소드
Coalesce(데이터),Value(0)
데이터가 Null값이라면, 우항의 Value값을 반환한다.