Django Admin,我的表的所有内容都没有显示和内联问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django Admin,我的表的所有内容都没有显示和内联问题相关的知识,希望对你有一定的参考价值。

下面是我用于我的项目的models.py,

我也附上了这个问题的照片。

问题1

由于某种原因,表中的所有内容都没有显示。在第一个表中,第一个内容丢失了。在第二个表中,第一个和第二个内容丢失

问题2

内联函数不起作用。我尝试通过文档和几个YouTube视频来了解如何处理这种情况,但它没有多大帮助。

问题3

对于错误表,当我选择项目名称时,如何确保只允许选择我添加到该项目表中的人员?

问题4

有没有办法从“用户”页面中提取电子邮件ID,当我在“项目”页面中选择用户名时,电子邮件ID会自动填写吗?

同样,在问题页面中,当我选择用户时,将自动输入电子邮件ID。

enter image description here enter image description here

models.朋友

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

我还附上了admin.py文件。 Admin.py

from django.contrib import admin
from .models import Bug, Project
from django.contrib.admin.models import LogEntry
admin.site.register(LogEntry)


# Register your models here.


class BugDisplay(admin.ModelAdmin):
    list_display = ('Project', 'Status', 'Basic_Description', 'Assigned_to', 'Created', 'Updated', 'Issue_Title')
    list_filter = ('Status', 'Assigned_to', 'Project')
    search_fields = ('Reporters_Mail_ID', 'Reported_by', 'Basic_Description',)



admin.site.register(Bug, BugDisplay)


# Register your models here.

@admin.register(Project)
class ProjectDisplay(admin.ModelAdmin):
    list_display = ('Project_Name','Admin_Name', 'Project_Manager_1', 'Status_of_the_project')
    list_filter = ('Admin_Name', 'Status_of_the_project')
    search_fields = ('Project_Name', 'Project_Description', 'Admin_Name', 'Admin_Mail_ID', 'Project_Manager_1 '
                        'Project_Manager_1_Mail_ID', 'Project_Manager_2 ', 'Project_Manager_2_Mail_ID',
                        'Technician_1',
                        'Technician_1_Mail_ID', 'Technician_2', 'Technician_2_Mail_ID', 'Technician_3',
                        'Technician_3_Mail_ID', 'Tester_1', 'Tester_1_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', 'Status_of_the_project', 'Created',
                        'Finish_Date', 'Supporting_Documents')
答案

根据您的问题,请提供您的管理员内联课程(参见问题2),请详细说明问题1并提供更多详细信息。

关于问题2,您可以使用this覆盖相应的字段。您可以将自定义查询集(通过应用过滤条件)提供给外键字段。

def formfield_for_foreignkey(self, db_field, request, **kwargs):
    if db_field.name == 'your_field_name':
        kwargs["queryset"] = your_custom_queryset_based_on_conditions

    return super().formfield_for_foreignkey(db_field, request, **kwargs)

对于问题4,您希望根据用户选择自动填写电子邮件ID。这可以使用自定义javascript实现。

另一种方法是,您不显示电子邮件字段,在保存项目/问题管理员的记录时,您会自动从用户表中获取电子邮件ID并将其分配给对象并保存。您可以查看this,从管理面板保存对象时进行自定义。

def save_model(self, request, obj, form, change):
    email = your_method_to_find_email_for_user(obj.user_field) 
    obj.email_field = email
    super().save_model(request,obj,form,change)

以上是关于Django Admin,我的表的所有内容都没有显示和内联问题的主要内容,如果未能解决你的问题,请参考以下文章

django-19.admin后台自定义显示

带有空白表的 Django 2.1 admin.py changeform_view 方法错误

Django admin:仅显示各个用户的记录

Django Admin 样式表不会加载到生产服务器上

Django / sqlite3“OperationalError:没有这样的表”关于线程操作

django python - 关系不存在