基于Django类的视图查询集模型尚未定义

时间:2017-05-18 10:27:11

标签: django django-models django-views django-class-based-views

我正在Django制作一个存储和排序课程的网络应用程序。我遇到的问题是查询集无法识别模型。我已经设法在基于类的视图中提取所有数据,然后,当我尝试执行查询设置时,它只是说模型"课程"没有定义。我导入了以下模型:

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

2 个答案:

答案 0 :(得分: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 :(得分:0)

您是否从models.py?

导入了模型

您应该按,

导入模型
from .models import Course