gpt4 book ai didi

python - Models.py 问题( key )

转载 作者:行者123 更新时间:2023-11-29 06:35:40 27 4
gpt4 key购买 nike

我正在尝试为一个小项目制作一个基本的 Web 应用程序,但我遇到一个问题,每当我使用两个属性之间的任何类型的关系时。就像当我尝试从另一个表中提取一些数据时,它显示为“人员对象(1)”或“项目对象(1)”。如何确保不会发生这种情况并且显示人员姓名和项目名称?我也愿意接受任何形式的帮助来改进我的数据库代码。预先感谢您。

下面附上源代码。

当我尝试从另一个表中提取一些数据时,它显示为“人员对象(1)”或“项目对象(1)”。 如何确保不会发生这种情况并且显示人员姓名和项目名称?

从 django.db 导入模型

from django.db import models

from django.contrib.auth.models import User, Group
from django.db import models
from django.core.mail import EmailMessage
from django.contrib import admin


class Project(models.Model):
STATUS_CHOICE = (
('Work Assigned', 'Work Assigned'),
('Work in Progress', 'Work in Progress'),
('Testing', 'Testing'),
)
project_name = models.CharField(max_length=100)
project_description = models.CharField(max_length=100)
status_of_the_project = models.CharField(max_length=18, choices=STATUS_CHOICE)
created = models.DateTimeField(auto_now_add=True, null=True, blank=True)
finish_date = models.DateTimeField(null=True, blank=True)
supporting_documents = models.FileField(null=True, blank=True)
Admin_Name = models.CharField(max_length=100)


def __str__(self):
return self.project_name


class Meta:
verbose_name = "List of Projects"
verbose_name_plural = "List of Projects"


class Person(models.Model):
PERSON_TYPE = (
('Admin', 'Admin'),
('Project Manager', 'Project Manager'),
('Technician', 'Technician'),
('Tester', 'Tester')
)

user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='user_person')
projects = models.ManyToManyField(Project, null=True, related_name='people')
mail_id = models.EmailField(max_length=50, blank=True, null=True)
person_type = models.CharField(max_length=18, choices=PERSON_TYPE)

class Meta:
verbose_name = "User-Project Assignment"
verbose_name_plural = "User-Project Assignment"


class Bug(models.Model):
STATUS_CHOICE = (
('Unassigned', 'Unassigned'),
('Assigned', 'Assigned'),
('Testing', 'Testing'),
('Tested', 'tested'),
('Fixed', 'Fixed')
)
SITUATION_TYPE = (
('Bug', 'Bug'),
('Issue', 'Issue'),
('Enhancement', 'Enhancement'),
('Not an issue or bug', 'Not an issue or bug'),
('Fixed', 'Fixed')
)

project = models.ForeignKey(Project, on_delete=models.CASCADE)
issue_title = models.CharField(max_length=50, blank=True, null=True)
situation_type = models.CharField(max_length=25, choices=SITUATION_TYPE)
basic_description = models.CharField(max_length=100)
detailed_description = models.TextField(default='The Description, here.')
status = models.CharField(max_length=18, choices=STATUS_CHOICE)
assigned_to = models.ForeignKey(Person, on_delete=models.CASCADE, default=False)
reported_by = models.CharField(max_length=50, blank=True, null=True)
reporters_mail_id = models.EmailField(max_length=50, blank=True, null=True)
reported_date = models.DateTimeField(null=True, blank=True)
created = models.DateTimeField(auto_now_add=True, null=True, blank=True)
updated = models.DateTimeField(auto_now=True, null=True, blank=True)
deadline_date = models.DateTimeField(null=True, blank=True)
supporting_documents_by_reporter = models.FileField(null=True, blank=True)
project_managers_comment = models.TextField(default='The Description, here.')
supporting_documents_by_project_manager = models.FileField(null=True, blank=True)
technicians_comment = models.TextField(default='The Description, here.')
supporting_documents_by_technician = models.FileField(null=True, blank=True)
testers_comment = models.TextField(default='The Description, here.')
supporting_documents_by_tester = models.FileField(null=True, blank=True)
admin = models.ForeignKey(Person, on_delete=models.CASCADE, related_name='user_admin', default=False)

def __str__(self):
return '{} ({}) [{} {}]'.format(self.project, self.situation_type, self.status, self.issue_title)

class Meta:
verbose_name = "Project-Tasks"
verbose_name_plural = "Project-Tasks"

最佳答案

看来你的Project类(class)__str__方法没有正确缩进。这会让您看到 Project object(1) .

Person object(1)为例,您尚未实现__str__此类的方法。

在任何 django 模型类定义中,您通常希望实现并覆盖 django.db.models.model __str__方法就像您在 Bug 中所做的那样类(class)。

这个__str__方法规定模型在打印到流(例如 stdout)时如何呈现或者在本例中我假设由 Django 模板引擎呈现。

典型的用例如下(如 python3.6 shell 所示):

>>> class MyClass:
>>> def __init__(self, x):
>>> self.x = x
>>>
>>> def __str__(self):
>>> # a string formatted with this instances x value
>>> return "{}".format(self.x)
>>>
>>> my_instance = MyClass(1)
>>> print(my_instance)
>>> 1

请注意旧的 __unicode__方法。此方法的操作方式与__str__相同。但是在 python 2.x 中使用。

关于python - Models.py 问题( key ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53985762/

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