2013-05-15 98 views
1

我在想,什麼是更新多個領域的標準方式,其中包括在Django模型的實例的相關領域? ...如何更新包含django模型實例相關字段的多個字段?

Class User(models.Model): 
    id = models.CharField() 
    name = models.CharField() 
    dob = models.CharField() 

Class Appointment(models.Model): 
    user = models.ForeignKey(User) 
    app_id = models.CharField() 
    time = models.DateTimeField() 
    status = models.CharField() 

要更新預約模型中的多個領域,我可以只是做

dict ={ 
"app_id": "123", 
"time": "2012-01-01 10:30", 
"status": "Open" 
} 
Appointment.objects.filter(app_id=123).update(dict) 

有沒有辦法更新相關模型? 我怎樣才能更新約會模型和用戶模型?
案例:

dict ={ 
"name": "foo", 
"app_id": "123", 
"time": "2012-01-01 10:30", 
"status": "Open" 
} 

回答

2

爲了清楚起見 - 這不是什麼 '繼承' 的意思。約會與用戶有關,但不從其繼承。

查詢集的更新方法無法在單個調用中執行此操作。該文檔說:

對更新的QuerySet的唯一限制是它只能更新模型主表中的列,而不能更新相關模型中的列。

https://docs.djangoproject.com/en/dev/ref/models/querysets/#update

如果你有APP_ID,沒有別的,你可以寫一個第二更新電話:

User.objects.filter(appointment__app_id=123).update(name='foo') 
相關問題