曦远 (DealiAxy)一份私人的文字刊物

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中间件之实现Admin后台IP白名单

前言 Django自带的Admin管理后台很方便,但是实际生产环境真的会有挺多安全问题的,在admin的安全防护这方面,我之前就研究实现了给admin加上登录验证码和限流功能,可以参考这篇文章:给Django Admin添加验证码和多次登录尝试限制 不过就在内部使用的话就好很多,因此我们可以通过Django的中间件来实现IP过滤,所以本文中涉及到两个知识点,一个是Django的中间件,另外一个是P

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

讨论

共 0 条

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

加载评论中…