Filtrar Foreign Key

Asked

Viewed 129 times

1

Good afternoon...I’m trying to make a filter through Foreignkey, return only items that are not included in the registry of allocations, I’m using Select2...but I can’t filter...

I have 3 models: class, class and Allocate

want to make allocations from them, and in pop up, I just want to appear items that have not been allocated yet

model allocation


class Alocar(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT)
    data = models.DateField('Data', auto_now=True, blank=True)
    turma = models.ForeignKey(Turma, on_delete=models.PROTECT)
    sala = models.ForeignKey(Sala, on_delete=models.PROTECT)
    criada = models.DateTimeField('Criada', auto_now_add=True)
    alterada = models.DateTimeField('Alterada', auto_now=True)


model class

class Turma(models.Model):
    turma = models.CharField('Código', max_length=8, unique=True)
    curso = models.CharField('Curso', null=False, max_length=50)
    periodo = models.CharField('Periodo', null=False, max_length=50)
    disciplina = models.CharField('Disciplina', max_length=50)
    qtdalunos = models.PositiveSmallIntegerField('Qtd')
    professor = models.CharField('Professor', max_length=50)
    alocada = models.BooleanField('Alocada', default=False)
    internet = models.BooleanField('Internet', default=False)
    projetor = models.BooleanField('Projetor', default=False)
    computador = models.BooleanField('Computador', default=False)
    criada = models.DateTimeField('Criada', auto_now_add=True)
    alterada = models.DateTimeField('Alterada', auto_now=True)

model room

class Sala(models.Model):
    bloco = models.ForeignKey(Bloco, on_delete=models.PROTECT)
    sala = models.CharField('Sala', max_length=50, unique=True, help_text="Descrição")
    capmaxima = models.PositiveSmallIntegerField('Cap. Máxima:')
    disponivel = models.BooleanField('Disponivel', default=True)
    internet = models.BooleanField('Internet', default=False)
    projetor = models.BooleanField('Projetor', default=True)
    computador = models.BooleanField('Computador', default=False)
    criada = models.DateTimeField('Criada', auto_now_add=True)
    alterada = models.DateTimeField('Alterada ', auto_now=True)


form


from django_select2 import forms as s2forms
import select2.fields
import select2.models
from django_select2.forms import Select2Widget



class AddAlocForm(forms.ModelForm):


    class Meta:
        model = Alocar
        fields = ('turma','sala','dia','horario')

        widgets = {
            'turma': Select2Widget,
            'sala': Select2Widget,
            'dia': Select2Widget,
            'horario': Select2Widget,
        }

note the pop up, the featured item, is not to appear in the list,

filter pop up

  • If the items were not registered the data describing them exist where? Maybe you should rephrase your question.

  • Thanks, buddy... I took your advice and added more code...

  • How a room is allocated?

  • @Italo Lemos Good evening...on model allocatar.. there is the foreign key, the class and the room......

1 answer

0


Come on First point, no need django_select2 twice, just use as the documentation recommends:

from django_select2 import forms as s2forms

and then you call the widget that way

s2forms.Select2Widget

To filter the way you want, your class field is a Modelchoicefield so you can set a queryset for them as follows within the class of their form:

class AddAlocForm(forms.ModelForm):
    turma = forms.ModelChoiceField(queryset=Turma.objects.filter(alocada=False))

Only unallocated classes will appear in your field as an option.

Reference: Modelchoicefield

`

  • 1

    ....Good afternoon...even the CLASS attribute being a FOREIGNKEY in the model...I can do it this way?

  • Yes @Elvisdba any field that represents a relationship between two models it can be of type Modelchoicefield or Modelmultiplechoicefield.

  • Thank you my noble.... @Italo Lemos ...not wanting to abuse your good will.. could you help me in this other post???? ... Error rendering with Django Select2....

Browser other questions tagged

You are not signed in. Login or sign up in order to post.