Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.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/24.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外键根据模型上的另一个字段自动填充_Python_Django_Django Models - Fatal编程技术网

Python Django外键根据模型上的另一个字段自动填充

Python Django外键根据模型上的另一个字段自动填充,python,django,django-models,Python,Django,Django Models,所以我现在有一个“Account”模型和“Account Comments”模型——请记住,我不能真正控制数据库的方案,我正在围绕现有数据库编写一个包装器 在AccountComments模型下,有一个名为“数据”的字段。这就是AccountComments的实际位置,我试图在Account模型上创建一个外键,而不需要重新设计并在Account上添加一个保存AccountID的“AccountComments”字段 class Account(models.Model): Account

所以我现在有一个“Account”模型和“Account Comments”模型——请记住,我不能真正控制数据库的方案,我正在围绕现有数据库编写一个包装器

在AccountComments模型下,有一个名为“数据”的字段。这就是AccountComments的实际位置,我试图在Account模型上创建一个外键,而不需要重新设计并在Account上添加一个保存AccountID的“AccountComments”字段

class Account(models.Model):
    AccountID = models.AutoField(editable=False, db_column='AccountID', verbose_name='ID',
                             primary_key=True) 
    acctno = models.IntegerField(editable=True, unique=True, db_column='ACCTNO', verbose_name='Acct #', blank=True,
                             null=True)  # Field name made lowercase.
    accountComments = models.ForeignKey('accountComments',to_field='accountID',db_column='Data')

    def __str__(self):
        return str(self.acctno)

    class Meta:
        managed = False
        db_table = 'Account'


class accountComments(models.Model):
    accountCommentID = models.AutoField(db_column='AccountCommentID', primary_key=True)  # Field name made lowercase.
    accountID = models.IntegerField(db_column='AccountID')  # Field name made lowercase.
    EntryUserID = models.IntegerField(db_column='EntryUserID')  # Field name made lowercase.
    EntryStamp  = models.DateTimeField(db_column='EntryStamp', )  # Field name made lowercase.
    Data = models.TextField(db_column='Data')  # Field name made lowercase.
    guid = models.CharField(db_column='guid', max_length=255)  # Field name made lowercase.
    class Meta:
        managed = False
        db_table = 'AccountComment'
最后,希望帐户模型上的accountComments使用“AccountID”查找accountComments.AccountID,然后返回“数据”

我知道我可以用

def accountComments(self):
    return str(accountComment.objects.get(accountID = self.AccountID).Data)
但我希望它能与Django Admin一起工作,所以我需要它成为模型的一个集成部分


如果有人能给我指出正确的方向,谢谢。

你用外键做的太多了。Django中的外键后面应该返回模型实例,而不是模型实例中的特定字段

db_列
应该是
Account
表中存储id的列,例如

accountComments = models.ForeignKey('accountComments',to_field='accountID',db_column='AccountID')
然后,要获取特定
帐户
实例的
数据
,您可以执行以下操作:

account.accountComments.Data

你不是把fk搞错了吗?多条评论不应该与一个帐户相关,反之亦然吗?我正在从2000 ASP.net迁移,在那里所有的评论都是一个巨大的文本字符串。因此,没有。最终,如果我能够在帐户上创建另一个名为“accountComment”的字段,并将accountID从所有现有字段复制到“accountComment”,并使用外键反转accountComment模型str返回数据,我就会看到这一点。。我只是觉得必须有一种方法来定义外键查找是基于该模型上的另一个字段。哦,我太蠢了。。是的——我没有理由不能将db_列定义为同一事物两次!!我会很快试试这个。