Ad

How To Pull Variables Of The Main Class From A Subclass In Django

I have a filter code for the site:

def buy_files(request):
    bdfiles = FeedFile.objects.all()
    # bdfiles = UploadFile.objects.all()
    form = FileFilterForm(request.GET)
    if form.is_valid():
        if form.cleaned_data["number_course"]:
            bdfiles = bdfiles.filter(number_course = form.cleaned_data["number_course"])
        if form.cleaned_data["number_semestr"]:
            bdfiles = bdfiles.filter(number_semestr = form.cleaned_data["number_semestr"])
        if form.cleaned_data["subjectt"]:
             bdfiles = bdfiles.filter(subjectt = form.cleaned_data["subjectt"])
        if form.cleaned_data["type_materials"]:
             bdfiles = bdfiles.filter(type_materials = form.cleaned_data["type_materials"])
        if form.cleaned_data["institute"]:
            bdfiles = bdfiles.filter(institute = form.cleaned_data["institute"])
        
    return render(request, 'chat/files/buyfile.html', {'bdfiles': bdfiles, 'form':form})

And models.py:

 class UploadFile(models.Model):
        user = models.ForeignKey(User,on_delete = models.CASCADE,related_name='file_created' ,verbose_name='Автор')
        title = models.CharField(max_length=200, verbose_name='Заголовок')
        # uploadedfile = models.FileField(upload_to='files/',null=True, verbose_name='Файл')
        description = models.TextField(blank=True, verbose_name='Описание')
        createdtime = models.DateField(auto_now_add=True, db_index=True, verbose_name='Дата создания')
        price = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, verbose_name='Цена')
        number_course = models.IntegerField(null=True, blank=True, verbose_name='Курс')
        number_semestr = models.IntegerField(null=True, blank=True, verbose_name='Семестр')
        subjectt = models.CharField(max_length=200, null=True,verbose_name='Предмет')
        type_materials = models.CharField(max_length=200,null=True, verbose_name='Тип работы')
        institute = models.CharField(max_length=200, null=True, verbose_name='Институт')
    
        def __str__(self):
            return self.title
    
        class Meta:
            verbose_name = 'Загрузка файла'
            verbose_name_plural = 'Загрузка файлов'
    
    class FeedFile(models.Model):
        file = models.FileField(upload_to="files/")
        feed = models.ForeignKey(UploadFile, on_delete=models.CASCADE)

When I start entering data in the filter on the html page, an error occurs that there are no fields number_semestr, number_course and so on, but there are only fields feed, feed_id, file, id. HTML code of the page:

      <form action="" method="get" style="width:90%">
                     {% csrf_token %}
<!--                       {{form|crispy}}-->
                     <p><label class="form-label" for="{{ form.number_course.id_for_label }}">Курс: </label> {{ form.number_course }}</p>
                          <div class="form-error">{{ form.number_course.errors }}</div>
                     <p><label class="form-label" for="{{ form.number_semestr.id_for_label }}">Семестр: </label> {{ form.number_semestr }}</p>
                          <div class="form-error">{{ form.number_semestr.errors }}</div>
                    <p><label class="form-label" for="{{ form.subjectt.id_for_label }}">Дисциплина </label> {{ form.subjectt }}</p>
                          <div class="form-error">{{ form.subjectt.errors }}</div>
                     <p><select name = "type_materials" required class="form-select" aria-label="Тип материала">
                          <option selected>Тип материала</option>
                          <option value="Практические работы">Практические работы</option>
                          <option value="Лабораторные работы">Лабораторные работы</option>
                          <option value="Курсовые">Курсовые</option>
                             <option value="Дипломная работа">Дипломная работа</option>
                         <option value="Лекции">Лекции</option>
                          <option value="Диск с работами">Диск с работами</option>
                         <option value="Другое">Другое</option>
                        </select></p>
                     <p><select name = "institute" required class="form-select" aria-label="Институт">
                          <option selected>Институт</option>
                          <option value="ИВТИ">ИВТИ</option>
                          <option value="ГПИ">ГПИ</option>
                          <option value="ИЭЭ">ИЭЭ</option>
                             <option value="ИГВИЭ">ИГВИЭ</option>
                          <option value="ИнЭИ">ИнЭИ</option>
                          <option value="ИРЭ">ИРЭ</option>
                         <option value="ИЭТЭ">ИЭТЭ</option>
                          <option value="ИТАЭ">ИТАЭ</option>
                          <option value="ИЭВТ">ИЭВТ</option>
                             <option value="ЭнМИ">ЭнМИ</option>
                         <option value="Другой">Другой</option>
                        </select></p>
                          <div class="form-error">{{ form.institute.errors }}</div>
                    <button type="submit" class="btn btn-primary">Найти</button>
                </form>

How can I pull the variables of the uploadFile class from the Feedfile class?

Ad

Answer

You need to refer to your ForeignKey:

def buy_files(request):
    bdfiles = FeedFile.objects.all()
    # bdfiles = UploadFile.objects.all()
    form = FileFilterForm(request.GET)
    if form.is_valid():
        if form.cleaned_data["number_course"]:
            bdfiles = bdfiles.filter(feed__number_course = form.cleaned_data["number_course"])
        if form.cleaned_data["number_semestr"]:
            bdfiles = bdfiles.filter(feed__number_semestr = form.cleaned_data["number_semestr"])
        if form.cleaned_data["subjectt"]:
             bdfiles = bdfiles.filter(feed__subjectt = form.cleaned_data["subjectt"])
        if form.cleaned_data["type_materials"]:
             bdfiles = bdfiles.filter(feed__type_materials = form.cleaned_data["type_materials"])
        if form.cleaned_data["institute"]:
            bdfiles = bdfiles.filter(feed__institute = form.cleaned_data["institute"])
        
    return render(request, 'chat/files/buyfile.html', {'bdfiles': bdfiles, 'form':form})
Ad
source: stackoverflow.com
Ad