Django Admin:电子邮件ID自动填充和Related_name =问题

时间:2018-08-26 13:22:15

标签: python django database

这是我在models.py中拥有的代码。我正在尝试仅使用Django Admin开发一个小型网站。

from django.db import models

# Create your models here.
from django.contrib.auth.models import User
from django.db import models
from django.core.mail import EmailMessage

# Create your models here.

class Project(models.Model):
   STATUS_CHOICE = (
       ('Project Manager', 'Project Manager'),
       ('Technician', 'Technician'),
       ('Tester', 'Tester')
   )
   STATUS_CHOICE_1 = (
       ('Work Assigned', 'Work Assigned'),
       ('Work in Progress', 'Work in Progress'),
       ('Testing', 'Testing'),
       ('Completed', 'Completed')
   )
   Project_Name = models.CharField(max_length=100)
   Project_Description = models.CharField(max_length=100)
   Admin_Name = models.CharField(max_length=100)
   Admin_Mail_ID = models.EmailField(max_length=50)
   Project_Manager_1 = models.ForeignKey(User, on_delete=models.CASCADE, related_name='Project_Manager_1_users')
   Project_Manager_1_Mail_ID = models.EmailField(max_length=50)
   Project_Manager_2 = models.ForeignKey(User, on_delete=models.CASCADE, related_name='Project_Manager_2_users')
   Project_Manager_2_Mail_ID = models.EmailField(max_length=50, blank=True, null=True)
   Technician_1 = models.ForeignKey(User, on_delete=models.CASCADE, related_name='Technician_1_users')
   Technician_1_Mail_ID = models.EmailField(max_length=50)
   Technician_2 = models.ForeignKey(User, on_delete=models.CASCADE, related_name='Technician_2_users')
   Technician_2_Mail_ID = models.EmailField(max_length=50, blank=True, null=True)
   Technician_3 = models.ForeignKey(User, on_delete=models.CASCADE, related_name='Technician_3_users')
   Technician_3_Mail_ID = models.EmailField(max_length=50, blank=True, null=True)
   Tester_1 = models.ForeignKey(User, on_delete=models.CASCADE, related_name='Tester_1_users')
   Tester_1_Mail_ID = models.EmailField(max_length=50, default='Example@gmail.com')
   Additional_User_1 = models.ForeignKey(User, on_delete=models.CASCADE, related_name='Ad_1_users')
   Additional_User_1_Type = models.CharField(max_length=18, choices=STATUS_CHOICE, blank=True, null=True)
   Additional_User_1_Mail_ID = models.EmailField(max_length=50, blank=True, null=True)
   Additional_User_2 = models.ForeignKey(User, on_delete=models.CASCADE, related_name='Ad_1_users')
   Additional_User_2_Type = models.CharField(max_length=18, choices=STATUS_CHOICE, blank=True, null=True)
   Additional_User_2_Mail_ID = models.EmailField(max_length=50, blank=True, null=True)
   Additional_User_3 = models.ForeignKey(User, on_delete=models.CASCADE, related_name='Ad_1_users')
   Additional_User_3_Type = models.CharField(max_length=18, choices=STATUS_CHOICE, blank=True, null=True)
   Additional_User_3_Mail_ID = models.EmailField(max_length=50, blank=True, null=True)
   Status_of_the_project = models.CharField(max_length=18, choices=STATUS_CHOICE_1)
   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)


   def __str__(self):
       return self.Project_Name

   class Meta:
       verbose_name_plural = "List Of Projects"

class Bug(models.Model):

   STATUS_CHOICE = (
       ('Unassigned', 'Unassigned'),
       ('Assigned', 'Assigned'),
       ('Testing', 'Testing'),
       ('Tested', 'tested'),
       ('Fixed', 'Fixed')
   )
   STATUS_CHOICE_1 = (
       ('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=STATUS_CHOICE_1)
   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(User, on_delete=models.CASCADE)
   Assigned_to_Mail_ID = models.EmailField(max_length=50, blank=True, null=True)
   Admin_Mail_ID = models.EmailField(max_length=50, blank=True, null=True)
   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)

   def __str__(self):
       return self.Project + ' (' + self.Situation_Type + ') ' + ' [' + self.Status + '] '


   def save(self, force_insert=False, force_update=False, using=None,
             update_fields=None):
       if self.id:
           user=self.Assigned_to
           self.Assigned_to_Mail_ID=user.email
       send_mail(self.Admin_Mail_ID, ass=self.Assigned_to_Mail_ID)
       super(Bug, self).save()

   class Meta:
       verbose_name_plural = "Projects Tasks/Issues"




def send_mail(admin,ass):
    email=EmailMessage('Changes made to Task','Changes have been made to one of your Task reports and we hereby request you to have a look at it at the earliest.', to=[admin,ass])
    email.send()

这是我已经工作了一段时间的项目。我在Django / Python中没有很多经验。

1)与数据库密钥相关的问题

