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

not able to use CustomColumn on extra_columns #962

Open
miyou995 opened this issue Sep 25, 2024 · 0 comments
Open

not able to use CustomColumn on extra_columns #962

miyou995 opened this issue Sep 25, 2024 · 0 comments

Comments

@miyou995
Copy link

miyou995 commented Sep 25, 2024

my case is simple i have a Product PaymentType and Price

i want my table to have as columns name, PT1, PT2, PT3 ...etc
and match the values so on PT1 i get the PaymentType id pass it to my model method

    def get_price_for_payment_type(self, payment_type_id):
        price = self.prices.get(payment_type_id=payment_type_id)
        return price.amount

and get the price, but it doesn't work like this

the custom Column

class PriceColumn(tables.Column):
    def __init__(self, payment_type_id, *args, **kwargs):
        self.payment_type_id = payment_type_id
        super(PriceColumn, self).__init__(*args, **kwargs)

    def render(self, record):
        return record.get_price_for_payment_type(self.payment_type_id)
    

the Table

class ProductHTMxTable(tables.Table):
    def __init__(self, data, *args, **kwargs):
        payment_types = PaymentType.objects.filter(is_active=True).order_by('order')

        extra_columns = []
        for payment_type in payment_types:
            column_name = f'price_{payment_type.id}'
            
            column = PriceColumn(
                payment_type_id=payment_type.id,
                verbose_name=payment_type.name,
            )

            extra_columns.append((column_name, column))

        super().__init__(data, extra_columns=extra_columns, *args, **kwargs)

i found a working solution but i still want to use a simple method like this .
the working solution create a model method that return a dict

     def get_price_dict(self):
        return {price.payment_type.id: price.amount for price in self.prices.all()}

and traverse it like this... full init method

class ProductHTMxTable(tables.Table):
    def __init__(self, data, *args, **kwargs):
        payment_types = PaymentType.objects.filter(is_active=True).order_by('order')

        extra_columns = []
        for payment_type in payment_types:
            column_name = f'price_{payment_type.id}'
            accessor_str = f'get_price_dict.{payment_type.id}' 

            column = tables.Column(
                verbose_name=payment_type.name,
                accessor=accessor_str,
                orderable=False,
                default='-'
            )
            extra_columns.append((column_name, column))

        super().__init__(data, extra_columns=extra_columns, *args, **kwargs)

i really want to know why the custom Column doesn't work
there is no error just the render method is not being called as i added a print on it and got nothing

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