Django - Показать все ForeignKeys, принадлежащие заданному ID, как поле select multiple
У меня есть такая модель:
class OrderProduct(models.Model):
order = models.ForeignKey(to=Order, on_delete=models.CASCADE)
product = models.ForeignKey(to=Product, on_delete=models.CASCADE)
quantity = models.PositiveIntegerField(default=1)
price_paid = models.DecimalField(max_digits=5, decimal_places=2)
@property
def total_value(self):
return self.price_paid * self.quantity
def __str__(self):
return f"{self.order.id} / {self.order.user} // {self.product.name} / {self.quantity} ks"
def save(self, *args, **kwargs):
self.price_paid = self.product.price
super(OrderProduct, self).save(*args, **kwargs)
Это моя форма:
class ChangeOrderProductForm(ModelForm):
class Meta:
model = OrderProduct
fields = ('product',)
Это мое мнение:
def change_orderproduct(request, order_id: int):
set_session_cookie_restraunt_status(request)
if not check_if_user_has_permission(request, 'kitchen.change_orderproduct'):
messages.warning(request, 'Unauthorized to perform this action.')
return redirect("index")
ordered_products = OrderProduct.objects.filter(order__pk=order_id).first()
order = Order.objects.filter(pk=order_id).first()
if not order:
messages.info(request, "Given order does not exist.")
return redirect(request.META.get('HTTP_REFERER', index))
if request.method == "GET":
form = ChangeOrderProductForm()
context = {
"form": form,
"order": order
}
return render(request, "kitchen/change_orderproduct.html", context=context)
if request.method == "POST":
form = ChangeOrderProductForm(data=request.POST, instance=ordered_products)
if form.is_valid():
form.save()
messages.success(request, f"Products in order {order.pk} were successfully changed.")
return redirect(request.META.get('HTTP_REFERER', all_orders_view))
The code above works, but it only allows you to choose 1 Product in the ChangeOrderProductForm view: 
Почти для всех экземпляров Order существует несколько товаров.
Как сделать так, чтобы:
- Show all the possible
Productinstances to choose from in the form? - Make the already existing ForeignKeys assigned to
OrderProductas the pre-selected ones?
Basically my idea is, that if a user wants to modify the Products assigned to OrderProduct he will be able to choose from the whole list of the existing Products while also he will be able to see the already existing Products which are already linked to the OrderProduct. The user will then be able to select which Products he wants to either unlink or link to the given OrderProduct.
Спасибо