RamezIssac / django-slick-reporting

The Reporting Engine for Django. Create dashboards and standalone Reports and Charts.

Home Page:https://django-slick-reporting.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to get full name on related object in column

yellowlabrat opened this issue · comments

Hello,

I'm attempting to pull the full name of a user who created an instance into a column.

For example, in views.py:

columns = ['service_date',
               'created_by',
               'created_by__first_name',
               'created_by__get_full_name'
                ]

In the code above, "created_by" is defined in models.py like this:
created_by = models.ForeignKey(User, on_delete=models.RESTRICT)

If I use the columns listed above...

  • "created_by" column just shows the ID of the user who created the instance, but not the name - unsuccessful
  • "created_by__first_name" column shows the first name - successful
  • "created_by__get_full_name" (or other variations of it) gives the error:

django.core.exceptions.FieldDoesNotExist: Field "created_by__get_full_name" not found either as an attribute to the generator class <class 'slick_reporting.generator.ReportGenerator'>, or a computation field, or a database column for the model "<class 'clients.models.MyModel'>"

So, I'm wondering how to display the full name of a user in the report.

I'm a Django newbie, so I've experimented with several "creative" solutions:

  • tried concatenating first_name and last_name: 'created_by__first_name' + 'created_by__last_name' - unsuccessful
  • tried creating a "dummy" column name and then formatting it later with: def format_row(self, row_obj): - unsuccessful

This all came about because I ultimately wanted to use the django-simple-history module to access the user's name like this:

columns = ['service_date',
               'history.first.history_user.get_full_name'
                ]

But that definitely didn't work. So, I dialed it back to creating a "created_by" field on my MyModel (see above) and trying to access the standard User object, but this doesn't appear to work either.

What am I missing, or is this just not possible with the currently functionality?

Thank you for any feedback!

The issue is understandable

get_full_name is a method, here we are dealing with fields .
In slick reporting you can still arrive to what you want by having the ca callable on the ReportGenerator.
Something like:

`
columns = ['get_client_name', .....]

def get_client_name(self, obj):
return obj.client.get_full_name()
`

Thank you for the response!

When you refer to the "ca", what does that stand for? I didn't see that in the documentation.

Also, I tried the suggestion, but that did not work for me either.
Here's what I have in the views.py:

class TestReport(SlickReportView):
    # The model containing the data we want to analyze
    report_model = MyModel

    # The main date field used for the model
    date_field = 'service_date'

    # The columns you want to display
    columns = ['service_date',
               'get_client_name',
               'units',
               'service_rendered',
               'client__first_name',
                ]
    
    # Create client full name column
    def get_client_name(self, obj):
        return obj.client.full_name()

    def format_row(self, row_obj):
        row_obj['service_date'] = date(row_obj['service_date'], 'n/j/y') # Set date to: m/d/yy
        row_obj['units'] = "" if row_obj['units'] == None else row_obj['units'] # Hide "None" values

        return row_obj

The new column, "get_client_name" gives an error:

raise FieldDoesNotExist(
django.core.exceptions.FieldDoesNotExist: Field "get_client_name" not found either as an attribute to the 
generator class<class 'slick_reporting.generator.ReportGenerator'>, or a computation field, 
or a database column for the model "<class 'clients.models.MyModel'>"

I've tried moving the "def get_client_name" function above or below the "columns=[]", and outside of the TestReport class, but none of that changes the error message.

It seems like you are saying it is possible to put in a "dummy/placeholder" column name and then define it below, but for some reason the error doesn't seem to be allowing that.

Hello, sorry for late reply..
and another apologize as i made a mistake, the callable get_client_name , should go to the generator, not the SlickReportView, my bad.

Thank you for the correction.

I've looked through all of the documentation and Issues on the repo, but don't see an example of creating a report via the ReportGenerator. Do you have an example of that?

I see the docs on the ReportGenerator class, however, how does that get used on my views.py page?

Is it possible to use the ReportGenerator on views.py, then return the results into a SlickReportView?
Sorry if I'm missing something obvious.

Hello hello !
Since version 0.7 , you can set get_client_name method on the view (and not necessarily the generator)

For the documentation on how to use the generator, that would be added on day,
However, i'm working first on making all useage available from the view .