I want to open a popup with an add form associated with a proxy model.
but the code below - creates a widget with a link to the admin form of the model from which my proxy model was created
class FooForm(forms.ModelForm):
foo_field= forms.ModelMultipleChoiceField(
queryset=MyProxyModel.objects.all(),
widget=RelatedFieldWidgetWrapper(
FilteredSelectMultiple("some txt", is_stacked=True),
MyProxyModel._meta.get_field('foo_field').remote_field,
admin.site,
can_add_related=True
),
required=False,
)
perhaps someone knows how to get the widget with the correct link - without overriding the methods in the RelatedFieldWidgetWrapper?
UPD:
in case anyone else has this problem:
you can fix this problem like this, but it’s a dirty solution
class FooForm(forms.ModelForm):
foo_field= forms.ModelMultipleChoiceField(
queryset=MyProxyModel.objects.all(),
widget=CustomRelatedFieldWidgetWrapper(
MyProxyModel,
FilteredSelectMultiple("some txt", is_stacked=True),
MyProxyModel._meta.get_field('foo_field').remote_field,
admin.site,
can_add_related=True
),
required=False,
)
class CustomRelatedFieldWidgetWrapper(widgets.RelatedFieldWidgetWrapper):
def __init__(self, model, *args, **kwargs) -> None:
self.model = model
super().__init__(*args, **kwargs)
def get_related_url(self, info: Tuple[str, str], action: str, *args: Any) -> str:
rel_opts = self.model._meta
info = (rel_opts.app_label, rel_opts.model_name)
return super().get_related_url(info, action, *args)