Models.py问题(键)

时间:2018-12-31 09:27:28

标签: python mysql django pycharm

我正在尝试为一个小型项目创建一个基本的Web应用程序,并且遇到一个问题,即每当我在2个属性之间使用任何类型的关系时,都会遇到问题。 就像当我尝试从另一个表中提取某些数据时,该数据显示为“人对象(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"

2 个答案:

答案 0 :(得分:1)

可能还需要添加

def __unicode__(self):
    return u"%s" % (self. project_name)

到您的Project类(类似于“ __str__”函数)。然后对Person也是一样。

答案 1 :(得分:1)

似乎您的Project类的__str__方法没有正确缩进。这使您看到Project object(1)

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

在任何django模型类定义中,您通常都想像在django.db.models.model类中一样实现并覆盖__str__ Bug方法。

__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中使用了该方法。