I have a project where I create people and products, and at a given moment I have a screen where I have to search for the products that a person has in this way:
By clicking "move" I send the id of that row (belongs to an AssignedResource 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 excludes me.
I am using views as functions:
models.pyclass 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 do I modify the form so that it captures or knows that data passed by url (the AssignedResource id) and can filter all the people except the current owner of the product (the person of that AssignedResource )?