[[+content_image]]
W
W
windf1n2019-05-05 11:45:53
Django
windf1n, 2019-05-05 11:45:53

How to filter model objects in a form?

I have forms.py:

camera = Camera.objects.all()
TEST_CHOICES = [[x.id, 'Камера ' + str(x.number)] for x in camera]

class ZayavkaForm(forms.Form):
    name = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple(), choices=TEST_CHOICES)

The task is this: I do not need to take all the objects from the Camera model, but filter them the way I did in views.py:
def zayavka_view(request, adress_slug):

    adress = Adress.objects.get(slug=adress_slug)
    cameras_of_adress = Camera.objects.filter(adress=adress).order_by('-number')
    context = {
        'adress' : adress,
        'cameras_of_adress' : cameras_of_adress,
    }

    return render(request, 'z.html', context)

How to take adress_slug in forms.py?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
F
FulTupFul, 2019-05-05
@FulTupFul

class OrderForm(forms.ModelForm):
    class Meta:
        model = Address
        fields = ('slug', )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question