Python 如何在django导入/导出时导入身份验证用户
我无法将CSV文件导入Django上的模型。 我制作了一列“author”,并将超级用户的id(我正在登录的id)放入管理站点。 但是当我导入CSV文件时出现了这样的错误Python 如何在django导入/导出时导入身份验证用户,python,django,python-3.x,django-import-export,Python,Django,Python 3.x,Django Import Export,我无法将CSV文件导入Django上的模型。 我制作了一列“author”,并将超级用户的id(我正在登录的id)放入管理站点。 但是当我导入CSV文件时出现了这样的错误 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
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文件
author,title,text,file,free_tags
5,abc,blahblah,,"nah,wha,blah"
型号.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
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
admin.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
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
错误表示缺少
作者id
Django指向所有ForeignKey
字段,因此您应该尝试修改重命名列的文件:
author_id,title,text,file,free_tags
5,abc,blahblah,,"nah,wha,blah"
当我用UTF-8编码保存CSV时,它被修复了。这将不支持非字母字母,因此我建议改用.xlsx文件。
感谢所有试图解决我问题的人。也许可以在文档中添加,以改进答案。我这样做了,但不幸的是,没有任何更改。
作者id、类别、标题、文本、文件、基本标签、免费标签5、abc、blahblah、、“不,cmon,等等”
是的<代码>行号:1-列“author_id”中的null值违反了非null约束详细信息我还尝试将“author”列的值添加到用户名,但也没有更改。我还注意到我无法导入Django taggit。我会问另一个问题。