咨询Django多部署环境下Settings模型的自定义导入导出实现方案
Hey Brian, I’ve been in a similar spot with Django 1.11 and custom settings models, so I get exactly what you’re trying to pull off. The tools you’ve tested are great for basic use cases, but they fall short on your specific requirements (selective export/import, handling relations, and creating duplicate copies). Let’s go through the most viable options that can cover all your needs:
1. Extend django-import-export with Custom Logic
This package is the closest out-of-the-box solution—you just need to tweak it to fit your workflow:
- Selective Export: Create a custom
ModelResourceand add an admin form that lets admins tick which fields (including ForeignKeys/ManyToManyFields) to export. Overrideget_export_fields()to dynamically filter fields based on user selection, and for relation fields, add nested serialization to capture associated data (e.g., serialize the PK and display name of linked objects). - Selective Import with Create/Update Choice: When importing, add a step where users select which settings from the file to import, plus a radio button for "Update existing records" or "Create new records". For updates, use
get_or_create()matching on a unique identifier (like a setting name) instead of PK. For creates, exclude the PK field from the import data so Django auto-generates a new one. - Duplicate Copies on Re-Import: To generate copies, ensure you strip out existing PKs from the import data. For relations, add an optional toggle: users can choose to either link to existing related objects or create duplicates of those related items too (you’ll need to recursively handle creation for nested relations).
2. Use django-fixture-magic (Tweaked for Your Needs)
This tool builds on Django’s fixture system to add more control, which works well for your custom settings models:
- Selective Export: Wrap the package’s
dump_objectcommand in a custom admin action. Admins can select specific settings instances, and you can use the--followflag to include FK/M2M relations. The command generates a fixture file with only the selected data. - Selective Import & Create/Update: Write a simple management command or admin view that parses the fixture file, displays all available settings for users to select, and then processes each selected item. For updates, use
update_or_create(); for creates, remove PK values from the fixture data before loading. - Duplicate Copies: Before importing, modify the fixture file to remove all PK fields (or replace them with
null). For relations, if you need copies of linked objects, pre-process the fixture to duplicate those entries first, then link the new settings instances to the duplicated relations.
3. Custom DRF Serialization/Deserialization (Optimized)
You tried nested ModelSerializers before, but with a few tweaks, this can work:
- Selective Export: Build dynamic serializers that accept a list of fields from the admin form. For example, in your export view, take the user’s selected fields and pass them to the serializer’s
fieldsparameter. For relations, use nested serializers to include the necessary associated data (e.g., M2M object IDs or full nested objects). - Selective Import: Create an import view that first parses the uploaded file and displays a checklist of settings to import. Based on the user’s "Update/Create" choice, use the serializer’s
update()method (matching on a unique field) orcreate()method (skipping PK assignment). - Duplicate Copies: When creating new instances, never pass the original PK from the export file. For relations, add a user option to either reuse existing linked objects or create duplicates—handle the recursive creation of related objects in your serializer’s
create()method if needed.
Important Notes for Django 1.11
Make sure to use compatible versions of these packages:
django-import-export: Stick to version 1.2.x (it officially supports Django 1.11)django-fixture-magic: Version 0.10.x works with Django 1.11
All these approaches can be tailored to your exact requirements, and the hybrid option (extending django-import-export) is probably the fastest to implement since it leverages existing admin integration.
内容的提问来源于stack exchange,提问作者Brian

