Django:ForeignKey链接2个表

时间:2018-09-17 11:39:36

标签: python mysql django django-models

在这些代码行的Bugs表下,有一行代码可以读取

Assigned_to = models.ForeignKey(User, on_delete=models.CASCADE)

共有2个表,第一个是Project表,第二个是Bugs表。如何编辑这一行代码,并使之仅显示项目表中提到的人?

我已经尝试过了,但是如何写所有相关名称并确保该表中显示了第一张表中提到的所有名称?

 Assigned_to = models.ForeignKey(Project, on_delete=models.CASCADE, 
                                          related_name= ' ' | ' ' | ' ' )

很显然,使用'' | ''无效。

所有建议将不胜感激。

此外,由于某种原因,我为内联编写的代码行也没有显示。`对此问题的建议也将不胜感激。预先感谢!

from django.db import models

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

# 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.ForeignKey(User, on_delete=models.CASCADE, related_name='User.Admin_Name_users+')
   Admin_Mail_ID = models.EmailField(max_length=50)
   Project_Manager_1 = models.ForeignKey(User, on_delete=models.CASCADE, related_name='User.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='User.Project_Manager_2_users+', blank=True, null=True)
   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='User.Technician_1_users+')
   Technician_1_Mail_ID = models.EmailField(max_length=50)
   Technician_2 = models.ForeignKey(User, on_delete=models.CASCADE, related_name='User.Technician_2_users+', blank=True, null=True)
   Technician_2_Mail_ID = models.EmailField(max_length=50, blank=True, null=True)
   Technician_3 = models.ForeignKey(User, on_delete=models.CASCADE, related_name='User.Technician_3_users+', blank=True, null=True)
   Technician_3_Mail_ID = models.EmailField(max_length=50, blank=True, null=True)
   Tester_1 = models.ForeignKey(User, on_delete=models.CASCADE, related_name='User.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='User.Ad_1_users+', blank=True, null=True)
   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='User.Ad_1_users+', blank=True, null=True)
   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='User.Ad_1_users+', blank=True, null=True)
   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)

   class FlatPageAdmin(admin.ModelAdmin):
       fieldsets = (
           (None, {
               'fields': ('Project_Name','Project_Description','Admin_Name','Admin_Mail_ID','Project_Manager_1','Project_Manager_1_Mail_ID',
'Technician_1','Technician_1_Mail_ID','Tester_1','Tester_1_Mail_ID','Status_of_the_project','Created','Finish_Date','Supporting_Documents',
)
           }),
           ('Add More Users', {
               'classes': ('collapse',),
               'fields': ('Project_Manager_2','Project_Manager_2_Mail_ID','Technician_2','Technician_2_Mail_ID',
                          'Technician_3','Technician_3_Mail_ID','Additional_User_1','Additional_User_1_Type',
                          'Additional_User_1_Mail_ID','Additional_User_2','Additional_User_2_Type','Additional_User_2_Mail_ID',
                          'Additional_User_3','Additional_User_3_Type','Additional_User_3_Mail_ID'),
           }),
       )

   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 '{} ({})  [{}]'.format(self.Project, self.Situation_Type, self.Status, self.Issue_Title)


   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()

2 个答案:

答案 0 :(得分:1)

一些对您有很大帮助的事情:

  • 在代码中使用PEP8。例如,您的字段名称 应该全部小写,如下面的示例代码所示。
  • 了解有关database normalization的信息。您应该几乎从不 查看带编号的字段(即:IE:Additional_User_1,Additional_User_2, 数据库中的Additional_User_3)。理想情况下,这些应移至 单独的模型。
  • 不确定为什么在各处使用STATUS_CHOICE和STATUS_CHOICE_1。根据实际名称正确命名。
  • 您似乎对您的冗长名称有些困惑。请看一下我如何在下面的示例代码中使用这些名称。如有任何疑问,请告诉我。

我假设每个项目只有一名管理员,并且每个人仅担任角色(例如,如果一个人是技术员,则他/她只能是技术员)。如果这些假设不正确,则可能必须调整以下代码。

在下面的示例中,我为与项目相关联的各种人员创建了一个与User具有OneToOne关系的Person模型。与Project有一个ManyToMany关系(IE:每个人可以属于多个项目,每个项目可以有多个人)。最后,在Bug模型上,Assigned_to是Person的ForeignKey(可以将多个Bug分配给一个人)。

要将更多人添加到项目中,只需使用字段集即可。

这只是一个开始。我将为注释和文档等添加其他模型,例如,您可以从项目经理那里获得多个注释或文档。

$("pre").each(function(i){
  var iCharacters = $(this).text().length;
  $(this).prepend("<b class='charCount' style='color:red'>" + iCharacters + " </b>");
});

$("#showCounts").click(function() {
  $(".charCount").toggle();
});

答案 1 :(得分:0)

@FlyingTeller 尝试从以下声明中引用。 如果要创建外键关系,则需要添加另一个字段,就像我为ChannelSubscription类的subscribed_channel字段和Channel类字段的Subscribed_channels_list

添加一样
doctrine:database:create