저는 프로젝트에 추가 할 간단한 평가 및 댓글 앱을 만들고 있으며 모델 생성에 대한 조언을 찾고 있습니다.필드 값을 기반으로 다른 모델에 연결하는 모델
일반적으로, 나는 다음과 같이 이러한 데이터베이스 스키마를 만들 것입니다 :
이comment_
id - primary key
type - varchar (buyer_item, buyer_vendor, vendor_buyer)
source_id - int (primary key of the table based on the type)
target_id - int (primary key of the table based on the type))
timestamp - timestamp
subject - varchar
comment - text
rating_
id - primary key
type - varchar (buyer_item, buyer_vendor, vendor_buyer)
source_id - int (primary key of the table based on the type)
target_id - int (primary key of the table based on the type)
timestamp - timestamp
rating - int (the score given, ie: 1-5 stars)
이 날 나를 올바른 유형을 설정하여 일의 모든 유형에 대한 의견이나 등급을 적용 할 수 있도록 할 간단한 방법을 보자 것이며, 나는이 모델은 모델의 일부로서 다른 테이블의 관계를 정의 알고
add_comment('user_product', user.pk, product.pk, now, subject, comment)
add_comment('user_vendor', user.pk, vendor.pk, now, subject, comment)
:처럼, 그것은 (target_id)에 적용 무엇 (SOURCE_ID)를 제출 한 사람의 ID의 설정. TYPE 필드가 SOURCE_ID 및 TARGET_ID가 링크되는 테이블을 결정하는 이러한 유형의 테이블에서 관계를 정의하는 방법은 무엇입니까?
또는 모델에서 관계를 생략하고 QuerySet을 가져올 때 조인을 설정해야합니까?
일반 테이블 아이디어를 휴지통에 넣고 각 테이블 (예 : user_ratings, product_ratings, transaction_ratings 등)에 사용할 여러 테이블을 만듭니다.
여기에서 가장 좋은 방법은 무엇입니까? 내 DBA 감각은 공통 테이블을 사용한다고하지만 Django 초보자는 원주민이하는 일을 확신하지 못합니다.
감사합니다.
https://docs.djangoproject.com/en/1.0/ref/contrib/contenttypes/#generic-relations, 나는 아직 작성하지 않은 당신의 models.py – lalo
을 제시해주십시오. 그것이 질문의 요점이며, 그것을 쓰는 가장 좋은 방법을 결정하는 것입니다. :-) – diamondsea