[英]Models.py Issue (Keys)
我正在嘗試為一個小型項目創建一個基本的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"
可能是您還需要添加
def __unicode__(self):
return u"%s" % (self. project_name)
到您的Project類(類似於“ __str__”函數)。 然后對Person也是一樣。
您的Project
類的__str__
方法似乎沒有正確縮進。 這使您看到Project object(1)
。
對於Person object(1)
,您尚未實現__str__
方法。
在任何django模型類定義中,您通常都__str__
在Bug
類中一樣實現並覆蓋django.db.models.model
__str__
方法。
這個__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中使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.