Django DetailView 通过两个不使用主键的参数进行过滤
Django DetailView filter by two parameters not using the primary key
我需要通过两个值过滤模型,其中 none 个值是模型的 PK。
我想过滤特定汽车和一周的付款,这意味着我可以在一周内为一辆车支付不止一次付款。
我正在使用 Django-Tables2 来显示结果。
views.py
class PagosDetailView(SingleTableMixin, DetailView):
template_name = "AC/paymentsbycarandweek.html"
context_table_name = 'table'
model = Pagos
table_class = PagosTable
slug_url_kwarg = 'carro_id'
def get_queryset(self):
qs = super(PagosDetailView, self).get_queryset()
return qs.filter(carro=self.kwargs['carro'], semana=self.kwargs['semana'])
models.py
class Pagos(models.Model):
carro = models.ForeignKey(
Carros, on_delete=models.CASCADE, blank=False, null=False)
pago = models.DecimalField(max_digits=6, decimal_places=2)
fecha = models.DateField(
auto_now=False, auto_now_add=False, blank=True, null=True)
semana = models.CharField(max_length=20)
startweek = models.DateField(
auto_now=False, auto_now_add=False, blank=True, null=True)
endweek = models.DateField(
auto_now=False, auto_now_add=False, blank=True, null=True)
renta = models.ForeignKey(
Renta, on_delete=models.PROTECT, blank=False, null=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
verbose_name_plural = "Pagos"
def get_absolute_url(self):
return reverse('pagos')
def __str__(self):
return self.semana
urls.py
path('paymentsbycarandweek/<int:carro>/<slug:semana>',
views.PagosDetailView.as_view(), name='pagos_bycar')
这是我最后一次尝试多种选择时收到以下错误消息:
通用详细视图 PagosDetailView 必须在 URLconf 中使用对象 pk 或 slug 调用。
我该如何传递这两个参数?
找了很多,最后是这样解决的:
class PagosDetailView(SingleTableMixin, ListView):
model = Pagos
table_class = PagosDetailTable
template_name = 'AC/paymentsbycarandweek.html'
paginate_by = 10
def get(self, request, *args, **kwargs):
self.object_list = self.get_queryset()
self.object_list = self.object_list.filter(
carro=kwargs['carro'], semana=kwargs['semana'])
context = self.get_context_data()
return self.render_to_response(context)
我需要通过两个值过滤模型,其中 none 个值是模型的 PK。
我想过滤特定汽车和一周的付款,这意味着我可以在一周内为一辆车支付不止一次付款。
我正在使用 Django-Tables2 来显示结果。
views.py
class PagosDetailView(SingleTableMixin, DetailView):
template_name = "AC/paymentsbycarandweek.html"
context_table_name = 'table'
model = Pagos
table_class = PagosTable
slug_url_kwarg = 'carro_id'
def get_queryset(self):
qs = super(PagosDetailView, self).get_queryset()
return qs.filter(carro=self.kwargs['carro'], semana=self.kwargs['semana'])
models.py
class Pagos(models.Model):
carro = models.ForeignKey(
Carros, on_delete=models.CASCADE, blank=False, null=False)
pago = models.DecimalField(max_digits=6, decimal_places=2)
fecha = models.DateField(
auto_now=False, auto_now_add=False, blank=True, null=True)
semana = models.CharField(max_length=20)
startweek = models.DateField(
auto_now=False, auto_now_add=False, blank=True, null=True)
endweek = models.DateField(
auto_now=False, auto_now_add=False, blank=True, null=True)
renta = models.ForeignKey(
Renta, on_delete=models.PROTECT, blank=False, null=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
verbose_name_plural = "Pagos"
def get_absolute_url(self):
return reverse('pagos')
def __str__(self):
return self.semana
urls.py
path('paymentsbycarandweek/<int:carro>/<slug:semana>',
views.PagosDetailView.as_view(), name='pagos_bycar')
这是我最后一次尝试多种选择时收到以下错误消息:
通用详细视图 PagosDetailView 必须在 URLconf 中使用对象 pk 或 slug 调用。
我该如何传递这两个参数?
找了很多,最后是这样解决的:
class PagosDetailView(SingleTableMixin, ListView):
model = Pagos
table_class = PagosDetailTable
template_name = 'AC/paymentsbycarandweek.html'
paginate_by = 10
def get(self, request, *args, **kwargs):
self.object_list = self.get_queryset()
self.object_list = self.object_list.filter(
carro=kwargs['carro'], semana=kwargs['semana'])
context = self.get_context_data()
return self.render_to_response(context)