当管理站点的用户使用下拉列表选择“用户名”时,如何提取用户的电子邮件ID并使其自动填写?

2)备用数据库设计思路

优化“用户”列表的最佳方法是什么?你们还有其他建议我(菜鸟)的方式来创建网站,以便可以添加其他用户,而不会限制该网站仅容纳3个其他用户吗?

3)related_name问题

为什么使用related_name=时出现以下错误?我该如何解决?

ERRORS:
Issues.Project.Additional_User_1: (fields.E304) Reverse accessor for 'Project.Additional_User_1' clashes with reverse accessor for 'Project.Additional_User_2'.
        HINT: Add or change a related_name argument to the definition for 'Project.Additional_User_1' or 'Project.Additional_User_2'.
Issues.Project.Additional_User_1: (fields.E304) Reverse accessor for 'Project.Additional_User_1' clashes with reverse accessor for 'Project.Additional_User_3'.
        HINT: Add or change a related_name argument to the definition for 'Project.Additional_User_1' or 'Project.Additional_User_3'.
Issues.Project.Additional_User_1: (fields.E305) Reverse query name for 'Project.Additional_User_1' clashes with reverse query name for 'Project.Additional_User_2'.
        HINT: Add or change a related_name argument to the definition for 'Project.Additional_User_1' or 'Project.Additional_User_2'.
Issues.Project.Additional_User_1: (fields.E305) Reverse query name for 'Project.Additional_User_1' clashes with reverse query name for 'Project.Additional_User_3'.
        HINT: Add or change a related_name argument to the definition for 'Project.Additional_User_1' or 'Project.Additional_User_3'.
Issues.Project.Additional_User_2: (fields.E304) Reverse accessor for 'Project.Additional_User_2' clashes with reverse accessor for 'Project.Additional_User_1'.
        HINT: Add or change a related_name argument to the definition for 'Project.Additional_User_2' or 'Project.Additional_User_1'.
Issues.Project.Additional_User_2: (fields.E304) Reverse accessor for 'Project.Additional_User_2' clashes with reverse accessor for 'Project.Additional_User_3'.
        HINT: Add or change a related_name argument to the definition for 'Project.Additional_User_2' or 'Project.Additional_User_3'.
Issues.Project.Additional_User_2: (fields.E305) Reverse query name for 'Project.Additional_User_2' clashes with reverse query name for 'Project.Additional_User_1'.
        HINT: Add or change a related_name argument to the definition for 'Project.Additional_User_2' or 'Project.Additional_User_1'.
Issues.Project.Additional_User_2: (fields.E305) Reverse query name for 'Project.Additional_User_2' clashes with reverse query name for 'Project.Additional_User_3'.
        HINT: Add or change a related_name argument to the definition for 'Project.Additional_User_2' or 'Project.Additional_User_3'.
Issues.Project.Additional_User_3: (fields.E304) Reverse accessor for 'Project.Additional_User_3' clashes with reverse accessor for 'Project.Additional_User_1'.
        HINT: Add or change a related_name argument to the definition for 'Project.Additional_User_3' or 'Project.Additional_User_1'.
Issues.Project.Additional_User_3: (fields.E304) Reverse accessor for 'Project.Additional_User_3' clashes with reverse accessor for 'Project.Additional_User_2'.
        HINT: Add or change a related_name argument to the definition for 'Project.Additional_User_3' or 'Project.Additional_User_2'.
Issues.Project.Additional_User_3: (fields.E305) Reverse query name for 'Project.Additional_User_3' clashes with reverse query name for 'Project.Additional_User_1'.
        HINT: Add or change a related_name argument to the definition for 'Project.Additional_User_3' or 'Project.Additional_User_1'.
Issues.Project.Additional_User_3: (fields.E305) Reverse query name for 'Project.Additional_User_3' clashes with reverse query name for 'Project.Additional_User_2'.
        HINT: Add or change a related_name argument to the definition for 'Project.Additional_User_3' or 'Project.Additional_User_2'.

请记住,我仍然是一个初学者,并且对这个主题没有很多知识。您可能需要对我更具描述性才能理解您的意思。 非常感谢!

1 个答案:

答案 0 :(得分:0)

问题3的答案如下:

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

首先使用表名,然后使用.,然后最后使用+