Django ModelForm instance with custom queryset for a specific field
Asked Answered
H

3

35

I have a model not unlike the following:

class Bike(models.Model):
    made_at = models.ForeignKey(Factory)
    added_on = models.DateField(auto_add_now=True)

All users may work at a number of factories and therefore their user profiles all have a ManyToManyField to Factory.

Now I want to construct a ModelForm for Bike but I want the made_at list to consist of only factories at which the current user works. The idea is that users should be able to add bikes that they've assembled and enter which of the factories the bike was made at.

How do I do that?

Hull answered 30/11, 2009 at 17:48 Comment(5)
And, for what it is worth, your question is more readable and to the point than the one I have pointed as a dupe...Eelworm
@celopes: Do you have any source on that?Hull
Nah... I even deleted the comment where I said it was about to get deprecated. For some reason I had my mind that it was. I think I read a ticket about it... But the docs have it there, and there is no sign of deprecation in the source. Just ignore me. :-) And it is auto_now_add btw.Eelworm
Here is the ticket where I read auto_now_add was to be deprecated: code.djangoproject.com/ticket/6434. But ignore it because nothing in the code says it is...Eelworm
possible duplicate of How do I filter ForeignKey choices in a Django ModelForm?Attend
E
15

You question might be a dupe of this.

S. Lott's answer there is the ticket to solve your problem. He answered:

ForeignKey is represented by django.forms.ModelChoiceField, which is a ChoiceField whose choices are a model QuerySet. See the reference for ModelChoiceField.

So, provide a QuerySet to the field's queryset attribute. Depends on how your form is built. If you build an explicit form, you'll have fields named directly.

form.rate.queryset = Rate.objects.filter(company_id=the_company.id) If you take the default ModelForm object, form.fields["rate"].queryset = ...

This is done explicitly in the view. No hacking around.

Eelworm answered 30/11, 2009 at 18:46 Comment(0)
H
50

try something like this in the view

form  = BikeForm()
form.fields["made_at"].queryset = Factory.objects.filter(user__factory)

modify the Factory queryset so that it identifies the factory which the user works at.

Heavensent answered 30/11, 2009 at 18:42 Comment(0)
E
15

You question might be a dupe of this.

S. Lott's answer there is the ticket to solve your problem. He answered:

ForeignKey is represented by django.forms.ModelChoiceField, which is a ChoiceField whose choices are a model QuerySet. See the reference for ModelChoiceField.

So, provide a QuerySet to the field's queryset attribute. Depends on how your form is built. If you build an explicit form, you'll have fields named directly.

form.rate.queryset = Rate.objects.filter(company_id=the_company.id) If you take the default ModelForm object, form.fields["rate"].queryset = ...

This is done explicitly in the view. No hacking around.

Eelworm answered 30/11, 2009 at 18:46 Comment(0)
O
0

Nowaday, you should use:

    form.base_fields['alumno_item'].queryset = AlumnoItem.objects.prefetch_related(
        'alumno',
        'alumno__estudiante',
        'alumno__estudiante__profile',
        'item'
    )
Opportunism answered 13/7, 2018 at 19:47 Comment(1)
I don't think that is correct. It means you are modifying the form at the wrong time (not from the view).Louralourdes

© 2022 - 2024 — McMap. All rights reserved.