2013-01-10 1 views
0

내가 이런 세 가지 모델이 있다고 가정장고 모델 필드는 여러 모델과 어떻게 관련 될 수 있습니까?

class video(models.Model): 
     name=models.CharField(max_length = 100) 

class image(models.Model): 
     name=models.CharField(max_length = 100) 

class comments(models.Model): 
     content=models.CharField(max_length = 100) 

가 지금은 자신의 비디오 나 이미지가 코멘트

이 얻을 경우 사용자 notic 원하는 것은 내가 원하는 무엇

메시지 모델 :

class message(models.Model): 
     type=models.CharField(max_length = 100) # 'video' or 'image' 
     video_or_image=models.ForeignKey(video or image) 

     #the type is just a string to tell if the comment is about the video or image 
     #video_or_image need to be video foreignkey or image foreignkey depends on type 

이 가능합니다.

나는 현재 두 가지 방법

에 의해이 문제를 해결 :

class message(models.Model): 
     type = models.CharField(max_length = 100) # 'video' or 'image' 
     video_or_image_id = models.IntegerField(default = 1) 
     # 

class message(models.Model): 
     type=models.CharField(max_length = 100) # 'video' or 'image' 
     video=models.ForeignKey(video) 
     image=models.ForeignKey(image) 
     # if the comment is about video just leave the image empty 

여러 모델에 하나 개의 필드는 다음 수행 할 수없는 경우 내 작업 방식이 더 좋고, 더 나은 방법으로 도와주세요!

답변

관련 문제