1

So I am new to Django and I have been reading a lot of documentation to figure this out, I have a table called "Logs" that has logs of different positions (has FK of table "Position"), each position belongs to a department (has FK to table "Department") Check the image below :1

What I want to do is create a view just like this one : 2

and whenever you click on a department, it extends all the positions in it with their respective logs like this : 3

The Screenshots I have attached are my work in main app (or if you would like to call it front end), I wanted to replicate the same process in the Django Admin page, I keep seeing that I should use inlines but I can't seem to make it work, can someone help or put me in the right direction please ? much appreciated.

Here is what I have in my models.py :

from django.db import models
from django.contrib.auth.models import User
from PIL import Image


class Site(models.Model):
    site = models.CharField(max_length=200, blank=True, null=True)
    totalHC = models.IntegerField(blank=True, null=True)

    def __str__(self):
        return self.site


class Department(models.Model):
    department = models.CharField(max_length=200, blank=True)

    def __str__(self):
        return self.department


class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    site = models.ForeignKey(Site, on_delete=models.CASCADE, null=True, default=Site(id="1").site)
    department = models.ForeignKey(
        "Department", on_delete=models.CASCADE, null=True)
    image = models.ImageField(default='default.jpg', upload_to='profile_pics')
    bio = models.CharField(max_length=2000, blank=True)
    skills = models.CharField(max_length=2000, blank=True)
    aoi = models.CharField(max_length=2000, blank=True)
    github = models.CharField(max_length=200, blank=True)
    linkedin = models.CharField(max_length=200, blank=True)

    def __str__(self):
        return f'{self.user.username} Profile'

    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)
        img = Image.open(self.image.path)
        if img.height > 300 or img.width > 300:
            output_size = (300, 300)
            img.thumbnail(output_size)
            img.save(self.image.path)


class Grade(models.Model):
    user = models.OneToOneField(Profile, on_delete=models.CASCADE)
    ut1 = models.CharField(max_length=200, blank=True)
    ut2 = models.CharField(max_length=200, blank=True)
    ut3 = models.CharField(max_length=200, blank=True)

    ut1p = models.ImageField(upload_to='plots', blank=True)
    ut2p = models.ImageField(upload_to='plots', blank=True)
    ut3p = models.ImageField(upload_to='plots', blank=True)

    ut1pb = models.ImageField(upload_to='plots', blank=True)
    ut2pb = models.ImageField(upload_to='plots', blank=True)
    ut3pb = models.ImageField(upload_to='plots', blank=True)

    ut12 = models.ImageField(upload_to='plots', blank=True)
    ut13 = models.ImageField(upload_to='plots', blank=True)
    ut23 = models.ImageField(upload_to='plots', blank=True)


class Section(models.Model):
    class Meta:
        verbose_name = 'Department'
        verbose_name_plural = 'Departments'

    section = models.CharField(max_length=200, blank=True)

    def __str__(self):
        return self.section


class Question(models.Model):
    class Meta:
        verbose_name = 'Position'
        verbose_name_plural = 'Positions'

    section = models.ForeignKey(
        "Section", on_delete=models.CASCADE, null=True, blank=True)
    question_field = models.CharField(max_length=2000, blank=True, null=True)

    def __str__(self):
        return self.question_field


class Answer(models.Model):
    class Meta:
        verbose_name = 'Log'
        verbose_name_plural = 'Logs'

    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    user = models.ForeignKey(Profile, on_delete=models.CASCADE)
    answer_field = models.CharField(max_length=2000, blank=True, null=True)

    def __str__(self):
        return f"{self.user} answered {self.answer_field}"


class Position1(models.Model):
    class Meta:
        verbose_name = 'Position'
        verbose_name_plural = 'Positions'

    department = models.ForeignKey(
        "Department", on_delete=models.CASCADE, null=True, blank=True)
    position = models.CharField(max_length=200, blank=True)
    jobID = models.CharField(max_length=200, blank=True)

    class HCtype(models.TextChoices):
        Staff = 'Staff', ('Staff')
        IDL = 'IDL', ('IDL')
        DL = 'DL', ('DL')

    hctype = models.CharField(
        max_length=5,
        choices=HCtype.choices,

    )

    def __str__(self):
        return self.position


class Log(models.Model):
    position = models.ForeignKey(Position1, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    site = models.ForeignKey(Site, on_delete=models.CASCADE)
    INN = models.IntegerField(blank=True, null=True)
    OUT = models.IntegerField(blank=True, null=True)
    date = models.CharField(max_length=200, blank=True)
    internal = models.IntegerField(default=0, null=True)


class SiteHasPosition(models.Model):
    date = models.CharField(max_length=200, blank=True)
    site = models.ForeignKey(Site, on_delete=models.CASCADE)
    position = models.ForeignKey(Position1, on_delete=models.CASCADE)
    value = models.IntegerField(blank=True, null=True)
    standard = models.IntegerField(blank=True, null=True)
    turn_over = models.IntegerField(blank=True, null=True)


class SiteHasDepartment(models.Model):
    date = models.CharField(max_length=200, blank=True)
    site = models.ForeignKey(Site, on_delete=models.CASCADE)
    department = models.ForeignKey(Department, on_delete=models.CASCADE)
    value = models.IntegerField(blank=True, null=True)


class SiteKPIs(models.Model):
    site = models.ForeignKey(Site, on_delete=models.CASCADE)
    date = models.CharField(max_length=200, blank=True)
    staff = models.IntegerField(blank=True, null=True)
    dl = models.IntegerField(blank=True, null=True)
    idl = models.IntegerField(blank=True, null=True)
    total_hc = models.IntegerField(blank=True, null=True)
    total_in = models.IntegerField(blank=True, null=True)
    total_out = models.IntegerField(blank=True, null=True)
    staff_rate = models.IntegerField(blank=True, null=True)
    dl_rate = models.IntegerField(blank=True, null=True)
    idl_rate = models.IntegerField(blank=True, null=True)

Here is how I registred them in admin.py : 

admin.site.register(Profile)
admin.site.register(Log)
admin.site.register(Position1)
admin.site.register(Department)
admin.site.register(Site)
admin.site.register(SiteHasDepartment)
admin.site.register(SiteHasPosition)

I would like to have a page in admin.py where I can select a site and for that specific site display : all the departments(when you press a dpt all the positions will expand) for each position the standardHC, attributes from the Log table (that match that position,and that site) and attributes from SiteHasPosition( that match the site and that position)

I hope I made it clearer

Mar1941
  • 91
  • 1
  • 10
  • 1
    Hi, please add the code for your models and how you added them to the admin. – gdef_ Apr 08 '21 at 15:17
  • I am unable to right the code as it is too long to be in a comment @gdef_ – Mar1941 Apr 08 '21 at 16:40
  • class Log(models.Model): position = models.ForeignKey(Position1, on_delete=models.CASCADE) user = models.ForeignKey(User, on_delete=models.CASCADE) site = models.ForeignKey(Site, on_delete=models.CASCADE) INN = models.IntegerField(blank=True, null=True) OUT = models.IntegerField(blank=True, null=True) date = models.CharField(max_length=200, blank=True) internal = models.IntegerField(default=0, null=True) def __str__(self): return self.departmentadmin.py admin.site.register(Log) admin.site.register(Position1) admin.site.register(Department) – Mar1941 Apr 08 '21 at 16:41
  • @Mar1941 Edit the code into your question. – M-Chen-3 Apr 08 '21 at 17:28
  • 1
    Done @M-Chen-3 thank you – Mar1941 Apr 08 '21 at 17:50

0 Answers0