Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 许多领域不工作_Python_Django_Python 3.5 - Fatal编程技术网

Python 许多领域不工作

Python 许多领域不工作,python,django,python-3.5,Python,Django,Python 3.5,我想在一个人和其中提到的新闻文章之间建立一种关系 person/models.py class Person(models.Model): first_name = models.CharField(max_length=20) last_name = models.CharField(max_length=40) news_mentioned = models.ManyToManyField(News, blank=True) class News(models.Mo

我想在一个人和其中提到的新闻文章之间建立一种关系

person/models.py

class Person(models.Model):
    first_name = models.CharField(max_length=20)
    last_name = models.CharField(max_length=40)
    news_mentioned = models.ManyToManyField(News, blank=True)
class News(models.Model):
    title = models.CharField(max_length=250)
    abstract = models.TextField(max_length=1000, null=True, blank=True)
    description = models.TextField(max_length=5000, null=True, blank=True)
    url = models.URLField(unique=True)
    ne_person = models.CharField(max_length=250, blank=True, null=True)
news/models.py

class Person(models.Model):
    first_name = models.CharField(max_length=20)
    last_name = models.CharField(max_length=40)
    news_mentioned = models.ManyToManyField(News, blank=True)
class News(models.Model):
    title = models.CharField(max_length=250)
    abstract = models.TextField(max_length=1000, null=True, blank=True)
    description = models.TextField(max_length=5000, null=True, blank=True)
    url = models.URLField(unique=True)
    ne_person = models.CharField(max_length=250, blank=True, null=True)
从芹菜任务中提取:

article = News.objects.get(pk=pk)
person = Person.objects.update_or_create(first_name=fname, last_name=lname)
person.save()
mentioned = Person.objects.get(person.id).news_mentioned(article.id)
我这里做错了什么

模型是正确的。 要向需要使用
的人添加文章,请使用
.add()

美国

试着这样做:

article = News.objects.get(pk=pk)
person, created = Person.objects.update_or_create(first_name=fname, last_name=lname)
person.save()
person.news_mentioned.add(article)
mentioned = person.news_mentioned.all()
            person, created = Person.objects.update_or_create(first_name=fname, last_name=lname)
            person.save()
            article = News.objects.get(pk=pk)
            person.news_mentioned.add(article)
模型是正确的。 要向需要使用
的人添加文章,请使用
.add()

美国

试着这样做:

article = News.objects.get(pk=pk)
person, created = Person.objects.update_or_create(first_name=fname, last_name=lname)
person.save()
person.news_mentioned.add(article)
mentioned = person.news_mentioned.all()
            person, created = Person.objects.update_or_create(first_name=fname, last_name=lname)
            person.save()
            article = News.objects.get(pk=pk)
            person.news_mentioned.add(article)

我确实隔离了这个问题,结果发现这个问题与
update\u或\u create
有关,因为它返回一个元组

它现在的工作原理如下:

article = News.objects.get(pk=pk)
person, created = Person.objects.update_or_create(first_name=fname, last_name=lname)
person.save()
person.news_mentioned.add(article)
mentioned = person.news_mentioned.all()
            person, created = Person.objects.update_or_create(first_name=fname, last_name=lname)
            person.save()
            article = News.objects.get(pk=pk)
            person.news_mentioned.add(article)

我确实隔离了这个问题,结果发现这个问题与
update\u或\u create
有关,因为它返回一个元组

它现在的工作原理如下:

article = News.objects.get(pk=pk)
person, created = Person.objects.update_or_create(first_name=fname, last_name=lname)
person.save()
person.news_mentioned.add(article)
mentioned = person.news_mentioned.all()
            person, created = Person.objects.update_or_create(first_name=fname, last_name=lname)
            person.save()
            article = News.objects.get(pk=pk)
            person.news_mentioned.add(article)

是的,除了
add()
不返回任何内容之外,所以没有必要指定所提到的
;没有。对不起,我错了,我忽略了变量赋值。我更新了代码以显示如何从多对多字段检索数据。您也可以像在普通机型上一样使用
.filter()
,也尝试了
.add()
,但仍然不起作用。它拯救了人,但却没有创造与文章的关系。有什么想法吗?我想不出有什么理由不能这样做。也许可以尝试记录SQL语句,以便查看发生了什么?是的,除了
add()
不返回任何内容之外,所以没有必要指定所提到的
;没有。对不起,我错了,我忽略了变量赋值。我更新了代码以显示如何从多对多字段检索数据。您也可以像在普通机型上一样使用
.filter()
,也尝试了
.add()
,但仍然不起作用。它拯救了人,但却没有创造与文章的关系。有什么想法吗?我想不出有什么理由不能这样做。也许可以尝试记录SQL语句,以便查看发生了什么?您能否提供示例数据和输入或输出,以便任何想要帮助的人都可以看到数据错误的地方?它创建的是人,而不是文章中的人。您能否提供示例数据和输入或输出,以便任何想要帮助的人都可以看到数据错误的地方?它创造了人,但不是文章中的人。啊,当然!但是你应该得到一个
AttributeError
,而不是静静地失败。啊,当然!但是,您应该得到一个
AttributeError
,而不是静默失败。