0
假設我有三個模式是這樣的:django模型字段如何與多個模型相關?
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)
現在我想諾蒂奇用戶,如果他們的視頻或圖像獲得的評論
這就是我想要的
消息模型:
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
如果不能做的一個領域到多個模型,然後我的工作方法是更好的,或幫助我一個更好的!