-1

I have a model:

class Rent(models.Model):
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
    bikes = models.ManyToManyField(Bike)

when I create a new rent in the admin site, I would like to exclude the bikes that have the value of 1 for their status integer field from showing in the list to choose from. Is this possible?

Thanks

Yasser
  • 27
  • 1
  • 4

3 Answers3

0

I am not sure if you can do that from the Admin site. probably you cannot. But you can do it from views. just select the model objects you need with query and pass it into the field. I am attaching a ell explained similar scenario. condition your query and add those objects.

Adding many to many fields based on condition

SANGEETH SUBRAMONIAM
  • 1,098
  • 1
  • 9
  • 10
  • Thanks!, Yes I'm aware of this option, however I need it in the admin site if possible, I've seen something like `limit_choice_to` work with `foregin_key` but I don't really know how to achieve that with the many to many. – Yasser Mar 17 '21 at 02:33
0

You can customize the admin form. Here is a discussion of how to filter your form choices.

Dharman
  • 30,962
  • 25
  • 85
  • 135
ac2001
  • 726
  • 1
  • 6
  • 19
0

Thank you all! I figured it out, the problem was that the limit_choices_to doesn't work when I change the filter to inline and I found others with the same problem, so I just changed it to filter_horizontal and now it works as it should

Yasser
  • 27
  • 1
  • 4