carltongibson / neapolitan

Quick CRUD views for Django
https://noumenal.es/neapolitan/
MIT License
413 stars 30 forks source link

Fields that are `ForeignKey`s to other models render as `pk`, not as the string representation of that model #41

Open joshuadavidthomas opened 5 months ago

joshuadavidthomas commented 5 months ago

In the object_list and object_detail templatetags, a field's value is rendered using the field's value_to_string method. On fields that are a ForeignKey to another model, this just renders that model's primary key, not the string representation of the model.

Before:

image image

If you change how the field's value is rendered by getting the attribute and rendering to string, you can get that related model's string representation.

After:

image image

See this relevant commit for what this would look like.

carltongibson commented 3 months ago

Hey @joshuadavidthomas — yes, makes sense. Is this something you'd do a PR for?

wassafshahzad commented 1 month ago

I this open to take ?

carltongibson commented 1 month ago

@wassafshahzad Yes, great.

Please keep the fix to just this issue here, not blending with #39 and #40, as the linked commit does. (I need to look at those separately.

A test case for the change will be needed.

Thanks! Let me know if you get stuck anywhere.

nanuxbe commented 1 month ago

@carltongibson any specific reason you didn't use str(getattr(object, f)) instead of object._meta.get_field(f).value_to_string(object) here: https://github.com/carltongibson/neapolitan/blame/main/src/neapolitan/templatetags/neapolitan.py#L59C17-L59C66 ?

It looks to me like a pretty simple fix but I'm fearing it's too simple

carltongibson commented 1 month ago

No real reason. It just didn't occur to me when I was bashing it together.

This is probably a change we should make yes.

There are a couple of related issues about the table generation and the actions links that likely go together. The reason they're still here is I didn't get a chance to totally think them through yet, but happy to have input.