Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.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 Django TypeError:';酒吧';此函数的关键字参数无效_Python_Django_Mongodb - Fatal编程技术网

Python Django TypeError:';酒吧';此函数的关键字参数无效

Python Django TypeError:';酒吧';此函数的关键字参数无效,python,django,mongodb,Python,Django,Mongodb,所以,我曾经在同一个应用程序中有两个模型运行良好,但当我将两个模型分别移动到不同的类时,我似乎无法让ForeignKey正常工作。所讨论的模式: from app1.models import Bar class Foo(models.Model): name = models.CharField('...............') bar = models.ForeignKey(Bar, editable=False, verbose_name=_('Bar')) 但当我

所以,我曾经在同一个应用程序中有两个模型运行良好,但当我将两个模型分别移动到不同的类时,我似乎无法让ForeignKey正常工作。所讨论的模式:

from app1.models import Bar

class Foo(models.Model):
    name = models.CharField('...............')
    bar = models.ForeignKey(Bar, editable=False, verbose_name=_('Bar'))
但当我尝试时:

>>>f = Foo(name='name', bar=existing_bar).save()
我明白了

TypeError: 'bar' is an invalid keyword argument for this function
我正在使用mongodb和django。我不明白为什么当他们在同一个应用程序上时,它能完美地工作,但现在不行了:/


我注意到在我的旧收藏中,foo文档有一个bar_id字段,现在没有了,不管它值多少钱。

我不太清楚为什么会发生这种情况,django文档中可能会提到一些东西,但现在你可以提供一个
\u init\u
来让它继续工作-

对于python 3+

class Foo(models.Model):
    name = models.CharField('...............')
    bar = models.ForeignKey(Bar, editable=False, verbose_name=_('Bar'))

    def __init__(self, *args, bar=None, **kwargs):
        super().__init__(*args, **kwargs)
        self.bar = bar
对于python 2.6+

 class Foo(models.Model):
    name = models.CharField('...............')
    bar = models.ForeignKey(Bar, editable=False, verbose_name=_('Bar'))

    def __init__(self, *args, bar=None, **kwargs):
        super(Foo, self).__init__(*args, **kwargs)
        self.bar = bar

你能提供你的实际模型而不是模糊的版本吗?