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

Bug: relationship object not available in EDIT template but available in DETAIL template #534

Open
sglebs opened this issue Apr 7, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@sglebs
Copy link

sglebs commented Apr 7, 2024

Describe the bug

I overrode both the DETAIL template and the EDIT template of my object/table (let's call it Foo). It has a relationship to another object/table, named Client:

    client_id = Column(ForeignKey('clients.id', ondelete='SET NULL', onupdate='CASCADE'))
    client = relationship('Client')

This Client object has a field/column, vhost, which is a String.

I have this snippet in my temple for both EDIT and DETAIL:

data-backend-host="{{ obj["client"]["vhost"] }}"

Then I am looking at my entity, the value is populated. When I am editing, I get an empty string.

What I SUSPECT: in EDIT mode the relationships are fetched in a lazy fashion, perhaps? I need to force the loading, perhaps? How?

To Reproduce

Well, in any of your objects/entities/tables, override EDIT and DETAIL templates and print a property of a related entity.

Environment (please complete the following information):

Starlette-Admin version: 0.13.1
ORM/ODMs: SQLAlchemy, psycopg2-binary==2.9.9
SQLAlchemy-serializer==1.4.1

Additional context

@sglebs sglebs added the bug Something isn't working label Apr 7, 2024
@sglebs
Copy link
Author

sglebs commented Apr 7, 2024

In EDIT mode, obj["client"] is a STRING !!!!! I tried obj["client"].vhost.get() (based on https://stackoverflow.com/questions/25675569/how-to-access-properties-of-referenced-objects-in-jinja2-templates-google-app-e) but got jinja2.exceptions.UndefinedError: 'str object' has no attribute 'vhost'

Obviously obj["client"].get().vhost does not work either.

@sglebs
Copy link
Author

sglebs commented Apr 16, 2024

I could really use some help on this one. Any tips on how to move forward?

One difference between the DETAIL and the EDIT screens/HTMLs is that in EDIT mode my relationship entity (Client) is displayed in a drop-down, whereas in the DETAIL screen it is a "clickable blue button". Maybe this causes side-effects in the "obj" object in jinja2?

@jowilf
Copy link
Owner

jowilf commented Apr 16, 2024

The edit template only uses the value of the primary key

obj_serialized[field.name] = await foreign_model.get_pk_value(

You can use the raw_obj

"raw_obj": obj,
).

Here's an example: https://github.com/jowilf/starlette-admin-demo/blob/a1bc5d09dd6a11ecb17a6a834031c1ad4cc6561c/templates/admin/sqla/post_detail.html#L81

@sglebs
Copy link
Author

sglebs commented Apr 16, 2024

raw_obj worked like a charm. Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants