Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.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 从get_或_create result分配时,ForeignKey不保存_Python_Django_Django Models - Fatal编程技术网

Python 从get_或_create result分配时,ForeignKey不保存

Python 从get_或_create result分配时,ForeignKey不保存,python,django,django-models,Python,Django,Django Models,此代码将正常执行,但foreignkey记录不会保存在数据库中: new_org, created = models.Organization.objects.get_or_create(symbol = row['Symbol'], batch=batch_id) new_org.industry, created = models.DictIndustry.objects.get_or_create(value=row['industry'], batch=batch_id) new_org

此代码将正常执行,但
foreignkey
记录不会保存在数据库中:

new_org, created = models.Organization.objects.get_or_create(symbol = row['Symbol'], batch=batch_id)
new_org.industry, created = models.DictIndustry.objects.get_or_create(value=row['industry'], batch=batch_id)
new_org.sector, created = models.DictSector.objects.get_or_create(value=row['Sector'], batch=batch_id)
new_org.save()
print(new_org.__dict__)
输出具有指定的
行业
行业

{'_state': <django.db.models.base.ModelState object at 0x1142d0748>, 
'id': 3152, 
'date_created': datetime.datetime(2019, 2, 7, 21, 36, 55, 883816, tzinfo=<UTC>), 'date_updated': None,
'generation': 1,
'batch': 0,
'symbol': 'DDD',
'industry': <DictIndustry: Computer Software: Prepackaged Software>,
'sector': <DictSector: Technology>}
industry
sector
记录都能很好地进入数据库(postgresql)

我怀疑这与我编码模型的方式有关,但我看不出有什么东西会破坏它。是否有任何特定于postgresql的因素会导致这种情况

class BaseModel(models.Model):
    date_created = models.DateTimeField(auto_now=True)
    date_updated = models.DateTimeField(null=True, blank=True)
    generation = models.IntegerField(default=DATA_GENERATION)
    batch = models.IntegerField()

    class Meta:
        abstract = True


class AttributeBase(BaseModel):
    source = models.CharField(max_length=16, blank=False, null=False)

    def __str__(self):
        if hasattr(self, 'value'):
            return self.value
        raise NotImplementedError("value field not implemented for this model and it should have been")

    class Meta:
        abstract = True
        unique_together = ('source', 'parent', 'value', 'generation')


class DictBase(BaseModel):
    value = models.CharField(max_length=128, unique=True)

    class Meta:
        abstract = True

    def __str__(self):
        if hasattr(self, 'value'):
            return self.value
        raise NotImplementedError("value field not implemented for this model and it should have been")


class DictSector(DictBase):
    pass


class DictIndustry(DictBase):
    pass


class Organization(BaseModel):
    symbol = models.CharField(unique=True, max_length=12, null=False, blank=False)
    sector = models.ForeignKey(DictSector, on_delete=models.DO_NOTHING, null=True, blank=True)
    industry = models.ForeignKey(DictIndustry, on_delete=models.DO_NOTHING, null=True, blank=True)

    def __str__(self):
        return self.symbol


这与django无关,django工作正常。以防万一它会帮助别人。如果您在笔记本电脑(如Jupyter等)中运行django相关代码,请不要忘记重新加载内核,以便在运行过程中获取模型中的更改


(我尝试了自动重新加载,但效果不好)。

这与django无关,django工作正常。以防万一它会帮助别人。如果您在笔记本电脑(如Jupyter等)中运行django相关代码,请不要忘记重新加载内核,以便在运行过程中获取模型中的更改

(我尝试了自动重新加载,但效果不好)

class BaseModel(models.Model):
    date_created = models.DateTimeField(auto_now=True)
    date_updated = models.DateTimeField(null=True, blank=True)
    generation = models.IntegerField(default=DATA_GENERATION)
    batch = models.IntegerField()

    class Meta:
        abstract = True


class AttributeBase(BaseModel):
    source = models.CharField(max_length=16, blank=False, null=False)

    def __str__(self):
        if hasattr(self, 'value'):
            return self.value
        raise NotImplementedError("value field not implemented for this model and it should have been")

    class Meta:
        abstract = True
        unique_together = ('source', 'parent', 'value', 'generation')


class DictBase(BaseModel):
    value = models.CharField(max_length=128, unique=True)

    class Meta:
        abstract = True

    def __str__(self):
        if hasattr(self, 'value'):
            return self.value
        raise NotImplementedError("value field not implemented for this model and it should have been")


class DictSector(DictBase):
    pass


class DictIndustry(DictBase):
    pass


class Organization(BaseModel):
    symbol = models.CharField(unique=True, max_length=12, null=False, blank=False)
    sector = models.ForeignKey(DictSector, on_delete=models.DO_NOTHING, null=True, blank=True)
    industry = models.ForeignKey(DictIndustry, on_delete=models.DO_NOTHING, null=True, blank=True)

    def __str__(self):
        return self.symbol