Как условно исключить поле?
Мне нужно исключить id_from_integration, если Company.integration_enabled == False.
Мой ресурс и модель:
class Company(models.Model):
integration_enabled = models.BooleanField()
class Report(models.Model):
company = models.ForeignKey(Company)
class ReportResource(resources.ModelResource):
...
class Meta:
model = Report
fields = ('name', 'id_from_integration', ...)
Работает ли это для вас?
Обратите внимание, что вы не можете исключить столбец completed из-за случаев, когда Company.integration_enabled == True. Когда False, возвращается пустая строка.
class CustomField(Field):
def export(self, report):
if not report.company.id_from_integration:
return ""
return super().export(obj)
class ReportResource(resources.ModelResource):
# set column_name to the column name in the import
id_from_integration = CustomField(attribute="id_from_integration",
column_name="id_from_integration", widget=widgets.BooleanWidget())
class Meta:
model = Report
fields = ('name', 'id_from_integration', ...)
Я ожидаю, что ваш экспорт будет выглядеть примерно так:
id_from_integration |name |id
---------------------------|-------------|--
True | |1
| |12
| |13