Отображение иерархии моделей поля в форме администратора django
У меня есть модели django, как показано ниже:
class Subscription(models.Model):
"""
A subscription to a product.
"""
# Possible interval values.
user = models.ForeignKey(
to='account.User',
related_name='subscriptions',
on_delete=models.PROTECT,
)
uuid = models.UUIDField(unique=True, default=uuid4)
product = models.ForeignKey(
to='product.Product',
related_name='subscriptions',
blank=True,
null=True,
on_delete=models.PROTECT,
)
base_plan = models.ForeignKey(
to='product.Plan',
on_delete=models.PROTECT,
)
#code goes on...
class Plan(models.Model):
"""
Specifies a given set of billing parameters for a package.
"""
name = models.CharField(_('name'), max_length=50)
uuid = models.UUIDField(unique=True, default=uuid.uuid4)
product = models.ForeignKey(to=Product, related_name='plans',
on_delete=models.PROTECT)
package = models.ForeignKey(to=Package, related_name='plans',
on_delete=models.PROTECT)
#code goes on...
class Product(BillingProduct):
"""
A product is a collection of features that can be subscribed to.
"""
name = models.CharField(_('name'), max_length=50)
uuid = models.UUIDField(unique=True, default=uuid.uuid4)
slug = models.SlugField(_('slug'))
active = models.BooleanField(_('active'), default=True)
description = models.TextField(help_text=_("The public-facing description for the
product can include HTML."), blank=True)
#code goes on...
class Package(models.Model):
"""
Specifies a collection of features that a user can buy or trial.
"""
name = models.CharField(_('name'), max_length=50)
uuid = models.UUIDField(_('UUID'), unique=True, default=uuid.uuid4)
product = models.ForeignKey(to=Product, related_name='packages',
help_text=_("Must match features selected."),
on_delete=models.PROTECT)
feature_set = models.OneToOneField(to='product.FeatureSet', related_name='+',
on_delete=models.PROTECT)
#code goes on...
и Django Form, как показано ниже, которая будет использоваться в SubscriptionAdmin:
class SubscriptionForm(forms.ModelForm):
class Meta:
model = Subscription
exclude = []
widgets = {
'user': CustomerWidget
}
base_plan = forms.ModelChoiceField(queryset=Plan.objects.filter(is_active=True), to_field_name="slug")
base_plan.label = "Plan"
#code goes on...
def save(self, commit=True):
self.instance.feature_set = self.instance.base_plan.package.feature_set
self.instance.product = self.instance.base_plan.product
instance = super().save(commit=commit)
return instance
def __init__(self, *args, **kwargs):
super(SubscriptionForm, self).__init__(*args, **kwargs)
subscription = self.instance
def clean(self):
data = self.cleaned_data
#code goes on...
Теперь, то, чего я хочу добиться, это что-то вроде следующего для поля плана (base_plan) в форме Admin (как добавление, так и изменение):
ProductName(base_plan.product): #Collapsible Fieldset
--PackageName(base_plan.package): #Label
----(RadioButton)PlanName(base_plan) #Radio input
----(RadioButton)PlanName(base_plan) #Radio input
----(RadioButton)PlanName(base_plan) #Radio input
--PackageName(base_plan.package): #Label
----(RadioButton)PlanName(base_plan) #Radio input
----(RadioButton)PlanName(base_plan) #Radio input
ProductName(base_plan.product): #Collapsible Fieldset
--PackageName(base_plan.package): #Label
----(RadioButton)PlanName(base_plan) #Radio input
...
Могу ли я добиться чего-то подобного с помощью пользовательского виджета. Если да, то как? Как я могу передать данные в шаблон виджета? Кроме того, в случае изменения формы уже выбранный план должен быть помечен как отмеченный в RadioButton.