从外键关系子父子项中获取数据

时间:2018-05-21 10:20:28

标签: python django

我有像这样的数据库方案

database

models.py

class Employee(models.Model):
    name = models.CharField(max_length=255)

class Attendance(models.Model):
    date = models.DateField()
    employee = models.ForeignKey(Employee, related_name='attendance')

class Salary(models.Model):
    salary = models.DecimalField(max_digits=20, decimal_places=0)
    employee = models.ForeignKey(Employee, related_name='salary')

我有detailview的工资。

views.py

class Salary(DetailView):
    context_object_name = 'salary_detail'
    model = models.Salary
    template_name = 'system/salary_detail.html'

在我的template.html

我使用salary_detail.name来显示数据。

但是,我还需要显示用于生成attendance

工资的salary_template.html列表

怎么做?...

1 个答案:

答案 0 :(得分:0)

我认为您应该在from中设置salaryclass Employee(models.Model): name = models.CharField(max_length=255) class Attendance(models.Model): date = models.DateField() employee = models.ForeignKey(Employee, related_name='attendance') class Salary(models.Model): from_date = models.DateField() to_date = models.DateField() salary = models.DecimalField(max_digits=20, decimal_places=0) employee = models.ForeignKey(Employee, related_name='salary') employee.attendance.filter(date__gte=employee.salary.from_date, date__lte=employee.salary.to_date).values('dates') 日期,以便您可以跟踪哪个日期的工资,然后根据这样的内容过滤出勤率。< / p>

npm install material-ui@latest