Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
5.0k views
in Technique[技术] by (71.8m points)

python - Django REST framework - filtering against query param with date Outside Views.py file

I created my "API" using REST framework, now trying to do filtering for it. That's how my models.py look for BookingStatement model.

class BookingStatement(BaseModel):
    ticket_number = models.PositiveIntegerField(unique=True)
    booking = models.OneToOneField(Booking, on_delete=models.PROTECT)
    user_rate = AmountField()
    agent_rate = AmountField()
    total_amount = AmountField()

    class Meta:
        default_permissions = ()

    def __str__(self):
        return str(self.id)

Booking is One to One Key so the booking model has following Attributes.

class Booking(BaseModel):
    bus_seat = models.ManyToManyField(Seat)
    schedule = models.ForeignKey(Schedule, on_delete=models.PROTECT)
    boarding_point = models.ForeignKey(
        BoardingPoint,
        on_delete=models.PROTECT,
        null=True
    )
    remarks = models.JSONField(null=True, blank=True)
    contact = PhoneNumberField(null=True)
    booking_date_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        default_permissions = ()
        verbose_name = 'Booking'
        verbose_name_plural = 'Bookings'

    def __str__(self):
        return '{}-{}'.format(self.user, self.customer_name)

I used generic ListAPIView in my views.py as following.

class BusCompanyTicketDetailView(generics.ListAPIView, BusCompanyMixin):
    serializer_class = serializers.TicketDetailResponseSerializer
    def get_queryset(self):
        travel_date = (int(self.request.query_params.get('booking_date')))
        print(booking_date)

        return usecases.ListBusCompanyTicketUseCase(date=#'what should i pass?'#).execute()

I use usecases.py to filter booking_date_time with url as following.

http://127.0.0.1:8000/api/v1/ticket/list?booking_date=2021-1-29

So my usecase file to filter the Booking time is as following.

class ListBusCompanyTicketUseCase(BaseUseCase):
    def __init__(self, date:datetime):
        self._date = datetime

    def execute(self):
        self._factory()
        return self._booking_statements

    def _factory(self):
        self._booking_statements = BookingStatement.objects.filter(booking__booking_date_time=?? need to get date from views.)

Problem is I don't know to how to get query params from url in my usecases to filter with booking date any help will be very helpful.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you should use django-filter to implement filtering on the viewsets. It's a bit of knowledge you have to build up, but once you understand it, you can do a lot of complex filtering logic with it. Trying to implement a filtering system yourself is always more difficult in the long run. For starting point, check out the official documentation: https://www.django-rest-framework.org/api-guide/filtering/. For the filter, check out the documentation on DRF integration: https://django-filter.readthedocs.io/en/stable/guide/rest_framework.html.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...