''str' object has no attribute 'all',

I want to get last data choiced by the user of a field1(a foreignkey type) from a form1(model1) already submitted and display it on a another field2(Exist on form2(model2)),

Here is my models.py: Model1:

field1 = models.ForeignKey(ProfileSize, on_delete=models.CASCADE)

Model2:

field2 = models.ForeignKey(ProfileSize,  on_delete=models.CASCADE)

ProfileSize Model:

class ProfileSize(models.Model): 
    name = models.CharField(max_length=60)
    category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='sizes')

The story that I have declared a field2 as a foreignkey in model2 and I have did ModelChoiceField in forms.py To display to me just the last data entered by the user in the field1(exist in the form1) ,

Here is my forms.py:

class Form2(forms.ModelForm):
    field2 = forms.ModelChoiceField(queryset=Model1.objects.none(),)

I have did it with this queryset in views.py

def view2(request):
    if request.method == 'GET':
        form = Form2(request.GET, )
        form.fields['field2'].queryset = Model1.objects.filter(user=request.user).last().field1.name

it shows me error

'str' object has no attribute 'all',

My Question how i can filter correctly the foreignkey to get data from another form using the queryset,



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source