0
votes

I'm trying to get from data base only the data for current loggin user and not all data.

There are two models, a Campos model that could have many Lotes model. Each "Campos" and "Lote" are from only one user.

When I'm posting the Lote the template must filter choices for "Campos" for request.user only and not all database.

Here is the code

model.py

class Campos (models.Model):
    name = models.CharField(max_length=30)
    user = models.ForeignKey(User, on_delete=models.CASCADE)  
  
    def __str__(self):
        return   self.name

class Lote (models.Model):
    name = models.CharField(max_length=30)  
    user = models.ForeignKey(User, on_delete= models.CASCADE)
    has = models.PositiveIntegerField()
    campo = models.ForeignKey(Campos, on_delete=models.CASCADE)

    def __str__(self):
        return   self.name

** In Fomrs.py I can't query, it isn't working. Just cant filter choices in the template **

  • def init(self, user, *args, **kwargs):

  • super(LoteForm, self).init(*args, **kwargs)

  • self.fields['campo'].queryset = Campos.objects.filter(user=user)

forms.py

class LoteForm (forms.ModelForm):
    
    class Meta:
        model = Lote
        fields = [ 'campo','name','has']
        labels = {
            'campo': _('campo'),
            'name': _('Lote'),
            'has': _('Hectareas'),
        }
        def __init__(self, user, *args, **kwargs):
            super(LoteForm, self).__init__(*args, **kwargs)
            self.fields['campo'].queryset = Campos.objects.filter(user=user)

views.py

def campos(request):
    ...

    if request.method =='POST' and 'btn2' in request.POST:
        form_lote = LoteForm(user=request.user, request.POST)
        if form_lote.is_valid():
            Lote = form_lote.save(commit=False)
            Lote.user = request.user
            form_lote.save()
            form_lote = LoteForm()
            return redirect('reporte')

    else:
        ...        
        form_lote = LoteForm(user=request.user)
        
return render (request, 'campos/campos.html',
                {"form": form,
                "form_lote": form_lote,
                })

TAKE A LOOK AT THIS PICTURE

1

1 Answers

0
votes

I've solved it

FORMS.PY


class LoteForm (forms.ModelForm):

class Meta:
    

    model = Lote
    fields = [ 'campo','name','has']
    labels = {
        'campo': _('campo'),
        'name': _('Lote'),
        'has': _('Hectareas'),
    }

def __init__(self, user, *args, **kwargs):
    super(LoteForm, self).__init__(*args, **kwargs)
    self.fields['campo'].queryset = Campos.objects.filter(user=user)

VIEWS.PY


def campos(request):

if request.method =='POST'and 'btn1'in request.POST:
    form = CampoForm(request.POST)
    if form.is_valid():
        camp = form.save(commit=False)
        camp.user = request.user
        form.save()
        form = CampoForm()
        return redirect('reporte')

if request.method =='POST' and 'btn2' in request.POST:
    user = request.user
    form_lote = LoteForm(user, request.POST )
    print(form_lote)
    if form_lote.is_valid():
        Lote = form_lote.save(commit=False)
        Lote.user = request.user
        form_lote.save()
        form_lote = LoteForm(user)
        return redirect('reporte')

else:
    form = CampoForm()
    user = request.user
    form_lote = LoteForm(user)


return render (request, 'campos/campos.html',
            {"form": form,
            "form_lote": form_lote,
            })