如何让用户选择需要更新哪个表Django

时间:2012-07-25 15:27:42

标签: django django-models django-forms django-templates

我的网站上有一个表格,三个表格相同(家庭作业,班级,随机) 所以基本上我想在表单的顶部创建一个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',
                 )

1 个答案:

答案 0 :(得分: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()访问任何设置为“homework”的内容。