gpt4 book ai didi

python - django admin 不显示值

转载 作者:太空宇宙 更新时间:2023-11-03 15:12:32 27 4
gpt4 key购买 nike

如何正确显示作者姓名? enter image description here

它只在那里显示一个“破折号”。

<小时/>

以下是我的代码。

admin.py
@admin.register(Solution)
class SolutionAdmin(admin.ModelAdmin):
def get_original_question(self, obj):
se = obj.question_code
return format_html(se.question)

def get_name(self, obj):
se = obj.user_id.name
return se

fieldsets = [
('question', {'fields': ['get_original_question']}),
('author', {'fields': ['get_name']}),
('solution', {'fields': ['body']}),

]
readonly_fields = ('get_original_question','get_name')
get_original_question.short_description = 'question'
get_name.short_description = 'author'

模型.py
class Question(models.Model):
question = RichTextField(blank=True, null=True)
question_code = models.CharField(max_length=15,unique=True)

class Solution(models.Model):
question_code = models.OneToOneField(Question, to_field='question_code', db_column='question_code')
user_id = models.ForeignKey('Users',to_field='id' ,db_column='ID')
body = RichTextField(blank=True, null=True)


class Users(models.Model):
id = models.AutoField(db_column='ID', primary_key=True)
name = models.CharField(db_column='Name', max_length=255, blank=True, null=True)

在 django shell 中:

>>> Solution.objects.get(pk=1490).user_id
Traceback (most recent call last):
File "/home/aaron/.virtualenvs/django/lib/python3.5/site-packages/django/db/models/fields/related_descriptors.py", line 178, in __get__
rel_obj = getattr(instance, self.cache_name)
AttributeError: 'Solution' object has no attribute '_user_id_cache'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/home/aaron/.virtualenvs/django/lib/python3.5/site-packages/django/db/models/fields/related_descriptors.py", line 184, in __get__
rel_obj = self.get_object(instance)
File "/home/aaron/.virtualenvs/django/lib/python3.5/site-packages/django/db/models/fields/related_descriptors.py", line 159, in get_object
return qs.get(self.field.get_reverse_related_filter(instance))
File "/home/aaron/.virtualenvs/django/lib/python3.5/site-packages/django/db/models/query.py", line 379, in get
self.model._meta.object_name
den.models.DoesNotExist: Users matching query does not exist.

更新:以下是我的解表enter image description here

更新:添加1490表enter image description here

更新:

>>> Solution.objects.get(pk=180).user_id
<Users: John>
>>> Solution.objects.get(pk=180).user_id.name
'John'
>>> Solution.objects.get(pk=1490).user_id.name
Traceback (most recent call last):
File "/home/aaron/.virtualenvs/django/lib/python3.5/site-packages/django/db/models/fields/related_descriptors.py", line 178, in __get__
rel_obj = getattr(instance, self.cache_name)
AttributeError: 'Solution' object has no attribute '_user_id_cache'

更新:user297 enter image description here

最佳答案

在解决方案模型中

class Solution(models.Model):
question_code = models.OneToOneField(Question, to_field='question_code', db_column='question_code')
user_id = models.ForeignKey('Users',to_field='id' ,db_column='ID')
body = RichTextField(blank=True, null=True)

user_id字段,我指向了错误的列,所以将db_column更改为user_id

user_id = models.ForeignKey('Users',to_field='id' ,db_column='user_id')

解决问题。

关于python - django admin 不显示值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44107021/

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