2012-06-18 3 views
0

여러 모델로 주문 양식을 작성하는 예가 있습니다. 이 모델들을 하나의 형식으로 렌더링하고 formset이 제 대답이라고 말했습니다. 나는 이것이 어떻게 작동 하는지를 연구하고 있었고 여전히 내 바퀴를 돌리지 않았다. 죄송합니다. 이것이 간단하고 나는 그것을 보지 않을 것입니다.양식 세트에 대한 이해가없는 Noobie

class ContactForm(forms.ModelForm): 
    address = forms.ChoiceField(required = True, widget=RadioSelect(), choices=ADDRESS_CHOICES) 
    class Meta: 
     model = Contact 


class LetterHeadForm(forms.ModelForm): 
    address = forms.ChoiceField(required = True, widget=RadioSelect(attrs={'id': 'letterhead_address', 'required': 'True'}), choices=ADDRESS_CHOICES) 
    class Meta: 
     model = LetterHead 
     widgets = { 
      'contact': forms.HiddenInput, 
      'quantity': forms.Select(attrs={'id': 'letterhead_quantity'}, choices=QUANTITY_CHOICES), 
     } 


class WindowEnvForm(forms.ModelForm): 
    address = forms.ChoiceField(required = True, widget=RadioSelect(attrs={'id': 'windowenv_address', 'required': 'True'}), choices=ADDRESS_CHOICES) 
    class Meta: 
     model = WindowEnv 
     widgets = { 
      'contact': forms.HiddenInput, 
      'quantity': forms.Select(attrs={'id': 'windowenv_quantity'}, choices=QUANTITY_CHOICES), 
     } 


class NumberTenEnvForm(forms.ModelForm): 
    address = forms.ChoiceField(required = True, widget=RadioSelect(attrs={'id': 'numbertenenv_address', 'required': 'True'}), choices=ADDRESS_CHOICES) 
    class Meta: 
     model = NumberTenEnv 
     widgets = { 
      'contact': forms.HiddenInput, 
      'quantity': forms.Select(attrs={'id': 'numbertenenv_quantity'}, choices=QUANTITY_CHOICES), 
     } 


class NineByTwelveEnvForm(forms.ModelForm): 
    address = forms.ChoiceField(required = True, widget=RadioSelect(attrs={'id': 'ninebytwelveenv_address', 'required': 'True'}), choices=ADDRESS_CHOICES) 
    class Meta: 
     model = NineByTwelveEnv 
     widgets = { 
      'contact': forms.HiddenInput, 
      'quantity': forms.Select(attrs={'id': 'ninebytwelveenv_quantity'}, choices=QUANTITY_CHOICES), 
     } 


class TenByThirteenEnvForm(forms.ModelForm): 
    address = forms.ChoiceField(required = True, widget=RadioSelect(attrs={'id': 'tenbythirteenenv_address', 'required': 'True'}), choices=ADDRESS_CHOICES) 
    class Meta: 
     model = TenByThirteenEnv 
     widgets = { 
      'contact': forms.HiddenInput, 
      'quantity': forms.Select(attrs={'id': 'tenbythirteenenv_quantity'}, choices=QUANTITY_CHOICES), 
     } 


class BusinessCardForm(forms.ModelForm): 
    print_choices = forms.ChoiceField(required = True, widget=RadioSelect(), choices=PRINT_CHOICES) 
    card_styles = forms.ChoiceField(required = True, widget=RadioSelect(), choices=CARD_CHOICES) 
    card_mailing_address = forms.ChoiceField(required = True, widget=RadioSelect(), choices=ADDRESS_CHOICES) 
    class Meta: 
     model = BusinessCard 
     widgets = { 
      'contact': forms.HiddenInput, 
     } 


class RushOrderForm(forms.ModelForm): 
    class Meta: 
     model = RushOrder 
     widgets = { 
      'contact': forms.HiddenInput, 
      'rush_order': forms.CheckboxInput, 
      'in_hand_date': forms.extras.SelectDateWidget 
     } 


class OrderNoteForm(forms.ModelForm): 
    class Meta: 
     model = OrderNote 
     widgets = { 
      'contact': forms.HiddenInput, 
      'add_note': forms.CheckboxInput, 
      'notes': forms.Textarea 
     } 

그리고 여기 내이다 : 어떤 통찰력 사전에

class OrderFormView(CreateView): 
    model = Contact 
    form_class = ContactForm 
    template_name = 'orderform.html' 
    success_url = 'success' 

