如何导入身份验证用户关于导入/导出

2024-05-15 00:22:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我无法将CSV文件导入Django上的模型。 我做了一个列'作者'并把超级用户的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,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

管理员.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

Tags: 文件djangofromimportidtruetitlemodels
2条回答

当我用UTF-8编码保存CSV时,它被修复了。这将不支持非字母,所以我建议使用.xlsx文件代替。 感谢所有试图解决我问题的人。你知道吗

错误表示author_id丢失。你知道吗

Djangoadds a postfix到所有ForeignKey字段,因此您应该尝试修改重命名列的文件:

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

相关问题 更多 >

    热门问题