diff --git a/CHANGELOG.md b/CHANGELOG.md
index 43b2d22..c98d5ed 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,7 @@
## Improved
* game overview: improve how editions and purchases are displayed
+* add purchase: only allow choosing purchases of selected edition
## 1.5.1 / 2023-11-14 21:10+01:00
diff --git a/games/forms.py b/games/forms.py
index 19af792..a5e4384 100644
--- a/games/forms.py
+++ b/games/forms.py
@@ -1,5 +1,5 @@
from django import forms
-
+from django.urls import reverse
from games.models import Game, Platform, Purchase, Session, Edition, Device
custom_date_widget = forms.DateInput(attrs={"type": "date"})
@@ -50,6 +50,20 @@ class IncludePlatformSelect(forms.Select):
class PurchaseForm(forms.ModelForm):
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+
+ # Automatically update related_purchase
+ # to only include purchases of the selected edition.
+ related_purchase_by_edition_url = reverse("related_purchase_by_edition")
+ self.fields["edition"].widget.attrs.update(
+ {
+ "hx-get": related_purchase_by_edition_url,
+ "hx-target": "#id_related_purchase",
+ "hx-swap": "outerHTML",
+ }
+ )
+
edition = EditionChoiceField(
queryset=Edition.objects.order_by("sort_name"),
widget=IncludePlatformSelect(attrs={"autoselect": "autoselect"}),
diff --git a/games/templates/base.html b/games/templates/base.html
index 0e8b35f..7bbbb8d 100644
--- a/games/templates/base.html
+++ b/games/templates/base.html
@@ -13,7 +13,7 @@
-
+