Django - populate a MultipleChoiceField via request.user.somechoice_set.all() - django

Is there a more efficient, or cleaner way to do the following?
class SpamForm(ModelForm):
some_choices = fields.MultipleChoiceField()
def __init__(self, *args, **kwargs):
super(SpamForm, self).__init__(*args, **kwargs)
self.fields['some_choices'].choices = [[choice.pk, choice.description] for choice in self.instance.user.somechoice_set.all()]
class Meta:
model = Spam
This is to populate the new form with choices that pertain to the current request.user (which is set on the instance that's passed into the form). Is there a better way?

Use a ModelMultipleChoiceField in the form and set its queryset in __init__().

Related

Django: Forms Queryset

I wonder if that is the right approach. I first call queryset=Reward.objects.all() just to change it right after and filter it. However, I couldn't come up with a better solution. Do you have any thoughts on that?
class ClaimRewardForm(forms.ModelForm):
note = forms.CharField(widget=forms.Textarea)
title = forms.ModelChoiceField(queryset=Reward.objects.all())
# note = forms.DropDown()
class Meta:
model = Reward
fields = ['title']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['title'].queryset = Reward.objects.filter(event=self.initial['event'])
That queryset is never evaluated, because you always replace it on instantiation, so it doesn't really matter what you put there.
One alternative night be to use Reward.objects.none() to indicate that it's never used.
If you should process set of models, try change your approach to FormSets https://docs.djangoproject.com/en/2.0/topics/forms/formsets/
Briefly, FormSets approach seem so: 1. Declare ClaimRewardForm class for single model (Reward in your case) 2. Declare ClaimRewardFormSet for ClaimRewardForm with overriding
class BaseClaimRewardFormSet(BaseModelFormSet):
"""By default, when you create a formset from a model, the formset
will use a queryset that includes all objects in the model"""
def __init__(self, *args, **kwargs):
if 'event' in kwargs.keys():
event = kwargs.pop('event')
else:
event = None
super().__init__(*args, **kwargs)
if event is not None:
self.queryset = Reward.objects.filter(event=event)
else:
self.queryset = Reward.objects.none()
ClaimRewardFormSet = forms.modelformset_factory(Reward, RewardForm,
formset=BaseClaimRewardFormSet)

Prefill MultipleChoiceField django

Lets say I have the following model
class Application(models.Model):
occupation = models.TextField()
and form
class ApplicationForm(forms.ModelForm):
def __init__(self, *args, **kwargs)
super().__init__(*args, **kwargs)
self.fields['occupation'] = forms.MultipleChoiceField(choices=OCCUPATION_CHOICES, widget=CheckboxSelectMultiple)
class Meta:
model = Application
When I use it with a instance (for example via admin) the choices are not selected.
What am I doing wrong?
Edit: Clarification: When I select some choices, I hit submit it saves the data. They look like ['undergraduate', 'postdoc'] in the database. But they are not checked in the form anymore.
I managed to get it working like this.
import ast
class ApplicationForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
if kwargs.get('instance'):
kwargs['initial'] = {
'occupation': ast.literal_eval(kwargs.get('instance').occupation or '[]'),
}
super().__init__(*args, **kwargs)
class Meta:
model = Application
widgets = {
'occupation': CheckboxSelectMultiple(choices=OCCUPATION_CHOICES),
}
Thanks go to RodrigoDela who made me realize what I was doing wrong and pointed me in the right direction.
Edit: A TextField cannot store multiple choices. Take a look at this:
Django Model MultipleChoice
Either you use this, or you override your forms, so that these can parse the instance.occupation content into their form.occupation field.

Django model form - Exclude a field that has no model field

I have a simple model form what I use through the admin interface. Some of my model fields store datas that require a bit more time to calculate (they come from other sites). So I decided to put an extra boolean field to the form to decide to crawl these datas again or not.
class MyModelForm(forms.ModelForm):
update_values = forms.BooleanField(required=False) #this field has no model field
class Meta:
model = MyModel
This extra field doesn't exist in the model because only the form needs it.
The problem is that I only want it to appear if it's an existing record in the database.
def __init__(self, *args, **kwargs):
super(MyModelForm, self).__init__(*args, **kwargs)
if self.instance.pk is None:
#remove that field somehow
I tried nearly everything. Exclude it, delete the variable but nothing wants to work. I also tried dynamically add the field if self.instance.pk is exists but that didn't work too.
Any idea how to do the trick?
Thanks for your answers.
You could subclass the form and add the extra field in the subclass:
class MyModelForm(forms.ModelForm):
class Meta:
model = MyModel
class MyUpdateModelForm(MyModelForm):
update_values = forms.BooleanField(required=False) #this field has no model field
class Meta:
model = MyModel
You can then override the get_form method of your admin, which is passed the current instance: get_form(self, request, obj=None, **kwargs)
Rather than removing the field in __init__ if instance.pk is not None, how about adding it if it is None? Remove the class-level declaration and just change the logic:
class MyModelForm(forms.ModelForm):
class Meta:
model = MyModel
def __init__(self, *args, **kwargs):
super(MyModelForm, self).__init__(*args, **kwargs)
if self.instance and self.instance.pk is not None:
self.fields['update_values'] = forms.BooleanField(required=False)

CBV Django Form View set data for ChoiceField

I'm using the Django Form View and I want to enter custom choices per user to my Choicefield.
How can I do this?
Can I use maybe the get_initial function?
Can I overwrite the field?
When I want to change certain things about a form such as the label text, adding required fields or filtering a list of choices etc. I follow a pattern where I use a ModelForm and add a few utility methods to it which contain my overriding code (this helps keep __init__ tidy). These methods are then called from __init__ to override the defaults.
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = ('country', 'contact_phone', )
def __init__(self, *args, **kwargs):
super(ProfileForm, self).__init__(*args, **kwargs)
self.set_querysets()
self.set_labels()
self.set_required_values()
self.set_initial_values()
def set_querysets(self):
"""Filter ChoiceFields here."""
# only show active countries in the ‘country’ choices list
self.fields["country"].queryset = Country.objects.filter(active=True)
def set_labels(self):
"""Override field labels here."""
pass
def set_required_values(self):
"""Make specific fields mandatory here."""
pass
def set_initial_values(self):
"""Set initial field values here."""
pass
If the ChoiceField is the only thing you're going to be customising, this is all you need:
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = ('country', 'contact_phone', )
def __init__(self, *args, **kwargs):
super(ProfileForm, self).__init__(*args, **kwargs)
# only show active countries in the ‘country’ choices list
self.fields["country"].queryset = Country.objects.filter(active=True)
You can then make your FormView use this form with like this:
class ProfileFormView(FormView):
template_name = "profile.html"
form_class = ProfileForm

django modelform and additional fields and their order

I have a modelform and im creating additional fields (that do not exist in model) for its form.
I know you can reorder the fields in modelform like it says in the docs.
But the problem is - i want the additional fields to be rendered BEFORE the other fields.
Is it possible to somehow reorder the fields of the form before rendering? How does form object keep track of the order of its fields anyway?
Alan
No matter. It seems i found answer already and this seems to do the trick, since i have added 2 additional fields:
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
self.build_fields()
l = len(self.fields.keyOrder)
for i in range(0,2):
f = self.fields.keyOrder[l-1]
self.fields.keyOrder.pop(l-1)
self.fields.keyOrder.insert(0, f)
This above was my initial fix. Later on i found out that it did not cut any more. Then i did this :
class AlertForm(forms.ModelForm):
class Meta:
model = Message
fields = model_fields
def __init__(self, *args, **kwargs):
super(AlertForm, self).__init__(*args, **kwargs)
self.build_fields()
newKeyOrder = []
newKeyOrder.append('field_that_had_to_be_first')
if typechange:
newKeyOrder.append('field_thats_sometimes_necessary')
newKeyOrder += model_fields
self.fields.keyOrder = newKeyOrder
The solutions above no longer works with django 2 (I don't know since when)...
But now, there's an ordered dict fields property on ModelForm that we can use to reorder the fields...
class MyForm(forms.ModelForm):
class Meta:
fields = ['model_field1', 'model_field2']
model = MyModel
extra_field = forms.CharField()
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for k in self._meta.fields:
self.fields.move_to_end(k)
The fields attribute of your ModelForm`s Meta class define which fields to show and in which order.
Use the fields attribute of the ModelForm's inner Meta class. This attribute, if given, should be a list of field names to include in the form. The order in which the fields names are specified in that list is respected when the form renders them.