qor / admin

Qor Admin - Instantly create a beautiful, cross platform, configurable Admin Interface and API for managing your data in minutes.
918 stars 189 forks source link

How do I display meaningful previews for my models? #246

Open preslavrachev opened 3 years ago

preslavrachev commented 3 years ago

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?

raven-chen commented 3 years ago

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

preslavrachev commented 3 years ago

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?

lutfuahmet commented 3 years ago
type Sample struct {
    Name string //add this field for label
    Email string
}

func (s *Sample) BeforeSave() {
  if s.Name == "" {
      s.Name = s.Email
  }
}
preslavrachev commented 3 years ago
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.

willvictor commented 3 years ago

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

gstvg commented 3 years ago

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