Filter for form in Django 1.9

1

I have a project where I create people and products, and at a certain moment I have a screen where I should look for the products that a person has in this way:

By clicking "move" I send the id of that row (it belongs to an Assigned Resource class) to a page where I must assign another person to the product like this:

The action of moving the object to a new owner works correctly for me but I want to know how to put a filter on the second form so that the previous owner of the object will be excluded from me.

I'm using views as functions:

models.py
class Persona(models.Model):
    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255)
class Recurso(models.Model):
    code = models.CharField(max_length=255)
    name = models.CharField(max_length=255)
class RecursoAsignado(models.Model):
    person_warehouse = models.ForeignKey(Persona)
    resource_warehouse = models.ForeignKey(Recurso)
forms.py
El form por el que pregunto:
class BuscarMoverForm(forms.Form):
    def __init__(self, *args, **kwargs):
        super(BuscarMoverForm, self).__init__(*args, **kwargs)
    person = forms.ModelChoiceField(required=True, queryset=Person.objects.all())
views.py
El view por el que pregunto:
def mover(request, idAsignado=None):
    assignment = None
    try:
        assignment = RecursoAsignado.objects.get(id=idAsignado)
    except Exception as e:
        pass
    if request.method == 'POST':
        form = BuscarMoverForm(request.POST)
        if form.is_valid():
            # Actual Registro
            personId = assignment.person_warehouse.id
            assignment.assigned = False
            assignment.save()
            # Nuevo Registro
            fecha_hora_actual = datetime.now()
            new_person = form.cleaned_data['person']
            new_assignment = ResourcesAssignment(
                person_warehouse=new_person,
                resource_warehouse=assignment.resource_warehouse,
                date_assignment=fecha_hora_actual,
                assigned=True,
            )
            new_assignment.save()
            return redirect("resourcestock:search_assignment", personId)
    else:
        form = BuscarMoverForm()

    template = loader.get_template('resourcestock/move_assignment.html')
    context = {
         'form': form,
    }
    return HttpResponse(template.render(context, request))

So my question is ... how can I modify the form in such a way that it captures or knows that data passed through url (the id of assigned resource) and can filter all the people except the current owner of the product (the person of that ResourceAssigned)?

    
asked by Diana Carolina Hernandez 27.08.2017 в 23:37
source

1 answer

1

As I mentioned @toledano add the .exclude, but first in your view you must pass the id,

else:
    form = BuscarMoverForm(id=idAsignado)

and in your forms

class BuscarMoverForm(forms.Form):
    person = forms.ModelChoiceField(required=True, queryset=None)

    def __init__(self, *args, **kwargs):
        self.id_asignado = kwargs.pop('id')
        super(BuscarMoverForm, self).__init__(*args, **kwargs)
        self.person.queryset = Person.objects.all().exclude(id=self.id_asignado)

let me know if it worked for you

    
answered by 29.08.2017 / 02:24
source