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

0

它通过在 static/suit/js/suit.js 中添加以下行对我有用

添加:

(function ($) {
    Suit.after_inline.register('init_select2', function(inline_prefix, row){
        $(row).find('select').select2(); 
    });
于 2017-11-24T18:17:17.593 回答