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

How to deal with model property-functions #12

Open
odoublewen opened this issue Mar 11, 2022 · 1 comment
Open

How to deal with model property-functions #12

odoublewen opened this issue Mar 11, 2022 · 1 comment

Comments

@odoublewen
Copy link

@umesh-krishna Can you think of any clever way to make properties available in the server-side data table view?

I.e. say my models look like:

class Bar(models.Model):
    foo = models.ForeignKey(Foo, ....)

class Foo(models.Model):
    field1 = ...
    field2 = ...

    @property
    def number_of_bars(self):
        return Bar.objects.filter(foo=self).count()

How can I get number_of_bars into the datatable?

@odoublewen
Copy link
Author

I tried this:

class FooViewServerSide(ServerSideDatatableView):
    queryset = models.Foo.objects.annotate(number_of_bars =Count('bar')).all()
    columns = ['field1', 'field2', 'number_of_bars']

but, as you probably won't be surprised to learn, it doesn't work.

django.core.exceptions.FieldError: Cannot resolve keyword 'number_of_bars' into field

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant