[Answered ]-Django ModelField custom attribute

1👍

You are attempting to muddle together the data logic and business logic. You described one use case for that model: when it’s to be saved. The cell position you had mentioned is only applicable to that use case and would be useless when you, for example, need to find a customer by its name.

Much senseful would be to put these constants to the file which is responsible for saving Customer to a excel file, like this:

class CustomerExcelExporter():
    CUSTOMER_FIELD_TO_FILE_CELLS = {
        "name": "B2",
        # ...
    }

    _worksheet: Worksheet
    _customer: Customer

    # ...

    def _fill_customer_own_fields(self):
        for field, cell in self.CUSTOMER_FIELD_TO_FILE_CELLS:
            self._worksheet.write(cell, getattr(self._customer, field)

Leave a comment