1

ここに私のモデルと管理クラスがあります:

---------------------Models-----------------------
from django.contrib.auth.models import User

class PurchaseOrder(models.Model):
    buyer = models.ForeignKey(User)
    is_debit = models.BooleanField(default = False)
    delivery_address = models.ForeignKey('useraccounts.Address')
    organisation = models.ForeignKey('useraccounts.AdminOrganisations')
    date_time = models.DateTimeField(auto_now_add=True)
    total_discount = models.IntegerField()
    tds = models.IntegerField()
    mode_of_payment = models.ForeignKey(ModeOfPayment)
    is_active = models.BooleanField(default = True)
    class Meta:
        verbose_name_plural = "Purchase Orders"

    def __unicode__(self):
        return '%s' % (self.id)

----------------------------------Admin----------------------------------------
"""
This class is used to add, edit or delete the details of item purchased
"""
class PurchasedItemInline(admin.StackedInline):
    form = ItemSelectForm
    model = PurchasedItem
    fields = ['parent_category', 'sub_category', 'item', 'qty', ]
    extra = 10

class BuyerChoices(AutoModelSelect2Field):
    queryset = User.objects.all()
    search_fields = ['username__icontains', ]

class BuyerForm(ModelForm):
    user_verbose_name = 'Buyer'
    buyer = BuyerChoices(
        label='Buyer',
        widget=AutoHeavySelect2Widget(
            select2_options={
            'width': '220px',
            'placeholder': 'Lookup %s ...' % user_verbose_name
            }
        )
    )

    class Meta:
        model = PurchaseOrder
        fields = '__all__'

"""
This class is used to add, edit or delete the details of items
purchased but buyer has not confirmed the items purchased, this class
inherits the fields of PurchaseOrder derscribing the delivery address of
buyer , is_debit , total discount , tds and mode of payment
"""
class PurchaseOrderAdmin(admin.ModelAdmin):
    form = BuyerForm
   #list_display = ['id','buyer','delivery_address','date_time','is_active']
    inlines = [PurchasedItemInline]
  # model = PurchaseOrder
   #actions = [mark_active, mark_inactive]
   #list_filter = ['date_time']
   #search_fields = ['id']
    list_per_page = 20
    def response_add(self, request, obj, post_url_continue=None):
        request.session['old_post'] = request.POST
        request.session['purchase_order_id'] = obj.id
        return HttpResponseRedirect('/suspense/add_distance/')

を実装しようとしていdjango-select2ますが、インラインを使用する と、実装PurchaseOrderAdminしたフィールドが表示されません django-select2:

スクリーンショット1

しかし、インラインを削除すると、正常に動作します:

スクリーンショット2

編集

ここにItemSelectFormがあります

class ItemSelectForm(forms.ModelForm):
    class Media:
        js = (
            'http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js',
            'js/ajax.js', 
        )

    try:
        parent_category = forms.ModelChoiceField(queryset=Category.objects.\
            filter(parent__parent__isnull=True).filter(parent__isnull=False))

        sub_category_id = Category.objects.values_list('id',flat=True)
        sub_category_name = Category.objects.values_list('name',flat=True)
        sub_category_choices = [('', '--------')] + [(id, name) for id, name in
        itertools.izip(sub_category_id, sub_category_name)]
        sub_category = forms.ChoiceField(sub_category_choices)
    except:
        pass

    item = forms.ModelChoiceField(queryset = Product.objects.all())

    def __init__(self, *args, **kwargs):
        super(ItemSelectForm, self).__init__(*args, **kwargs)
        self.fields['parent_category'].widget.attrs={'class': 'parent_category'}
        self.fields['sub_category'].widget.attrs={'class': 'sub_category'}
        self.fields['item'].widget.attrs={'class': 'item'}
4

1 に答える 1