2011-03-28 17 views
2

我一直在使用Django的ORM(django 1.3)進行wtf時刻處理。在開發服務器:Django查詢從shell中運行,但不是dev服務器?在dev服務器中引發FieldError

# in x.modules.training.models 
class Training(x.models.Base): 
    trainers = models.ManyToManyField(staff_models.Staff, related_name='trainings_taught') 
    people = models.ManyToManyField(x.models.Person, related_name='trainings_attended') 

# in any view 
from x.modules.staff.models import Staff 
from x.models import Person 

Staff.objects.filter(trainings_taught=12) 
# [<Staff: Staff object>] 

Person.objects.filter(trainings_attended=12) 
# FieldError: Cannot resolve keyword 'trainings_attended' into field. Choices are: address, comment, contact_for_farmers, email, farmer, first_name, id, last_name, mobile_number, modified_by, modified_timestamp, national_id, passport_number, region, version 

# Why would it work for one ManyToMany and not the other? 

而在一般的蟒蛇殼,(注意X是Python路徑上),無論什麼兩個導入線的順序,這兩個查詢運行得很好。

>>> from x.models import * 
>>> from x import modules 
>>> Person.objects.filter(trainings_attended=12) 
>>> modules.staff.models.Staff.objects.filter(trainings_taught=12) 

這怎麼可能?

+0

你有任何類從繼承培訓? – zsong 2011-03-28 20:17:32

+0

工作人員確實從Person繼承(OneToOne繼承),但它從shell運行的事實似乎排除了模型驗證問題(並且模型確實通過了驗證)。 – Eloff 2011-03-29 00:02:45

回答

0

如果您有任何繼承自Training Class的類,則必須保證所有related_name都不相同。所以你應該聲明它:

people = models.ManyToManyField(x.models.Person, related_name='%(app_label)s_%(class)s_trainings_attended') 
+0

我有從Person繼承的類,但不是從Training繼承的類。 – Eloff 2011-03-28 22:28:48

+0

問題是員工類包含related_name「trainings_taught」,而培訓也包含該內容。 Django不允許通過模型類複製related_name。先試試我的解決方案。 – zsong 2011-03-29 04:11:23

+0

工作人員確實包含trainings_taught,該工作,但培訓不,培訓是ManyToManyField定義的地方。我嘗試了你的解決方案,我仍然在Person上得到FieldError,它無法將x_training_trainings_attended解析爲字段。 – Eloff 2011-03-29 12:45:28

相關問題