def init_fields(self): self.fields["groups"] = forms.ModelMultipleChoiceField( queryset=ContactGroup.objects.all_except_defaults(), initial=(self.instance.groups.all_except_defaults() if self.instance.pk else ()), required=False, widget=QuickAddContactGroupMultiSelect(attrs={"data-model": "shuup.ContactGroup"}), label=_("Contact Groups"), help_text=_( "The contact groups this contact belongs to. Contact groups are defined in Contacts - Contact Groups " "and are used to configure sales, campaigns, and product pricing tailored for a set of users." ) ) if "account_manager" in self.fields: self.fields["account_manager"].widget = PersonContactChoiceWidget(clearable=True) if not self.request or (self.request and self.request.user.is_superuser): shops_qs = Shop.objects.all() else: shops_qs = Shop.objects.filter(staff_members__in=[self.request.user]) if "tax_group" in self.fields: self.fields["tax_group"].widget = QuickAddTaxGroupSelect(editable_model="shuup.CustomerTaxGroup") if self.instance and self.instance.tax_group: self.fields["tax_group"].widget.choices = [ (self.instance.tax_group.id, self.instance.tax_group.name) ] self.fields["shops"] = forms.ModelMultipleChoiceField( queryset=shops_qs, initial=(self.instance.shops.all() if self.instance.pk else ()), required=False, widget=forms.SelectMultiple(), label=_("Shops"), help_text=_("The shops this contact belongs to") )
def init_fields(self): self.fields["groups"] = forms.ModelMultipleChoiceField( queryset=ContactGroup.objects.all_except_defaults(), initial=(self.instance.groups.all_except_defaults() if self.instance.pk else ()), required=False, widget=forms.SelectMultiple(), label=_("Contact Groups") ) if "account_manager" in self.fields: self.fields["account_manager"].widget = PersonContactChoiceWidget(clearable=True)
def init_fields(self): self.fields["groups"] = forms.ModelMultipleChoiceField( queryset=ContactGroup.objects.all_except_defaults(), initial=(self.instance.groups.all_except_defaults() if self.instance.pk else ()), required=False, widget=forms.SelectMultiple(), label=_("Contact Groups"), help_text=_( "The contact groups this contact belongs to. Contact groups are defined in Contacts - Contact Groups " "and are used to configure sales, campaigns, and product pricing tailored for a set of users." ) ) if "account_manager" in self.fields: self.fields["account_manager"].widget = PersonContactChoiceWidget(clearable=True)
def generate_fields(self): self.fields["type"] = forms.ChoiceField(choices=[ ("PersonContact", _("Person")), ("CompanyContact", _("Company")) ], label=_("Type")) self.fields["groups"] = forms.ModelMultipleChoiceField( queryset=ContactGroup.objects.all(), initial=(self.instance.groups.all() if self.instance.pk else ()), required=False, widget=forms.CheckboxSelectMultiple(), label=_("Contact Groups")) self.fields_by_model = {} classes = (Contact, PersonContact, CompanyContact) if self.contact_class: classes = ( self.contact_class, Contact, ) self._meta.model = classes[0] for model_cls in classes: model_name = str(model_cls.__name__) model_fields = forms.fields_for_model( model_cls, fields=self.FIELDS_BY_MODEL_NAME[model_name], formfield_callback=self.formfield_callback) self.fields_by_model[model_name] = model_fields self.fields.update(model_fields.items()) if self.contact_class: self.fields.pop("type") # Can't change that. if not self.instance.pk: self.fields.pop("is_active") self.fields["account_manager"].widget = PersonContactChoiceWidget( clearable=True)