I have a form called DemoForm
which is related to model Demo
class Demo(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
ans = models.CharField(max_length=1024)
and the form for this is
class DemoForm(forms.ModelForm):
class Meta:
model = Demo
exclude = ('user',)
widgets = {'ans': forms.CheckboxSelectMultiple}
I want to render this form using a queryset
I have tried different approaches like
form = DemoForm(initial=Love.objects.filter(user=request.user))
<form=GoodForm()
form.fields["ans"].queryset = Love.objects.filter(user=request.user) >
form=DemoForm(instance=Love.objects.filter(user=request.user)
form=DemoForm(instance=request.user.love_set.all())
Sometimes it is showing no _Meta present
and when I use initial, it shows the expected length 2 got 1 (got 3)
NOTE- The Love
model is related to user in the same way as the Demo
is related to user using ForeignKey
. Means the Love
model is a copy of Demo
model. So the query returns nested objects
request
. – Topper__init__
method of the form to change the queryset on the fly – Sedgewake