【问题标题】:django import-export, export multiple many to many modelsdjango import-export,导出多个多对多模型
【发布时间】:2019-12-16 20:50:24
【问题描述】:

我有 Rule 模型,它有多个 RuleCondtionRuleAction。我想将这些导出到 csv 文件中。我为此使用django import-export

例子:

name, priority, tags, conditions, actions
Rule 1, 1000, "tag1,tag2", "[{"identifier":"A", "operator":"B"..}]", "[{"identifier":"A", "operator":"B"..}]"

我的模特:

    class Rule(models.Model):
        name = models.CharField(max_length=128, help_text="Name of Rule")
        description = models.TextField(help_text="Brief Description of Rule", blank=True)
        priority = models.IntegerField(default=1000, help_text="Priority of rule, lesser applies first")
        created_at = models.DateTimeField(auto_now_add=True)
        updated_at = models.DateTimeField(auto_now=True)
        tags = models.ManyToManyField('Tag', blank=True)
        disabled = models.BooleanField(default=True)

        def __str__(self):
            return self.name

    class RuleCondition(models.Model):
        identifier = models.CharField(max_length=128, help_text="Select a Property", blank=True)
        operator = models.CharField(max_length=128, help_text="Select an Operator", blank=True, choices=CONDITION_OPERATOR_CHOICES)
        value = models.TextField(help_text="Content to match the rule")
        rule = models.ForeignKey('Rule', on_delete=models.CASCADE, related_name='conditions')
        created_at = models.DateTimeField(auto_now_add=True)
        updated_at = models.DateTimeField(auto_now=True)

        def __str__(self):
            return 'Rule Condition ' + str(self.id)

    class RuleAction(models.Model):
        identifier = models.CharField(max_length=128, help_text="Select a Property", blank=True)
        operator = models.CharField(max_length=128, help_text="Select an Operator", blank=True, choices=ACTION_OPERATOR_CHOICES)
        value = models.TextField(help_text="Content to apply on the rule")
        rule = models.ForeignKey('Rule', on_delete=models.CASCADE, related_name='actions')
        created_at = models.DateTimeField(auto_now_add=True)
        updated_at = models.DateTimeField(auto_now=True)

        def __str__(self):
            return 'Rule Action ' + str(self.id)

我怎样才能做到这一点,django import-export 中没有选项可以做到这一点。

【问题讨论】:

标签: python django django-import-export


【解决方案1】:

想通了。这是代码。重写django-import-export的内部函数。

import json
from import_export import resources, fields
from django.core import serializers
from .models import Rule, Tag, RuleCondition, RuleAction
from import_export.widgets import JSONWidget, ManyToManyWidget, ForeignKeyWidget

class RuleOperationsWidget(ManyToManyWidget):

    def render(self, value, obj=None):
        return json.dumps(
            list(value.values('identifier', 'operator', 'value')),
        )

class RuleResource(resources.ModelResource):
    tags = fields.Field(
        attribute='tags',
        widget=ManyToManyWidget(model=Tag, separator=',', field='name'),
    )

    conditions = fields.Field(
        attribute='conditions',
        widget=RuleOperationsWidget(model=RuleCondition),
    )

    actions = fields.Field(
        attribute='actions',
        widget=RuleOperationsWidget(model=RuleAction),
    )

    class Meta:
        model = Rule
        exclude = ('created_at', 'updated_at', 'id',)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-08
    相关资源
    最近更新 更多