2017-10-17 4 views
7

私は複数のインスタンスで使用できるDjangoアプリケーションを持っています。 1つのモデル(リスト)は、(異なるインスタンスの)可変数のフィールドを持つことができますが、そのインスタンスのフィールドを常に正確に保持します。別のレコードの項目内容をラベルとしてDjangoテンプレートに不明な数のフィールドを表示

class BespokeField (models.Model): 
    name = models.CharField(
     max_length = 20, 
     verbose_name = "Field Title" 
    ) 

    def __unicode__(self): 
     return self.name 

class Listing (models.Model): 
    name = models.CharField (
     verbose_name = 'Listing', 
     max_length = 30 
    ) 
    slug = models.SlugField (
     verbose_name = "Slug", 
     allow_unicode = True, 
     unique=True, 
     blank=True, 
     null=True 
    ) 

class ListingBespokeField (models.Model): 
    bespoke_field = models.ForeignKey(BespokeField) 
    listing = models.ForeignKey(Listing) 
    value = models.CharField (
     max_length = 60 
    ) 

    def __unicode__(self): 
     return u'%s | %s' % (self.listing.name, self.bespoke_field.name) 

理論はオーダーメイドのフィールドを指定し、これらは、その後の形でユーザーに表示されている管理者である私は、これらの追加のフィールドは、私はこのようなモデルを作成した管理者を介して追加します。

class ListingBespokeFieldInline(admin.TabularInline): 
    model = ListingBespokeField 
    extra = len(BespokeField.objects.all()) 
    max_num = len(BespokeField.objects.all()) 

class ListingAdmin(admin.ModelAdmin): 
    inlines = [ListingBespokeFieldInline] 

これは、管理ユーザーがドロップダウンから、各BespokeFieldのいずれかを選択しなければならない意味が、私はない:私のadmin.pyがどのように見えるので、私は、ユーザーからの知性のささやかをとることができるよう管理者の中で、これは比較的簡単です私はそれを不快に感じることはありません。なぜなら、unique_togetherを使うことによってそれぞれが1つしかないことを保証するからです。

私はどのようにしてうまくいかないのでしょうか?これは管理者ではないユーザーにフレンドリーな方法で提示します。私が欲しいのは、ListingBespokeField.valueのラベルとしてフォームにBespokeField.nameを表示することです。

これは私がforms.py(ListBespokeFieldの場合)にあるものです。

class ListingBespokeFieldInline(forms.ModelForm): 
    class Meta: 
     model = ListingBespokeField 
     exclude =['id'] 
     widgets = { 
      'bespoke_field' : forms.HiddenInput(), 
      'value' : forms.TextInput(attrs={'class' : 'form-control'}) 
     } 

class ListingBespokeFieldForm(forms.ModelForm): 
    class Meta: 
     model = ListingBespokeField 
     exclude =() 

BESPOKE_FIELD_COUNT = len(BespokeField.objects.all()) 

ListingBespokeFieldInlineFormSet = forms.inlineformset_factory (
    Listing, 
    ListingBespokeField, 
    form=ListingBespokeFieldInline, 
    extra = BESPOKE_FIELD_COUNT, 
    max_num = BESPOKE_FIELD_COUNT, 
    exclude = ['id'], 
    can_delete=False, 
    can_order=False 
) 

私は、次のようにテンプレートを通してそれを提示しようとしている:

<table class="table"> 
    {{ bespokefields.management_form }} 

    {% for form in bespokefields.forms %} 
     {% if forloop.first %} 
     <thead> 
      <tr> 
       {% for field in form.visible_fields %} 
        <th>{{ field.label|capfirst }}</th> 
       {% endfor %} 
      </tr> 
     </thead> 
     {% endif %} 
     <tr class="formset_row bespokefield"> 
      <td> 
       {{ form.listing }}{{ form.id }}{{ form.bespoke_field }} 
       {{ form.bespoke_field.label }} 
      </td> 
      <td>{{ form.value }}</td> 
     </tr> 
    {% endfor %} 
</table> 

これは動作しません。私はいくつかの洞察力を使用することができます。

+0

それが正確に動作しないことは何ですか? – Jonathan

+0

BespokeField.nameを、List.ListingBespokeFieldのフィールドを持つリストのフォームのラベルとして表示する必要があります。現在、「Bespoke field」と表示されています – HenryM

+1

公開リポジトリにアクセスできますか? –

答えて

2

これが私の解決策だった:

<table class="table"> 
    {{ bespokefields.management_form }} 

    {% for form in bespokefields.forms %} 
     <tr class="formset_row bespokefield"> 
      <td> 
       {{ form.listing }}{{ form.id }} 
       <select id="id_listingbespokefield_set-{{ forloop.counter0 }}-bespoke_field" name="listingbespokefield_set-{{ forloop.counter0 }}-bespoke_field" class="form-control"> 
       {% with forloop.counter as counter %} 
        {% for x,y in form.fields.bespoke_field.choices %} 
         {% if counter == forloop.counter0 %} 
          <option value="{{x}}" selected>{{y}}</option> 
         {% endif %} 
        {% endfor %} 
       {% endwith %} 
       </select> 
      </td> 
      <td>{{ form.value }}</td> 
     </tr> 
    {% endfor %} 
</table> 
関連する問題