WIL

손성수·2023년 4월 9일
0

WIL

목록 보기
4/5

주간 배운 내용

  • 앱을 만드는 과정
  1. 터미널에 앱 만들기
python manage.py startapp 앱이름
  1. 프로젝트 파일의 INSTALLED_APP리스트에 설치한 앱 등록
  2. url 연결(프로젝트 urls.py, app.urls.py
  3. app.admin에 등록할 모델 등록
from django.contrib import admin
from .models import Inbound
admin.site.register(Inbound)
  1. view.py 작성
  2. 터미널에 모델 변경사항 및 생성 알리기
python manage.py makemigrations
python manage.py migrate


  • form 태그
   <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>
  • form
    API, method와 action을 통해 POST형식으로 데이터를 전송하며
    action을 통해 어떤 경로로 데이터를 전달할건지 특정할 수 있다.
  • input
    type별로 텍스트,radio 등등 여러가지 데이터를 입력받을 수 있다.
    hidden은 사용자가 입력하는것이아닌 value값으로 POST로 전달할 데이터를 특정할 수 있다.
    submit을 통해, 사용자의 액션으로 입력받아 form 태그내의 input데이터들을 전달한다.
    input 태그에 required 조건을 추가하면, 필수사항 조건이 된다.


  • 테플릿 문법
    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 분기문은 등록한 유저가 현재 접근한 유저와 같은지 체크하는 과정.





  • render 와 redirect
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의 내장기능, 로그인한 유저만 접근할 수 있는 데코레이터를 연결할 수 있따.
    logout메소드로, 쉽게 로그아웃 기능을 구현할 수 있다.




  • one-to-one
    두개의 서로다른 데이터베이스의 연결 관계
    서로의 오브젝트는, 서로가 필드를 하나씩만 가질 수 있다.
product = models.OneToOneField(Product,on_delete=models.CASCADE)
  • one-to-many
    서로의 오브젝트는, 한쪽은 하나, 한쪽은 여러 데이터 필드를 가질 수 있다.
 product = models.ForeignKey(Product,on_delete=models.CASCADE)
  • many-to-many
    서로가, 여러개의 데이터 필드를 가질 수 있다.


  • 과제에서 깨달은것

이번 과제는, 이러한 데이터 필드의 특성을 알아가기 위한것이 아니였나 싶다.

UserModel 과 Product
one - to - many
사용자는 한명이지만, 상품은 여러가지를 가질 수 있다.

Product 와 Inbound
one - to - many
제품은 하나지만, 입고 기록은 여러가지를 가질 수 있다.

Product와 OutBound
one - to - many
제품은 하나지만, 출고 기록은 여러가지를 가질 수 있다.

Product와 Inventory
제품은 하나고, 입고 기록, 출고 기록 합산 내용도 하나만을 가진다.




  • filter와 get의 차이점
product = Product.objects.filter(code=search_code)
product = Product.objects.get(code=search_code)

위의 코드는 어떠한 검색값의 코드와 일치하는 제품을 탐색하는 코드다.

filter 는 탐색된 쿼리셋 (오브젝트의 집합) 을 반환한다.
get은 코드와 일치하는 오브젝트의 '코드 값'을 반환한다.
get을 사용할경우 단일 대상일 것이라는 확신이 있을때 사용한다.

  • first
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값을 반환한다.

profile
더 노력하겠습니다

0개의 댓글

관련 채용 정보

Powered by GraphCDN, the GraphQL CDN