Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark the field as non-linked #819

Open
1 task done
twistfire92 opened this issue Sep 12, 2024 · 1 comment
Open
1 task done

Mark the field as non-linked #819

twistfire92 opened this issue Sep 12, 2024 · 1 comment

Comments

@twistfire92
Copy link

twistfire92 commented Sep 12, 2024

Checklist

  • There are no similar issues or pull requests for this yet.

Is your feature related to a problem? Please describe.

I want to mark some relative fields so that they don't appear as links in the UI.

For example, I have User and Address models (some unnecessary details of the description of sqlalchemy models was omitted):

class Address:
    id: int
    zip_code: int
    city: str
    street: str
    house_number: str

    def __repr__(self):
        return f"{self.zip_code}, {self.city}, {self.street}, {self.house_number}"


class User:
    id: int
    name: str
    age: int
    address: Address
    

Also I want create only UserAdmin model

class UserAdmin(ModelView, model=User):
    name = "User"
    name_plural = "Users"

    column_list = ["id", "name"]

On User`s details page I will see the address field as a link leading to an 404 page (because I didn't create AddressAdmin model).

I want to mark address field as a simple text, without any links

Describe the solution you would like.

Add new ModelView attribute, called excluded_relations_names (for example)

class UserAdmin(ModelView, model=User):
    name = "User"
    name_plural = "Users"

    column_list = ["id", "name"]
    
    excluded_relations_names = ["address"]

add some conditions in details.html

...
<tr>
  <td>{{ label }}</td>
  {% set value, formatted_value = model_view.get_detail_value(model, name) %}
  {% if name in model_view._relation_names %}
  {% if is_list( value ) %}
  <td>
    {% for elem, formatted_elem in zip(value, formatted_value) %}
    <a href="{{ model_view._build_url_for('admin:details', request, elem) }}">({{ formatted_elem }})</a>
    {% endfor %}
  </td>
  {% else %}
  {% if name in model_view.excluded_relations_names %} <!-- NEW CHECKS HERE -->
  <td>{{ formatted_value }}</td>
  {% else%}
  <td><a href="{{ model_view._url_for_details_with_prop(request, model, name) }}">{{ formatted_value }}</a>
  </td>
  {% endif %}
  {% endif %}
  {% else %}
  <td>{{ formatted_value }}</td>
  {% endif %}
</tr>
...

Describe alternatives you considered

I found one way to show field as a normal text, without any links:

  1. Add some custom field to column_details_list
  2. Bind this field to related model
  3. Create a label for this custom field
class UserAdmin(ModelView, model=User):
    name = "User"
    name_plural = "Users"

    column_list = ["id", "name"]

    column_details_list = [
        "id",
        "name",
        "age",
        "address_field"  # some custom field name
    ]

    column_formatters_detail = {
        "address_field": lambda m, a: m.address
    }

    column_labels = {
        "address_field": "address"
    }

What if you have much more fields in UserModel? If you want add a custom field to column_details_list, you should explicitly specify all other fields.
Looks like a long workaround.

Additional context

No response

@Vasiliy566
Copy link
Contributor

You can try

``column_exclude_list: List of columns or column names to be excluded in the list page.
` `column_details_exclude_list`: List of columns or column names to be excluded in the details page.`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants