返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
Django

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

曦远 (DealiAxy)约 11 分钟
目录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

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~

_EOF_

Handling Foreign Keys in the Django-Import-Export Plugin

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

Django项目笔记:sessions处理以及复杂对象序列化

前言:一点题外话 我发现,不更新博客的时候,不是非常忙,就是效率非常低。最近没怎么更新博客,原因是第二种= =。惭愧惭愧。 今天效率出奇的高,一天时间把PassNote后端的接口全部写完了,Django很好用,不过实际项目中还是有些框架不能实现的功能需要解决的,我比较喜欢造轮子,不过为了保证效率,还是尽量用现成的工具,减少不必要的折腾。 关于PassNote 之前应该在博客有说过,这是一个密码管理

2022-04-02 · Django · 约 1 分钟

DjangoRestFramework框架三种分页功能的实现 - 在DjangoStarter项目模板中封装

前言 继续Django后端开发系列文章。刚好遇到一个分页的需求,就记录一下。 Django作为一个“全家桶”型的框架,本身啥都有,分页组件也是有的,但默认的分页组件没有对API开发做优化,所以DjangoRestFramework这个专门写API的框架又把Django的分页组件包装了一层,集成在viewsets里的时候会更方便。 不过我们不可能一直用viewsets,有一部分API还是要用自由度更

2022-08-26 · Django · 约 1 分钟

Django探索验证码功能的实现 - DjangoStarter项目模板里的封装

前言 依然是最近在做的这个项目,用Django做后端,App上提交信息的时候需要一个验证码来防止用户乱提交,正好我的「DjangoStarter」项目脚手架也有封装了验证码功能,不过我发现好像里面只是把验证码作为admin后台登录的校验手段,并没有给出前后端分离项目的验证码相关接口。 所以本文介绍验证码功能实现的同时,也对「DjangoStarter」的验证码模块做一层封装,使其更方便使用~ 用哪

2022-04-09 · Django · 约 1 分钟

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…