gpt4 book ai didi

python - 我收到错误消息 : TypeError: unsupported operand type(s) for %: 'DeferredAttribute' and 'dict' in django

转载 作者:行者123 更新时间:2023-12-01 06:54:03 26 4
gpt4 key购买 nike

我在 django Rest freamework 应用程序中创建了新模型,但收到此错误消息:

类型错误:% 不支持的操作数类型:“DeferredAttribute”和“dict”

这是错误截图: error screenshot

我不明白问题出在哪里?

这是我的models.py:

from django.db import models

class TblUserAccounts(models.Model):
uid = models.AutoField(primary_key=True)
username = models.CharField(unique=True, max_length=20)
alias_username = models.CharField(unique=True, max_length=20, blank=True, null=True)

class Meta:
managed = False
db_table = 'tbl_user_accounts'
ordering = ['uid']


class TblUserDetails(models.Model):
detail_id = models.IntegerField(primary_key=True)
useraccount = models.ForeignKey(TblUserAccounts, models.DO_NOTHING, related_name=TblUserAccounts.uid)
first_name = models.CharField(max_length=25, blank=True, null=True)
last_name = models.CharField(max_length=45, blank=True, null=True)
birthdate = models.DateTimeField(blank=True, null=True)
record_time = models.DateTimeField()
creator = models.ForeignKey(TblUserAccounts, models.DO_NOTHING, related_name=TblUserAccounts.uid, db_column='creator')

class Meta:
managed = False
db_table = 'tbl_user_details'
ordering = ['record_time']


class TblUserPassword(models.Model):
id_password = models.AutoField(primary_key=True)
useraccount_id_pwd = models.ForeignKey(TblUserAccounts, models.DO_NOTHING, related_name=TblUserAccounts.uid, db_column='useraccount_id_pwd')
salt = models.CharField(max_length=200, blank=True, null=True)
hash = models.CharField(max_length=200, blank=True, null=True)
record_time = models.DateTimeField()
creator = models.ForeignKey(TblUserAccounts, models.DO_NOTHING, related_name=TblUserAccounts.uid, db_column='creator')

class Meta:
managed = False
db_table = 'tbl_user_password'
ordering = ['record_time']

当然,你应该知道我是 Python 编程新手。

我猜问题出在 TblUserDetails 模型中,但我不知道它是什么?

非常感谢您的关注。

最佳答案

在无法测试的情况下,我真的只能在黑暗中试一试。不过,经过一些阅读,我认为问题在于您使用的 lated_name 。请尝试以下操作:

class TblUserDetails(models.Model):
detail_id = models.IntegerField(primary_key=True)
useraccount = models.ForeignKey(TblUserAccounts, models.DO_NOTHING, related_name='TblUserAccounts.uid')
first_name = models.CharField(max_length=25, blank=True, null=True)
last_name = models.CharField(max_length=45, blank=True, null=True)
birthdate = models.DateTimeField(blank=True, null=True)
record_time = models.DateTimeField()
creator = models.ForeignKey(TblUserAccounts, models.DO_NOTHING, related_name='TblUserAccounts.uid', db_column='creator')

class Meta:
managed = False
db_table = 'tbl_user_details'
ordering = ['record_time']

请注意,如果这是问题所在,您还需要对 TblUserPassword 进行此修复。

我认为lated_name不应该指向另一个类的属性,但它只是一个名称字段:

ForeignKey.related_name

The name to use for the relation from the related object back to this one. It’s also the default value for related_query_name (the name to use for the reverse filter name from the target model). See the related objects documentation for a full explanation and example. Note that you must set this value when defining relations on abstract models; and when you do so some special syntax is available.

If you’d prefer Django not to create a backwards relation, set related_name to '+' or end it with '+'. For example, this will ensure that the User model won’t have a backwards relation to this model.

user = models.ForeignKey(
User,
on_delete=models.CASCADE,
related_name='+', )

如果这不是问题,请告诉我,我会看看是否可以找到其他东西。

关于python - 我收到错误消息 : TypeError: unsupported operand type(s) for %: 'DeferredAttribute' and 'dict' in django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58882659/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com