Handling Foreign Keys in the Django-Import-Export Plugin
Preface Django-Import-Export is a very useful and convenient Django data import/export plugin that integrates perfectly with the Django Admin backend. With just a small amount of code configuration, you can easily implement import and export in various formats as needed. For more on how to use this plugin, you can check my previous article: put the article link here Previously when using it, I always created a dedicated raw data table to store the imported data, then did some data processing on the raw data table and stored the data in other tables before it could actually be used. (Not a very good
目录6 个章节
Preface
Django-Import-Export is a very useful and convenient Django data import/export plugin that integrates perfectly with the Django Admin backend. With just a small amount of code configuration, you can easily implement import and export in various formats as needed. For more on how to use this plugin, you can check my previous article: put the article link here
Previously when using it, I always created a dedicated raw data table to store the imported data, then did some data processing on the raw data table and stored the data in other tables before it could actually be used. (Doesn't seem like a great approach)
Now what I need is to import data based on the existing table structure, and the table I need to import data into also has several foreign keys, which involves handling foreign keys.
So I searched online for a long time (Google-oriented programming). Domestic materials mainly consist of the two articles in the references. After looking at them, they solved it with somewhat fancy hacky tricks, which felt so-so. That kind of hack couldn't satisfy me, so I gave up on Google and went straight to grinding through the official docs (I really regret why I didn't just grind through the official docs from the start...)
Solution
The real solution still has to come from the official documentation.
Model Configuration
First let's see how the models are defined. I'll put the official examples here; my own project's model has too many fields, so looking at the official ones is clearer.
class Author(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Category(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Book(models.Model):
name = models.CharField('Book name', max_length=100)
author = models.ForeignKey(Author, blank=True, null=True)
author_email = models.EmailField('Author email', max_length=75, blank=True)
imported = models.BooleanField(default=False)
published = models.DateField('Published', blank=True, null=True)
price = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True)
categories = models.ManyToManyField(Category, blank=True)
def __str__(self):
return self.name
Resource Configuration
For more on Resource configuration, you can refer to the official documentation. If you don't want to grind through the docs, you can check my previous article: article link
from import_export import fields, resources
from import_export.widgets import ForeignKeyWidget
class BookResource(resources.ModelResource):
author = fields.Field(
column_name='author',
attribute='author',
widget=ForeignKeyWidget(Author, 'name'))
class Meta:
fields = ('author',)
Done. This way, during import it will automatically associate the foreign key author.
References
- Official documentation: https://django-import-export.readthedocs.io/en/latest/api_widgets.html#import_export.widgets.ForeignKeyWidget
- Django-10 Import/Export Feature Optimization: https://www.jianshu.com/p/f82a465a41d8
- A method for django-import-export related foreign keys import/export in admin: https://blog.csdn.net/weixin_38496380/article/details/104717236
Welcome to Chat
Programming Lab focuses on exploring popular new internet technologies and team agile development practices. Reply with linux, flutter, c#, netcore, android, kotlin, java, python, etc. in the backend of the WeChat official account "Programming Lab" to get related technical articles and materials. If you have any questions, you can also leave a message on the official account~
- Cnblogs: https://www.cnblogs.com/deali/
- Coding livestream: https://live.bilibili.com/11883038
- Zhihu: https://www.zhihu.com/people/dealiaxy
_EOF_
Handling Foreign Keys in the Django-Import-Export Plugin
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
比Django官方实现更好的分页组件+Bootstrap整合
前言 Django全家桶自带的分页组件只能说能满足分页这个功能,但是没那么好用就是了 Django的分页效果 django-pure-pagination分页效果 使用方法 首先安装: pip install django-pure-pagination 添加到App INSTALLED_APPS = ( ... 'pure_pagination', ) 配置一下,配置说明如图 P
2021-12-18 · Django · 约 1 分钟
使用Django-Channels实现websocket通信+大模型对话
这个只是简单的demo,实际上生产还得考虑很多问题,本文就是为 channels 的应用开了个头,后续有新的研究成果会持续更新博客~
2024-08-14 · Django · 约 1 分钟
实测 Django 6.0:模版片段、后台任务、CSP 安全,三大特性体验报告
呵,连WebSocket都没塞进去就敢叫大版本更新?不过模版片段和内置后台任务总算让Django像个现代框架了,至少写HTMX时不用再到处塞碎片文件。原生CSP?早该有了。至于抛弃Python 3.11——跟不上时代的版本留着也是累赘。
2026-01-20 · Django · 约 1 分钟
Django中间件之实现Admin后台IP白名单
前言 Django自带的Admin管理后台很方便,但是实际生产环境真的会有挺多安全问题的,在admin的安全防护这方面,我之前就研究实现了给admin加上登录验证码和限流功能,可以参考这篇文章:给Django Admin添加验证码和多次登录尝试限制 不过就在内部使用的话就好很多,因此我们可以通过Django的中间件来实现IP过滤,所以本文中涉及到两个知识点,一个是Django的中间件,另外一个是P
2022-08-26 · Django · 约 1 分钟
讨论
共 0 条加载评论中…