我對Django來說很新,這讓我很難過。Django外鍵分配
我在調度工具的工作,有兩個型號至今:
#----------------------------------------------------------------------------------
class ReccurenceEvent(models.Model):
"""
A recurrence event defined an event which recurses over a period of time. The pattern of the recursion
is defined within the rec_type attribute.
"""
event_id = models.IntegerField(primary_key=True)
start_date = models.DateTimeField()
end_date = models.DateTimeField()
text = models.CharField(max_length=30)
rec_type = models.CharField(max_length=32)
event_length = models.BigIntegerField(null=True)
#----------------------------------------------------------------------------------
class Event(models.Model):
"""
A an event represents an independent calender event.
If the event relates to a series, the p_id points to a particular ReccurencePattern
"""
event_id = models.IntegerField(primary_key=True)
start_date = models.DateTimeField()
end_date = models.DateTimeField()
text = models.CharField(max_length=30)
rec_type = models.CharField(max_length=32)
event_length = models.BigIntegerField(null=True)
event_pid = models.ForeignKey(ReccurenceEvent, null=True)
在某些情況下,我要分配父事件ID(event_pid)一個事件,但是這應該是一個整數值而不是對該對象的引用。
當我做到以下幾點:
e.event_pid = event_pid
我得到以下信息:
Cannot assign "u'1359741862566'": "Event.event_pid" must be a "ReccurenceEvent" instance.
好了,但如果提供復發實例與:
e.event_pid = ReccurenceEvent.objects.get(event_id = event_pid)
我沒有存儲我需要的響應中的整數值。
有人能指出我要去哪裏嗎?
你構建模型的方式是錯誤的。您不需要放置主鍵,因爲django會自動生成它 – catherine