admin icon indicating copy to clipboard operation
admin copied to clipboard

How do I display meaningful previews for my models?

Open preslavrachev opened this issue 4 years ago • 6 comments

I know that the admin tries to make sense of the field names within the struct types and display the ones that make sense. However, it often falls into a situation, where it literally has no clue. Then, we end up having something like:

User#12345

image

What would be the preferred way to hint at QOR Admin which fields to use when displaying a preview?

preslavrachev avatar Feb 23 '21 10:02 preslavrachev

hi @preslavrachev Could you provide more context info? Where and when did you see this?

raven-chen avatar Feb 24 '21 07:02 raven-chen

hi @preslavrachev Could you provide more context info? Where and when did you see this?

@raven-chen Everywhere, where a relation is displayed. Say I have an order that has a user. If the user struct has no name attribute, I would get a list of orders, where in the column user, instead of seeing something humanly-readable (e.g. an email, or first namd + last name), I basically see something like the above. The same applies for when attaching a user to an order. I don't see humanly readable identifiers in the dropdowns, but some machine generated concatenation of the struct name and the id (all my model IDs are UUIDs BTW, I have a function that does that upon creation).

does that make sense?

preslavrachev avatar Feb 24 '21 07:02 preslavrachev

type Sample struct {
    Name string //add this field for label
    Email string
}

func (s *Sample) BeforeSave() {
  if s.Name == "" {
      s.Name = s.Email
  }
}

lutfuahmet avatar Feb 25 '21 10:02 lutfuahmet

type Sample struct {
    Name string //add this field for label
    Email string
}

func (s *Sample) BeforeSave() {
  if s.Name == "" {
      s.Name = s.Email
  }
}

Yeah, that's what I did too. If you mark the field as ignored by Gorm, it works, but it's a very obscure way of solving the problem. Relying on an established interface implementation (e.g. Stringer) would be much cleaner, IMO.

preslavrachev avatar Feb 25 '21 14:02 preslavrachev

Agreed that adding a "stringer" as an option for resources would be great!

willvictor avatar Feb 26 '21 20:02 willvictor

This is an undocumented feature, just add the method Stringify() string to your model, with value receiver, cause pointer receiver won't work.

It's implemented here:

https://github.com/qor/qor/blob/bfbb16ecbc40da17c123aa7cd7211b47caa31391/utils/utils.go#L164

gstvg avatar Jul 07 '21 14:07 gstvg