How to import auth.User on django-import/export

huangapple go评论65阅读模式
英文:

How to import auth.User on django-import/export

问题

无法将CSV文件导入Django模型。
我创建了一个名为'author'的列,并在超级用户登录到管理站点时将其ID放入。
但是当我导入CSV文件时,出现了如下错误。

行号:1 - 列“author_id”中的空值违反了非空约束详细信息:失败的行包含(10,abc,blahblah,null,)。
5,abc,blahblah,,nah,wha,blah

CSV文件

author,title,text,file,free_tags
5,abc,blahblah,,“nah,wha,blah”

models.py

from django.db import models
from django.urls import reverse
from taggit.managers import TaggableManager

class KnowHow(models.Model):    

    author = models.ForeignKey('auth.User',on_delete=models.CASCADE)
    title = models.CharField(max_length=200)
    text = models.TextField(blank=True)
    file = models.FileField(blank=True,upload_to='explicit_knowhows')
    free_tags = TaggableManager(blank=True)
    
    def __str__(self):
        return self.title

admin.py

from django.contrib import admin
from import_export import resources
from import_export import fields
from import_export.admin import ImportExportModelAdmin

from .models import KnowHow

class KnowHowResource(resources.ModelResource):

    class Meta:
        model = KnowHow
        exclude = 'id'
        import_id_fields = ('title', )

@admin.register(KnowHow)
class knowHowAdmin(ImportExportModelAdmin):
    resource_class = KnowHowResource
英文:

I can't import a CSV file into model on Django.
I made a column 'author' and put the superuser's id which I am login to the admin site.
But there was an error like this when I import the CSV file.

Line number: 1 - null value in column "author_id" violates not-null constraint DETAIL: Failing row contains (10, abc, blahblah, null, ).
5, abc, blahblah, , nah,wha,blah

csv file

author,title,text,file,free_tags
5,abc,blahblah,,"nah,wha,blah"

models.py

from django.db import models
from django.urls import reverse
from taggit.managers import TaggableManager

class KnowHow(models.Model):    

    author = models.ForeignKey('auth.User',on_delete=models.CASCADE)
    title = models.CharField(max_length=200)
    text = models.TextField(blank=True)
    file = models.FileField(blank=True,upload_to='explicit_knowhows')
    free_tags = TaggableManager(blank=True)
    
    def __str__(self):
        return self.title

admin.py

from django.contrib import admin
from import_export import resources
from import_export import fields
from import_export.admin import ImportExportModelAdmin

from .models import KnowHow
# Register your models here.

class KnowHowResource(resources.ModelResource):

    class Meta:
        model = KnowHow
        exclude = 'id'
        import_id_fields = ('title', )

@admin.register(KnowHow)
class knowHowAdmin(ImportExportModelAdmin):
    resource_class = KnowHowResource

答案1

得分: 1

错误提示说author_id字段丢失。

Django会在所有的ForeignKey字段后面添加一个后缀,因此您应该尝试修改文件以重命名该列:

author_id, title, text, file, free_tags
5, abc, blahblah, "", "nah,wha,blah"
英文:

The error says that author_id is missed.

Django adds a postfix to all the ForeignKey fields, so you should try to modify the file renaming the column:

author_id,title,text,file,free_tags
5,abc,blahblah,,"nah,wha,blah"

答案2

得分: 0

CSV 文件在保存时采用 UTF-8 编码后问题得到解决。然而,这不支持非字母字符,因此我建议改用 .xlsx 文件格式。感谢所有试图解决我的问题的人。

英文:

It fixed when I saved the CSV by encoding in UTF-8. This will not support non-alphabet letter so I recommend using .xlsx file instead.
Thank you to everyone who tried to fix my problem.

huangapple
  • 本文由 发表于 2020年1月3日 22:06:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579970.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定