0

我的网站上有一个表格,三个表格(家庭作业,班级,随机)都是一样的所以基本上我想在表格顶部制作一个ChoiceField,让用户选择上传文件的位置。

我在想,因为这些表有共同的抽象类,也许我可以从那里以某种方式选择它。但无法弄清楚如何。

或者可能有更好的解决方案。

以防万一这是我的代码:

#models.py
class FileDescription(models.Model):

    class Meta:
        abstract = True;
        ordering = ['file_creation_time']

    subject = models.ForeignKey('Subjects', null=True, blank=True, primary_key=True)
    subject_name = models.CharField(max_length=100)
    file_uploaded_by = models.CharField(max_length=100)
    file_name = models.CharField(max_length=100)
    file_description = models.TextField()
    file_creation_time = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return u'%s' % (self.file_name)

#template
<ul id="tabs">
        <li><a href="#homework">Homework</a></li>
        <li><a href="#class-papers">Class Papers</a></li>
        <li><a href="#random-papers">Random Papers</a></li>
    </ul>

    <div id="homework" class="tab-section">
        <h2>Homework</h2>
        <p>This section contains Homework</p>

            {% if homework_files %}
                <ul>
                    {% for file in homework_files %}
                        <li>{{ file.file_name }}
                    {% endfor %}
                </ul>
            {% endif %}
    </div>

#forms.py
class Homework_Content_Form(forms.ModelForm):
    class Meta:
        model=Homework_Content
        exclude=('subject', 
                 'subject_name',
                 'file_creation_time',
                 'file_uploaded_by',
                 )
4

1 回答 1

1

方法一:分开表格

class GenericContentForm(forms.ModelForm):
     class Meta:
         exclude = ('subject', 'subject_name', 'file_creation_time', 'file_uploaded_by')

     TYPE_CHOICES = (
         ('homework', 'Homework'),
         ('class', 'Class Paper'),
         ('random', 'Random Paper'),
     )

     type = forms.ChoiceField(choices=TYPE_CHOICES)

class HomeworkForm(GenericContentForm):
     class Meta(GenericContentForm.Meta):
         model = Homework

class ClassPaperForm(GenericContentForm):
     class Meta(GenericContentForm.Meta):
         model = ClassPaper

class RandomPaperForm(GenericContentForm):
     class Meta(GenericContentForm.Meta):
         model = RandomPaper

然后在您的视图中,您只需选择一个开始,当您拥有 POST 数据时,您可以实例化另一个:

def my_view(request):
    if request.method == 'POST':
        type = request.POST.get('type')
        if type == 'homework':
            form = HomeworkForm(request.POST)
        elif type == 'class':
            form = ClassPaperForm(request.POST)
        elif type == 'random':
            form = RandomPaperForm(request.POST)

    else:
        form = HomeworkForm()

    ...

方法 2:使用代理模型

由于这三个模型都共享相同的数据,因此拥有三个单独的表是多余的。与其FileDescription抽象,不如把它变成一个普通的标准模型,并为其添加一个类型字段,可选择“作业”、“课堂论文”和“随机论文”。然后为每个创建代理模型:

class HomeworkManager(models.Manager):
    def get_query_set(self, *args, **kwargs):
        qs = super(HomeworkManager, self).get_query_set(*args, **kwargs)
        return qs.filter(type='homework')

class Homework(FileDescription):
    class Meta:
        proxy = True

    objects = HomeworkManager()

    def save(self, *args, **kwargs):
        self.type = 'homework'
        super(Homework, self).save(*args, **kwargs)

然后,您只需要一个表单FileDescription来保存用户对“类型”的选择。然后,您可以使用标准访问任何设置为“家庭作业”类型的内容Homework.objects.all()

于 2012-07-25T15:54:52.987 回答