根据ForeignKey返回结果

几天来我的代码一直有问题。我声称对 Django 没有经验。我想根据特定的“CalendarGroups”获得结果,例如仅特定 CalendarGroups 的所有日历。要在其上过滤日历的组的 ID 我想通过 url whit“goup_id”传递它。


在 Url.py 中:


urlpatterns = [

    url(r'^$', views.hello, name='hello'), #Prova

    url(r'^home/$', views.GroupCalendarView.as_view(), name='home'), #Prova

    url(r'^home/(?P<group_id>\d+)/$', views.CalendarsOfGroupView.as_view(), name='calendar_view'),

]

在模型.py 中:


class CalendarGroups(models.Model):

    GRP = (

       ('Amazon', 'Amazon'),

       ('VICHY', 'VICHY'),

    )


    name = models.CharField(max_length = 155, blank=True, null=True)

    

    @property

    def get_html_url(self):

        url = reverse('', args=(self.id,))

        return f'<a href="{url}"> {self.name} </a>'

   


class Calendar(models.Model):

    name = models.CharField(max_length=200)

    #created_by

    group = models.ForeignKey(CalendarGroups, on_delete = models.CASCADE, default='')

 

    @property

    def get_html_url(self):

        url = reverse('cal:calendar_view', args=(self.id,))

        return f'<a href="{url}"> {self.name} </a>'

在 views.py 中:


class GroupCalendarView(generic.ListView):

    model = CalendarGroups

    template_name = 'cal/home.html'


    def get_context_data(self, **kwargs):

        context = super().get_context_data(**kwargs)

        #print (context)

        return context


class CalendarsOfGroupView(generic.ListView):

    

    model = Calendar

    template_name = 'cal/calendarOfGroup.html'


    def get_queryset(self, **kwargs):

        context = super().get_context_data(**kwargs)

               

        return context

这是我当前的代码,我不知道如何使用我在 views.py 中通过“group_id”传递的内容。


神不在的星期二
浏览 112回答 2
2回答

尚方宝剑之说

class CalendarsOfGroupView(generic.ListView):&nbsp;&nbsp; &nbsp; &nbsp; model = Calendar&nbsp;&nbsp; &nbsp; &nbsp; template_name = 'cal/calendarOfGroup.html'&nbsp;&nbsp; &nbsp; &nbsp; def get_context_data(self, **kwargs):&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context = super().get_context_data(**kwargs)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; group_id = self.kwargs['group_id']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # create var to hold all objects that do not have name==group_id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; object_list = Calendar.objects.exclude(name=group_id)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # update the context&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.update({'object_list': object_list})&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return context编辑:过滤日历对象以仅包含匹配的对象group_idclass CalendarsOfGroupView(generic.ListView):&nbsp;&nbsp; &nbsp; &nbsp; model = Calendar&nbsp;&nbsp; &nbsp; &nbsp; template_name = 'cal/calendarOfGroup.html'&nbsp;&nbsp; &nbsp; &nbsp; def get_context_data(self, **kwargs):&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context = super().get_context_data(**kwargs)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; group_id = self.kwargs['group_id']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # create var to hold all objects that do have name==group_id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; object_list = Calendar.objects.filter(name=group_id)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # update the context&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.update({'object_list': object_list})&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return context

Cats萌萌

group_id或任何其他 URL 参数将在以下位置可用kwargs:group_id&nbsp;=&nbsp;self.kwargs['group_id']get_queryset您可以在您的方法或您的类可用的任何其他方法中访问它。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python