I'm working on a project where a form is filled out. And in that form you have the is_active fields. If the user selects True, it means the account is active and if it is False, it means it is no longer active, and the user can no longer see it. Through the filters I'm trying to present only the forms with the is_active True, but I'm not able to.
Follow one of my attempts in my views:
class BookingViewSet(viewsets.ModelViewSet):
serializer_class = BookingSerializer
#queryset = Booking.objects.all()
#queryset = Booking.objects.filter(is_active="True")
#filter_backends = (filters.DjangoFilterBackend,)
#filterset_class = BookingFilter
#filterset_fields = ['is_active']
def get_queryset(self):
queryset = Booking.objects.all()
username = self.request.query_params.get('bookings')
if username is not None:
queryset = queryset.filter(is_active__username=username)
return queryset
and here are my models
class Booking(models.Model):
booking_id = models.AutoField(primary_key=True)
account = models.ForeignKey(Account, models.DO_NOTHING)
tenant = models.ForeignKey(Tenant, models.DO_NOTHING)
full_name = models.CharField(max_length=128)
email = models.CharField(max_length=256)
phone = models.CharField(max_length=256)
postal_code = models.CharField(max_length=64, null=True, blank=True)
from_city = models.CharField(max_length=256, null=True, blank=True)
to_city = models.CharField(max_length=256, null=True, blank=True)
travel_date = models.DateField()
travel_period = models.CharField(max_length=256, null=True, blank=True)
adults_travelers_count = models.SmallIntegerField()
children_travelers_count = models.SmallIntegerField()
senior_travelers_count = models.SmallIntegerField()
booking_request_description = models.TextField(blank=True, null=True)
booking_status_cd = models.CharField(max_length=10, null=True, blank=True)
locator_code = models.CharField(max_length=32, null=True, blank=True)
total_booking_price_atm = models.DecimalField(max_digits=11, decimal_places=2)
total_booking_cost_atm = models.DecimalField(max_digits=11, decimal_places=2)
payment_type_cd = models.CharField(max_length=10, null=True, blank=True)
payment_status_cd = models.CharField(max_length=10, null=True, blank=True)
payment_datetime = models.DateTimeField(blank=True, null=True)
discount_percent = models.DecimalField(max_digits=3, decimal_places=1)
discount_amount = models.DecimalField(max_digits=12, decimal_places=2)
payment_amount = models.DecimalField(max_digits=12, decimal_places=2)
voucher_file_path = models.TextField(blank=True, null=True)
receipt_file_path = models.TextField(blank=True, null=True)
invoice_file_path = models.TextField(blank=True, null=True)
modified_ts = models.DateTimeField()
modified_by = models.CharField(max_length=31)
modified_op = models.CharField(max_length=1)
created_by = models.CharField(max_length=31)
created_ts = models.DateTimeField()
is_active = models.BooleanField()
class Meta:
managed = True
db_table = 'booking'
unique_together = (("booking_id", "account", "tenant"),)
CodePudding user response:
Essentially, you're correct that the filter Booking.objects.filter(is_active=True)
will give you all the active bookings. If you also want to filter by username, you need to have another filter (can comma-separate in the same filter function) to filter by that. It's unclear where username
lives in your models, but assuming it's on the account model:
Booking.objects.filter(is_active=True, account__username=username)
CodePudding user response:
def get_queryset(self):
queryset = []
user = self.request.user
if user.is_authenticated:
bookings = Bookings.objects.all()
for i in bookings:
if i.is_active
queryset.append(i)
return queryset
This should do it!!