2017-10-17 22 views
7

Tôi có ứng dụng Django mà tôi muốn có thể sử dụng trong nhiều phiên bản. Một mô hình (Liệt kê) có thể có một số trường biến (cho các trường hợp khác nhau) nhưng sau đó sẽ luôn luôn có chính xác các trường bổ sung đó cho cá thể đó. Tôi muốn những trường bổ sung thêm thông qua quản trị mô hình vì vậy tôi đã tạo như thế này:Hiển thị số trường không xác định trong mẫu Django có nội dung trường của bản ghi khác dưới dạng nhã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) 

Lý thuyết là admin xác định các lĩnh vực bespoke và đây là sau đó hiển thị cho người sử dụng trong hình thức. Trong quản trị này là tương đối đơn giản như tôi có thể giả định một chút về thông tin tình báo từ những người sử dụng nên admin.py của tôi trông giống như:

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

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

Mà có nghĩa người sử dụng quản trị viên phải lựa chọn một trong mỗi BespokeField từ menu thả xuống nhưng tôi 'không khó chịu với điều đó bởi vì bằng cách sử dụng unique_together đảm bảo chỉ có một trong mỗi.

Điều tôi không thể biết cách thực hiện là trình bày điều này cho người dùng không phải quản trị viên một cách thân thiện. Những gì tôi muốn là BespokeField.name để hiển thị trên biểu mẫu dưới dạng nhãn cho ListingBespokeField.value.

Đây là những gì tôi có trong forms.py (đối với ListingBespokeField).

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 
) 

tôi sau đó cố gắng để trình bày nó thông qua một khuôn mẫu như sau:

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

này không hoạt động. Tôi có thể sử dụng một số cái nhìn sâu sắc xin vui lòng.

+0

nó là gì mà không làm việc chính xác? – Jonathan

+0

Tôi cần nó để hiển thị BespokeField.name làm nhãn trong một biểu mẫu cho Liệt kê nơi tôi có các trường cho Listing.ListingBespokeField. Hiện tại, nó hiển thị 'trường Bespoke' – HenryM

+1

Bạn có thể cung cấp quyền truy cập vào kho lưu trữ công cộng không? –

Trả lời

2

Đây là giải pháp của tôi:

<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> 
Các vấn đề liên quan