def get_context_data(self, **kwargs): 
    context = super(OrderFormView, self).get_context_data(**kwargs) 
    context.update({ 
     'letterhead_form': LetterHeadForm, 
     'windowenv_form': WindowEnvForm, 
     'numbertenenv_form': NumberTenEnvForm, 
     'ninebytwelveenv_form': NineByTwelveEnvForm, 
     'tenbythirteenenv_form': TenByThirteenEnvForm, 
     'businesscard_form': BusinessCardForm, 
     'rushorder_form': RushOrderForm, 
     'ordernote_form': OrderNoteForm, 
     }) 
    return context 

def form_valid(self, form): 
    if form.is_valid(): 
     data = form.cleaned_data 
     email = OrderFormNotification(to=[settings.ORDERFORM_EMAIL_ADDRESS, ], 
       extra_context=data) 
     email.send() 

덕분에 여기

class Contact(models.Model): 
    first_name = models.CharField(max_length=100) 
    last_name = models.CharField(max_length=100) 
    email_address = models.EmailField(max_length=275) 
    address = models.CharField(max_length=10, choices=ADDRESS_CHOICES) 

    def __unicode__(self): 
     return "%s %s" % (self.first_name, self.last_name) 


class BaseStationary(models.Model): 
    contact = models.ForeignKey(Contact, related_name='%(class)s_related') 
    address = models.CharField(max_length=10, choices=ADDRESS_CHOICES) 
    quantity = models.CharField(max_length=3, choices=QUANTITY_CHOICES) 

    class Meta: 
     abstract = True 


class LetterHead(BaseStationary): 
    pass 


class WindowEnv(BaseStationary): 
    pass 


class NumberTenEnv(BaseStationary): 
    pass 


class NineByTwelveEnv(BaseStationary): 
    pass 


class TenByThirteenEnv(BaseStationary): 
    pass 


class BusinessCard(models.Model): 
    contact = models.ForeignKey(Contact, related_name='businesscards') 
    card_first_name = models.CharField(max_length=100) 
    card_last_name = models.CharField(max_length=100) 
    title = models.CharField(max_length=100) 
    print_choices = models.CharField(max_length=19, choices=PRINT_CHOICES) 
    card_styles = models.CharField(max_length=12, choices=CARD_CHOICES) 
    card_email_address = models.EmailField(max_length=275) 
    office_phone_number = PhoneNumberField(_('main office phone number'), 
     blank=True, null=True) 
    toll_free_number = PhoneNumberField(_('toll free number'), 
     blank=True, null=True) 
    mobile_number = PhoneNumberField(_('mobile phone number'), 
     blank=True, null=True) 
    fax_number = PhoneNumberField(_('main office fax'), 
     blank=True, null=True) 
    card_mailing_address = models.CharField(max_length=10, 
     choices=ADDRESS_CHOICES) 
    card_quantity = models.CharField(max_length=3, 
     choices=CARD_QUANTITY_CHOICES) 


class RushOrder(models.Model): 
    contact = models.ForeignKey(Contact, related_name='rushorders') 
    rush_order = models.BooleanField() 
    in_hand_date = models.DateField(blank=True, null=True) 


class OrderNote(models.Model): 
    contact = models.ForeignKey(Contact, related_name='ordernotes') 
    add_note = models.BooleanField() 
    notes = models.TextField() 

내 형태이다 : 여기

내 모델입니다. 비록 그것이 나를위한 형식을 더 잘 이해하는 방향으로 나를 가리킬지라도.

+0

정확히 무엇이 문제입니까? –

+0

@MarkLavin이 템플릿을 내 템플릿 ... 하나가 아닌 9 가지 형식으로 만들 필요가 있습니다. 나는 형식 세트를 사용하라는 말을 들었지만, 읽은 모든 것에서는 9 가지 ModelForms로 구현하는 방법을 이해하지 못합니다. – tjoenz

답변

1

9 가지 모델에 대해 9 가지 양식이 필요한 경우 양식 세트가 도움이된다고 생각하지 않습니다. FormsSet은 동일한 유형의 여러 양식을 구성하기위한 것입니다. 마찬가지로 CreateView은 단일 모델을 만드는 단순한 경우에만 사용하기위한 것입니다. 여러 모델을 만들거나 여러 양식의 유효성을 검사하는 경우 CreateView과 싸워서이 작업을 수행 할 수 있습니다. ProcessFormView (아마도 View)까지 작성한 자신 만의 뷰 클래스를 작성하는 것이 좋습니다.

+0

@MarkLavin,이 두 가지 클래스 기반 뷰를 살펴 보겠습니다. 내가 읽은 것으로부터 포맷이 필요하다고 말했을 때, 그것은 의미가 없었습니다. – tjoenz

관련 문제