2017-10-17 87 views
7

olarak gösteriyorum Birden çok örnekte kullanabileceğim bir Django uygulamasına sahibim. Bir model (Listeleme) değişken sayıda alana sahip olabilir (farklı örnekler için) ancak örnek için her zaman tam olarak bu ek alanlara sahip olacaktır. Böyle yönetici aracılığıyla yüzden oluşturduk modeller eklendi bu ekstra alanları istiyorum:Başka bir kaydın alan içeriği olan Django şablonunda bilinmeyen alanların sayısını

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) 

teori yönetici ısmarlama alanları belirtir ve bunlar daha sonra olan formlarda kullanıcıya görüntülenen olduğunu.

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

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

Bu yönetici kullanıcı açılır menüsünden her BespokeField birini seçmek gerektiği anlamına gelmez ama: my admin.py benziyor bu yüzden kullanıcılardan istihbarat bir nebze varsayabiliriz olarak yönetici dahilinde bu nispeten basittir Bundan rahatsız değilim çünkü benzersiz bir özellik kullanarak, her birinin yalnızca bir tane olduğundan emin olun.

Ne yapamayacağımı, bunu yönetici olmayan kullanıcıya kolay bir şekilde sunmaktır. İstediğim şey, formda ListingBespokeField.value için bir etiket olarak görüntülenecek BespokeField.name.

forms.py'da (ListingBespokeField için) sahip olduğum şey budur.

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 
) 

Sonra şöyle bir şablon aracılığıyla sunmaya çalışıyorum: Bu işe yaramazsa

<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> 

. Biraz içgörü kullanabilirim lütfen.

<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> 
+0

tam olarak çalışmadığını nedir: – Jonathan

+0

Listing.ListingBespokeField için alanlara sahip olduğum Liste için bir formda BespokeField.name etiketini göstermek gerekiyor. Şu anda 'Ismarlama alanı' – HenryM

+1

'u gösterir. Bir kamu havuzuna erişim sağlayabiliyor musunuz? –

cevap

2

Bu

benim çözüm oldu?