2013-09-25 30 views
2

首先,它不是關於圓形Model的引用。我可以找到很多關於這些問題的答案。Django管理命令:提供沒有循環引用的基類

我正在構建一個應用程序,讓您在設置中輕鬆定義提醒。因此,REMINDERS設置如下:

設置

REMINDERS = [ 
    { 
     'reminder_id': 'my-id', 
     'reminder_class': 'students.reminders.ExpiredAvailabilityEmailReminder', 
    }, 
] 

在這個應用程序(reminders),我對這些提醒提供一個基類,即中有一些默認的行爲。要定義一個提醒,我要做的唯一事情是例如:

students_app/reminders.py

from reminders.base_reminders import ReminderEmail 
from .models import Student 


class ExpiredAvailabilityEmailReminder(ReminderEmail): 
    template_name = 'students/emails/setting_expired.html' 

    def get_queryset(self): 
     return Student.objects.filter(
        is_active=True, 
        setting_expired__lt=datetime.date.today()) 

reminders_app /管理/命令/ send_reminders.py

管理命令導入此reminder_class -string在handledjango.utils.module_loading.import_by_path,但這種失敗:

django.core.exceptions.ImproperlyConfigured: Error importing module studenten.reminders: "No module named base_reminders" 

因爲它成爲這是一個循環參考。 如何嚮應用程序的用戶提供子類別的基類,而不會遇到此問題?

謝謝。

+0

爲什麼它是循環引用?你是否也在導入基類,如果是的話,爲什麼? –

+0

你需要調用'reminders.py'文件嗎? – alecxe

回答

1

此問題與Python 2中的默認導入順序有關。如果您處於students_app/reminders.py並且您有from reminders.base_reminders import ReminderEmail,則不清楚reminders模塊是指自己還是您的應用程序,也稱爲提醒。爲了解決這個問題,你可以在Python中使用from __future__ import absolute_import 2.5+(或使用Python 3中,這是默認設置)

from __future__ import absolute_import 
from reminders.base_reminders import ReminderEmail 

,這將確保reminders進口使用reminders應用程序,而不是相對reminders模塊。

另一個修復程序是一個文檔,它不鼓勵人們命名模塊reminders以避免這種命名衝突。

+0

非常感謝。 'students_app'中文件的命名實際上並沒有發生變化。但'absolute_import'完美運作。 – Tino