2016-04-29 16 views
0

複数のコンテンツタイプのフィールドを持つ最善の方法を見つけようとします。 私がこれまで行ってきたことはcontacttype CharFieldですと接触モデルです:私の目標は、ユーザーが別の連絡先情報を追加できるようにすることですDjango:1つのモデル、異なるフォームとウィジェットですか?

class Contact(models.Model): 
    CONTACT_TYPES = (
     ('email', 'Email'), 
     ('phone', 'Phone'), 
     ('address', 'Address'), 
     ('facebook', 'Facebook'), 
     ('linkedin', 'LinkedIn'), 
     ('youtube', 'Youtube'), 
     ('twitter', 'Twitter'), 
     ('google', 'Google'), 
    ) 
    teammember = models.ForeignKey(TeamMember) 
    description = models.CharField(max_length=100, null=True) 
    contacttype = models.CharField(max_length=100, choices= CONTACT_TYPES, default='email') 
    contact = models.TextField() 

、プロフィールページに1つだけで一覧表示されwhich'llモデル。私はそれぞれのModelFormのクラスを考えていた

:/連絡先/追加/電子メールまたは/連絡先:

class ContactForm(ModelForm): 

    def __init__(self, data, *args, **kwargs): 
     super(ModelForm, self).__init__(data, *args, **kwargs) 
     self.contacttype = "" 

    class Meta: 
     model = Contact 
     fields = ['description', 'contact'] 
     widgets = {'contact': TextInput()} 

    def clean_contacttype(self): 
     return self.contacttype 

class ContactEmailForm(ContactForm): 

    def __init__(self, data, *args, **kwargs): 
     super(ContactForm, self).__init__(data, *args, **kwargs) 
     self.contacttype = "email" 

    class Meta(ContactForm.Meta): 
     model = Contact 
     fields = ['description', 'contact'] 
     widgets = {'contact': EmailInput()} 

class ContactPhoneForm(ContactForm): 

    def __init__(self, data, *args, **kwargs): 
     super(ContactForm, self).__init__(data, *args, **kwargs) 
     self.contacttype = "phone" 

    class Meta(ContactForm.Meta): 
     model = Contact 
     fields = ['description', 'contact'] 
     widgets = {'contact': TextInput()} 

    def clean_contact(self): 
     cleaned_data = super(ContactForm, self).clean() 
     contact = cleaned_data.get("contact") 
     # Perform some phone number validations 
     return contact 

その後は、私の見解では、私がリクエスト引数(例に応じて、正しいフォームを選ぶだろう/ add/phone)。

私はこれを行う最もエレガントな方法を見つけようとしているので、どんな助けも歓迎します。

読んでいただきありがとうございます。

答えて

0

1つのhtmlファイルを使用する場合は、これが解決策の1つです。

class YourView(TemplateView): 
    template_name = 'your_template.html' 

    def get(self, request, *args, **kwargs): 
     context = self.get_context_data(**kwargs) 
     if context['type'] == 'phone': 
      context['form'] = ContactPhoneForm 
     elif ... 

あなたのビューを設定した場合、その後、

<form action="{% url "your view" %}" enctype="multipart/form-data" method="POST">{% csrf_token %} 
{% if form.contenttype == 'phone' %} 
    {% include "partials/contact_phone_form.html" with form=form %} 
{% elif form.contenttype == 'email' %} 
    {% include "partials/contact_email_form.html" with form=form %} 
{% else %} 
    do something. 
{% endif %} 
</form> 

と、contact_phone_form.htmlとcontact_email_formされ、このソリューションは1つのテンプレート、マルチフォーム用です

anything. 
<input name='phone'> 
anything. 

のように見えます。

特定のフォーム用に複数のテンプレートを作成する場合は、これを使用できます。

class YourView(TemplateView): 
    template_name = 'your_template.html' 

    def get(self, request, *args, **kwargs): 
     context = self.get_context_data(**kwargs) 
     if context['type'] == 'phone': 
      template_name = 'phone_form_template.html' 
     elif context['type'] == 'email': 
      template_name = 'email_form_template.html' 
     ... 
0

私のような要求の引数に応じて、forms.Formを使用します。??

/連絡先/追加/タイプ=メールまたは/連絡先/追加/タイプ=電話

とだからあなたは(テストされていないコードのように)それを使用することができます:

class ContactForm(forms.Form): 

    def __init__(self, *args, **kwargs): 
     super(CreateUserquestionnaireForm, self).__init__(*args, **kwargs) 
     if "email" in self.data: 
      ... do some thing like change contact type or widget 
     if "phone" in self.data: 
      ... do some thing like change contact type or widget 
関連する問題