UW-GAC / django-anvil-consortium-manager

Django app to manage AnVIL workspace permissions
https://django-anvil-consortium-manager.readthedocs.io/en/latest/index.html
MIT License
1 stars 0 forks source link

Cannot create a workspace data type with a (second) fk to Workspace #434

Closed amstilp closed 12 months ago

amstilp commented 12 months ago

When trying to create a new workspace for a workspace type that has a second fk to workspace, WorkspaceCreate gives this error:

Traceback (most recent call last):
  File "/Users/amstilp/devel/django/primed-django/venv/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/Users/amstilp/devel/django/primed-django/venv/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/amstilp/devel/django/primed-django/venv/lib/python3.9/site-packages/django/views/generic/base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/amstilp/devel/django/primed-django/venv/lib/python3.9/site-packages/django/contrib/auth/mixins.py", line 104, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/Users/amstilp/devel/django/primed-django/venv/lib/python3.9/site-packages/django/views/generic/base.py", line 98, in dispatch
    return handler(request, *args, **kwargs)
  File "/Users/amstilp/devel/django/primed-django/venv/lib/python3.9/site-packages/anvil_consortium_manager/viewmixins.py", line 290, in get
    return super().get(request, *args, **kwargs)
  File "/Users/amstilp/devel/django/primed-django/venv/lib/python3.9/site-packages/django/views/generic/edit.py", line 133, in get
    return self.render_to_response(self.get_context_data())
  File "/Users/amstilp/devel/django/primed-django/venv/lib/python3.9/site-packages/anvil_consortium_manager/views.py", line 1184, in get_context_data
    kwargs["workspace_data_formset"] = self.get_workspace_data_formset()
  File "/Users/amstilp/devel/django/primed-django/venv/lib/python3.9/site-packages/anvil_consortium_manager/views.py", line 1160, in get_workspace_data_formset
    formset_factory = inlineformset_factory(
  File "/Users/amstilp/devel/django/primed-django/venv/lib/python3.9/site-packages/django/forms/models.py", line 1069, in inlineformset_factory
    fk = _get_foreign_key(parent_model, model, fk_name=fk_name)
  File "/Users/amstilp/devel/django/primed-django/venv/lib/python3.9/site-packages/django/forms/models.py", line 1045, in _get_foreign_key
    raise ValueError(
ValueError: 'miscellaneous_workspaces.CustomWorkspaceData' has more than one ForeignKey to 'anvil_consortium_manager.Workspace'. You must specify a 'fk_name' attribute.

Example model:

class CustomWorkspaceData(BaseWorkspaceData):
    """Custom workspace data with a second fk to Workspace."""

    other_workspace = models.ForeignKey(
        Workspace,
        on_delete=models.PROTECT,
        related_name="custom_workspaces",
    )

The form:

class CustomWorkspaceForm(forms.ModelForm):
    """Form for a DataPrepWorkspace object."""

    class Meta:
        model = models.DataPrepWorkspace
        fields = (
            "workspace",
            "other_workspace",
        )