【问题标题】:Return results according to ForeignKey根据 ForeignKey 返回结果
【发布时间】:2020-11-20 11:02:22
【问题描述】:

我的代码有问题好几天了。我声称对 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'),
]

在models.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”传递的内容。

【问题讨论】:

    标签: python django django-models django-views django-urls


    【解决方案1】:
    class CalendarsOfGroupView(generic.ListView): 
          model = Calendar 
          template_name = 'cal/calendarOfGroup.html' 
    
          def get_context_data(self, **kwargs): 
              context = super().get_context_data(**kwargs) 
              group_id = self.kwargs['group_id']
              # create var to hold all objects that do not have name==group_id
              object_list = Calendar.objects.exclude(name=group_id)
              # update the context
              context.update({'object_list': object_list})          
              return context
    

    编辑:过滤日历对象以包括只匹配group_id

    class CalendarsOfGroupView(generic.ListView): 
          model = Calendar 
          template_name = 'cal/calendarOfGroup.html' 
    
          def get_context_data(self, **kwargs): 
              context = super().get_context_data(**kwargs) 
              group_id = self.kwargs['group_id']
              # create var to hold all objects that do have name==group_id
              object_list = Calendar.objects.filter(name=group_id)
              # update the context
              context.update({'object_list': object_list})          
              return context
    

    【讨论】:

    • 当使用.exclude()时,输入你不想看到的group_id。如果您只想查看带有group_id 的对象,请使用.filter(Calendar.name=group_id)
    • 谢谢@Alec,但我有这个语法错误:“SyntaxError:keyword can't be an expression” for "(" in the line:"object_list = Calendar.objects.filter(Calendar.name =group_id) "
    • 我错了。 .filter() 内的参数不应包含对象名称。我已经编辑了我的答案。
    【解决方案2】:

    group_id 或任何其他 URL 参数将在 kwargs 中可用:

    group_id = self.kwargs['group_id']
    

    您可以在您的 get_queryset 方法或您的班级可用的任何其他方法中访问它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-24
      • 2019-06-10
      • 2021-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多