2

我生產一個web應用程序。我遇到的問題是查詢集不能識別模型。我已經成功地將所有的基於類的視圖和數據混淆了,但是,當我嘗試並執行查詢設置時,它只是說模型「課程」未定義。我導入了以下型號:Django的類查詢集模型視圖中<code>Django</code>存儲和排序的課程沒有定義

class Course(models.Model): 

    provider = models.ForeignKey(Provider) 

    title = models.CharField('Course Title', 
    max_length=200, 
    ) 

    first_line = models.CharField('Address Line: 1', 
    max_length=200, 
    ) 
    second_line = models.CharField('Address Line: 2', 
    max_length=200, 
    ) 
    third_line = models.CharField('Address Line: 3', 
    max_length=200, 
    ) 
    city = models.CharField('City', 
    max_length=200, 
    ) 
    post_code = models.CharField('Post Code', 
    max_length=200, 
    ) 
    course_description = models.TextField('Description') 
    date = models.DateField('Date') 

    start_time = models.TimeField('Starting time') 
    finish_time = models.TimeField('Finishing time') 
    duration = models.IntegerField('Number of hours') 
    CPD = models.IntegerField('CPD points') 
    link = models.CharField('Link', max_length=200) 
    category = models.ForeignKey(Categories) 
    gen_cat = models.ForeignKey(Gen_Categories) 
    location = models.ForeignKey(Gen_Location) 
    cost = models.FloatField('Cost') 

而且我有以下基於類的視圖。像date_screen()那樣的函數被寫入另一個文件並被導入,它們在我的基於函數的視圖中工作。問題是它一直說Course沒有定義。如果你可以發現基於課堂觀點的其他問題,請給我一個提醒。我可以開發一個基於班級的視圖,可以提取所有數據,但更多的細微差別現在是頭腦風暴。

class Courses_By_Location(ListView): 
    template_name = 'courses/course_list.html' 
    model = Course 

    def get_queryset(self): 
     self.Course = get_object_or_404(Course, name=self.args[0].order_by('date')) 
     raw_courses = Course.objects.filter(location=self.location) 
     courses = date_screen(raw_courses) 
     categories = category_screen(courses) 
     locations = location_screen(courses) 

    def get_context_data(self, **kwargs): 
     context = super(searchView, self).get_context_data(**kwargs) 
    context.update({'locations': self.locations, 
       'categories': self.categories, 
       'courses': self.courses, 
       'count': self.count,}) 
     return context 
+0

如果您可以發佈基於工作功能的視圖,則更有可能找到解決方案。 – Matt

+0

感謝您的輸入。我已經採取了另一個答案,因爲他已經完全重寫了這個觀點,並且工作得很好,而且非常清楚。再次感謝您抽出時間幫助我。 – max89

回答

1

我不知道到底是什麼問題,但它看起來像你把一些東西在不合適的地方。

# URLs 
url(
    r'^local/(?P<location>[-\w]+)/$', 
    views.Courses_By_Location.as_view(), 
    name='by_location', 
), 

# Views 
class Courses_By_Location(ListView): 
    model = Course 
    context_object_name = 'courses' 

    def dispatch(self, request, *args, **kwargs): 
     self.location = kwargs.get('location', 'DEFAULT-LOCATION') 
     return super().dispatch(request, *args, **kwargs) 

    def get_queryset(self): 
     # `date_screen` must return a QuerySet 
     return date_screen(
      # assuming the `Gen_Location` model has a `name` field 
      super().get_queryset().filter(location__name__iexact=self.location), 
     ) 

    def get_context_data(self, **kwargs): 
     context = super().get_context_data(**kwargs) 

     context['locations'] = location_screen(self.object_list) 
     context['categories'] = category_screen(self.object_list) 
     context['count'] = self.object_list.count() 

     return context 
+1

非常感謝你!我如何定義self.location?我的網址存根是:'網址(?r'local /(P [ - \ W] +)/ $」,views.Courses_By_Location.as_view(), 名= 'by_location'),' – max89

+0

我已經更新了我回答並添加了「dispatch」覆蓋 - 這將設置「self.location」。 – Matt

+1

再次感謝你。我的位置一般是'類Gen_Location(models.Model): gen_local = models.CharField( '常規位置', MAX_LENGTH = 200) display_gen_local = models.CharField( '顯示位置', MAX_LENGTH = 200, ) 高清__str __(個體經營): 回報self.gen_local'這是否意味着我改變'location__name__iexact'爲'location__charfield__iexact'?因爲它拋出一個Field錯誤 – max89

0

您是否從models.py導入了模型?

你應該通過導入模型,

from .models import Course 
+0

傻我,我忘了前綴的車型它現在承認它,但現在扔了錯誤:「對象的類型爲‘模塊’,而必須是一個Django模型,經理,或查詢集」 – max89

+0

你能張貼痕跡? – zaidfazil

+1

感謝您的輸入。我已經採取了另一個答案,因爲他已經完全重寫了這個觀點,並且工作得很好,而且非常清楚。再次感謝您抽出時間幫助我。 – max89

相關問題