“无法分配/必须是实例”从CSV创建模型实例时出错(ForeignKey相关) [英] "Cannot assign / must be an instance" error when creating a model instance from a CSV (ForeignKey related)

查看:192
本文介绍了“无法分配/必须是实例”从CSV创建模型实例时出错(ForeignKey相关)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我已经阅读了一些相关的问题,但是对于我的特定问题,似乎没有答案,或者也许我误解了他们。)在我的应用程序中,我有一个模型 Kurs ,每个Kurs以这种方式分配给用户:

(I have read a few related questions, but none seems to have the answer for my particular problem, or perhaps I have misunderstood them.) In my app I have a model Kurs, and each Kurs is assigned to a user in this way:

class Kurs(models.Model):
    prowadzacy = models.ForeignKey(User)

我想从CSV中填充Kurs的数据库。每行的第0个元素是用户名。所以在我的读者例程中,我写了以下内容(跳过,我希望,不相关的细节):

I want to populate my database of Kurs's from a CSV. The 0th element of each row is a username. So in my reader routine I wrote what follows (skipping the, I hope, irrelevant details):

n = Kurs()
n.prowadzacy = User.objects.get(username=row[0])

现在在shell中,当我这样做时,我想到的是一个User实例:

Now, in the shell, when I do this, what I'm getting is, I thought, a User instance:

>>> User.objects.get(username='leszekwronski')
<User: leszekwronski>

但是当我运行我的阅读器例程时,它告诉我实际上没有一个User实例分配给prowadzacy字段:

But when I run my reader routine it tells me it does not actually have a User instance to assign to the 'prowadzacy' field:

ValueError: Cannot assign "'leszekwronski'": "Kurs.prowadzacy" must be a "User" instance.

我做错了什么?

(我认为也许我得到用户的身份,所以我也一心一意尝试

(I considered that maybe I'm getting the id of the User, so I also halfheartedly tried

n.prowadzacy = User.objects.get(id=User.objects.get(username=row[0]))`         

但是到达同样的错误。)

but arrived at the same error.)

--------------------------编辑
这是我的整个阅读器例程:

-------------------------- EDIT This is my whole reader routine:

from .models import *

import csv

def importcourses(filename):
    dataReader = csv.reader(open(filename), delimiter=';', quotechar='"')
    for row in dataReader:
        if row[0] != 'Username': # Ignore the header row, import everything else
            n = Kurs()
            n.prowadzacy = User.objects.get(username=row[0])
            n.nazwa = row[1]
            n.opis = row[2]
            n.kategoria = Kategoria.objects.get(id=row[3])
            n.semestr = Semestr.objects.get(id=row[4])
            n.ects = row[5]
            n.godziny = row[6]
            n.kanon = row[7]
            n.otwarty = row[8]
            n.save()

I认为其他领域的细节不相关...

I thought the details about other fields were not relevant...

-----------------编辑2
在RichSmith的评论下面,我已经从django.contrib.auth.models import添加了

----------------- EDIT 2 After RichSmith's comments below, I have added

from django.contrib.auth.models import User

给读者程序。现在更改为以下错误:

to the reader routine. The error now changed to the following:

TypeError: int() argument must be a string or a number, not 'User'

,错误分配到与以前相同的行。

and the error is assigned to the same line as before.

----------------------编辑3:
让这是一个教训我们所有的新手:-)重新启动后外壳,一切正在工作。看来RichSmith的想法是必需的修复!

---------------------- EDIT 3: Let this be a lesson to all of us newbies :-) After restarting the shell, everything is working. It seems RichSmith's idea was the needed fix!

推荐答案

您是否导入用户?来自django.contrib.auth.models import User

are you importing User? from django.contrib.auth.models import User

这篇关于“无法分配/必须是实例”从CSV创建模型实例时出错(ForeignKey相关)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