내 웹 페이지에 댓글 섹션을 만들고 있으며 사용자가 댓글에 대해 찬성 또는 반대 투표를 할 수 있기를 바랍니다.
내 모델은 다음과 같습니다.
class Comment(models.Model):
owner = models.ForeignKey(User)
body = models.TextField(null=True, blank=True, max_length=500)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Vote(models.Model):
comment = models.ForeignKey(Comment)
upvote = models.SmallIntegerField(null=True, blank=True, default=0)
downvote = models.SmallIntegerField(null=True, blank=True, default=0)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
사용자가 댓글을 게시 할 때 해당 댓글에 연결된 투표 모델도 만들고 싶습니다.
나는 장고와 프로그래밍을 처음 접했지만 내 이해에서 저장 후크 또는 유사한 것을 만들어야합니까?
모델 의 save()
방법을 재정의 할 수 있습니다 Comment
.
class Comment(models.Model):
...
def save(self, **kwargs):
super(Comment, self).save(**kwargs)
vote = Vote(comment=self)
vote.save()
더 나은 통찰력을 위해 문서 를 읽는 것이 좋습니다 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다