2017-01-10 63 views
0

我正在從1.6升級到1.9。 我的項目包括派生自此示例Audit Trail的代碼:https://code.djangoproject.com/wiki/AuditTrailrelated_name更改後1.6

實質上,此AuditTrail允許創建一個即時審計模型以記錄對審計表的任何更改。

部分代碼需要確保避免related_name之間的衝突。這如下完成,從1.6開始工作正常。

if isinstance(field, models.ForeignKey): 
    rel = copy.copy(field.rel) 
    rel.related_name = '_audit_' + field.related_query_name() 
    attrs[field.name].remote_field = rel 

出於某種原因,在升級後,Django的失敗,SystemCheckError對所有這些,與回溯看起來像:

SystemCheckError: System check identified some issues: 

ERRORS: 
email_reporting.ReportAudit.team: (fields.E304) Reverse accessor for 'ReportAudit.team' clashes with reverse accessor for 'Report.team'. 
     HINT: Add or change a related_name argument to the definition for 'ReportAudit.team' or 'Report.team'. 
email_reporting.ReportAudit.team: (fields.E305) Reverse query name for 'ReportAudit.team' clashes with reverse query name for 'Report.team'. 
     HINT: Add or change a related_name argument to the definition for 'ReportAudit.team' or 'Report.team'. 

任何想法如何解決呢?我找不到可能是罪魁禍首related_name的任何更改。

這隻會發生,如果我啓動與./manage.py shell_plus Django,但事情似乎啓動正確,如果開始./manage.py shell

+0

設置'related_name = '+''似乎解決它,但很明顯,它失去了功能。 – leonsas

回答

0

解決的辦法是直接修改rel代替remote_field

if isinstance(field, models.ForeignKey): 
    rel = copy.copy(field.rel) 
    if rel.related_name: 
     rel.related_name = '_audit_' + rel.related_name 
    elif rel: 
     rel.related_name = '_audit_' + rel.get_accessor_name() 
    attrs[field.name].rel.related_name=rel.related_name