Django形式:外键的参考字段

时间:2019-03-24 17:09:21

标签: python django django-models django-forms django-model-field

我正在制作a task tracker webapp(也可以使用the full source code),并且我有一个数据库结构,其中每个任务都有标题,描述和一些实例,每个实例都可以标记为不完整/不完整:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def plot(data):
    fig = plt.figure(figsize=(10,10))
    ax = fig.gca(projection='3d')

    for i in range(data.shape[0]):
        for j in range(data.shape[1]):
            ax.scatter([i]*data.shape[0], [j]*data.shape[1], 
            [i for i in range(data.shape[2])], 
                   c=['r' if i == 0 else 'b' for i in data[i,j]], s=50)

plot(data)
plt.show()
plt.close('all')
plot(v)
plt.show()

任务和实例可以分别共享,尽管对实例的访问应隐含对任务的读取访问。这是针对教室情况的,教师可以创建任务并将其分配给学生。

class Task(models.Model):
    title = OneLineTextField()
    description = models.TextField(blank=True)


class TaskInstance(models.Model):
    task = models.ForeignKey(Task, on_delete=models.CASCADE)
    is_complete = models.BooleanField()

我的问题是如何为class TaskPermission(models.Model): task = models.ForeignKey(Task, on_delete=models.CASCADE, related_name='permissions') user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='task_permissions_granted') shared_by = models.ForeignKey(User, on_delete=models.PROTECT, null=True, related_name='task_permissions_granting') can_edit = models.BooleanField(default=False) class Meta: unique_together = 'task', 'user', 'shared_by', class TaskInstancePermission(models.Model): task_instance = models.ForeignKey(TaskInstance, on_delete=models.CASCADE, related_name='permissions') user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='task_instance_permissions_granted') shared_by = models.ForeignKey(User, on_delete=models.PROTECT, null=True, related_name='task_instance_permissions_granting') can_edit = models.BooleanField(default=False) class Meta: unique_together = 'task_instance', 'user', 'shared_by', 创建表单,并为其TaskInstance及其is_complete的{​​{1}}和Task字段。这样的事情行吗?还是我需要实现自己的titledescription方法?

save

1 个答案:

答案 0 :(得分:0)

我认为inlineformset_factory是我想要的!

实际上,它似乎没有用:它用于相同类型的多种形式,而不是不同类型...