【问题标题】:Passing variable to a django queryset将变量传递给 Django 查询集
【发布时间】:2017-11-25 02:05:23
【问题描述】:

所以我在下面有这个 urlset。而在 urlset 中有两个 url 将参数带入 url 并将其传递给 apiviw。

urlpatterns = [
    url(r'^([0-9]{4})/([0-9]{1,2})/test/$', views.DuperView.as_view()),
    url(r'^test/([0-9]{4})/([0-9]{2})/$', views.SuperView.as_view()),
    url(r'^test/', views.TestView.as_view()),
]

这里是传入所有内容的 APIView。

class DuperView(APIView):
    queryset = models.DuperModel.dupers.duperdeduper()

    def get(self, request, year, month, format=None):
        an_apiview = [
            year,
            month,
            ]

        return Response({'http_method': 'GET', 'api_view': an_apiview})

在 url 中定义为 ([0-9]{4}) 的第一个参数作为 year 传递给 get 方法,而定义为 ([0-9]{1,2}) 的第二个参数传递作为月份。

这是我的模型、模型管理器和模型查询集,用于所有对应的模型。

class DuperQuerySet(models.QuerySet):
    def duperdeduper(self):
        return self.filter(year='2000')

class DuperModelManager(models.Manager):
    def get_queryset(self):
        return DuperQuerySet(self.model, using=self._db)

    def duperdeduper(self):
        return self.get_queryset().authors()

class DuperModel(models.Model):
    year = models.ForeignKey(YearModel)
    month = models.ForeignKey(MonthModel)
    name = models.CharField(max_length=255)

    dupers = DuperModelManager()

我想知道是否有办法以某种方式将年份和月份参数传递给模型,这样我可以在处理模型中的数据时使用它?

SELECT * FROM dupertable, yeartable WHERE year='variablevaluepassedon'

这种功能可能吗?我知道我可以在我的查询集上使用 .filter() 方法,但我的问题是将变量获取到查询集以使该查询工作。

【问题讨论】:

  • 不太清楚你在这里问什么。您可以通过覆盖视图中的get_queryset 方法将参数传递给查询集;你是这个意思吗?
  • 不,我无法根据需要修改 get_queryset 方法。假设请求的 URL 是“/api/1234/12/test/”。我需要将 1234 和 12 传递到模型中来处理一些数据。所以不知何故,我需要从 URL 到 APIView,然后从 APIView 到模型中获取这些确切的值。我希望查询将这两个值带到数据库并检查它们(因为这两个值是外键)然后我将从与该数据相关的两个模型/数据库表中获取数据。
  • 我看不出这与您发布的代码有什么关系。原来你说的是月份和年份,那你为什么不能DuperModel.dupers.filter(month=self.kwargs['month], year=self.kwargs['year'])呢?你在说什么其他模型?
  • 那么当我使用过滤器方法时,我可以将它们传递到模型中吗? apiview中的self.kwargs也是?我想了解更多关于 kwargs 的信息。
  • 但我不明白您所说的“将它们传递给模型”是什么意思?通过什么,在哪里 - 用它做什么?

标签: mysql django django-rest-framework jointable


【解决方案1】:

正如 Daniel 提到的,您应该从视图中覆盖 get_queryset 方法。从那里你可以传递你从 url 获得的参数。

class DuperView(GenericApiView, ListModelMixin):

    def get_queryset(self):
        year = self.kwargs.get('year')
        month = self.kwargs.get('month')

        return DuperModel.dupers.filter(month=month, year=year)

我已使用 GenericApiView 添加 get_queryset 方法。然后,您可以添加 API 所需的 mixin。在这种情况下,我添加了 ListModelMixin,它实现了一个 get 方法来检索元素列表。

您还应该在您的网址中引用您的参数:

urlpatterns = [
    url(r'^(?P<year>[0-9]{4})/(?P<month>[0-9][0-9]{1,2})/test/$', 
]

【讨论】:

  • A listapiview 是只读的,这可以与标准 apiview 一起使用吗?
  • 我不知道 apiview。我通常使用继承自 Apiview 的 GenericAPIView。
  • 那么既然 GenericAPIView 继承自 APIView,我是否只是在 DuperView 中实现 get、post、put 等?
  • 使用 GenericApiView,部分逻辑被实现为使用 get、post、put 等的 mixin(CreateModelMixin、ListModelMixin、RetrieveModelMixin、UpdateModelMixin 和 DestroyModelMixin),并使用由GenericApiView.
  • 那么我必须制作额外的通用 apiviews 吗?昨天当您提到 GenericAPIView 时,我查看了该页面,但我完全迷失了。我会阅读而不是像昨天那样略读。
猜你喜欢
  • 2015-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-16
  • 2016-12-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多