Django - 如果已经选择,则从queryset表单字段中删除选项

时间:2015-05-27 14:21:33

标签: django django-forms

我有一个表单字段,可以调用查询集并使用' select'小部件。

有没有办法从查询集中删除选项值,如果已经添加了'购物车?

在选择表格中,有三个选项:选项A,选项B,选项C.

用户选择选项A,然后点击“添加”。现在,当用户点击“添加”时,我想从选择中删除选项A.

只有选项B和选项C可供选择。

这可以使用Django + Python来完成吗?或者我需要使用额外的JS / jQuery吗?

谢谢!

models.py

class Pickup(models.Model):
    # id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    user = models.ForeignKey(settings.AUTH_USER_MODEL, blank=False, null=True)
    total = models.DecimalField(max_digits=100, decimal_places=2, default=0.00)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)
    active = models.BooleanField(default=True)
    status = models.CharField(max_length=120, choices=STATUS_CHOICES, default="Open")

    def __str__(self):
        return "Pickup Order ID: %s" %(str(self.id))

class PickupItem(models.Model):
    pickup = models.ForeignKey('Pickup', null=True, blank=True)
    dropoffitem = models.ForeignKey(DropoffItem) 
    notes = models.TextField(null=True, blank=True)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)

forms.py

class AddPickupItemForm(forms.ModelForm):
    dropoffitem = forms.ModelChoiceField(queryset=DropoffItem.objects.all(), widget=forms.Select(attrs={'class':'form-control'}))


    class Meta:
        model = PickupItem
        fields = ['dropoffitem']

views.py

def add_item_to_pickup_order(request):
    request.session.set_expiry(120000)

    try:
        user = request.user
        the_id = request.session['pickup_id']               
        pickup = Pickup.objects.get(id=the_id)          
    except:
        user = request.user                                 
        new_pickup_order = Pickup(user=user)                
        new_pickup_order.save()
        request.session['pickup_id'] = new_pickup_order.id          

        the_id = new_pickup_order.id
        pickup = Pickup.objects.get(id=the_id)                  

    try:
        dropoffitem = DropoffItem.objects.get(id=id)

    except DropoffItem.DoesNotExist:
        pass
    except:
        pass

    form = AddPickupItemForm(request.POST or None)
    if request.method == "POST":

        dropoffitem_id = int(request.POST['dropoffitem'])
        pickup_item = PickupItem.objects.create(pickup=pickup, dropoffitem_id=dropoffitem_id)

        pickup_item.save()


        return HttpResponseRedirect('%s'%(reverse('add_item_to_pickup_order')))

    context = {

        "pickup": pickup,
        "form": form,

    }
    return render(request, 'pickups/create_pickup_order.html', context)

html的

{% extends "base.html" %}

{% block content %}
<div class="row">
    <div class="container">
        <div class="col-xs-12">
            <h1>Create Cart</h1>

            <form method="POST" action="{% url 'add_item_to_pickup_order' %}">
                {% csrf_token %}
                <table class="table">
                    <thead>
                        <th>Item</th>
                        <th></th>
                    </thead>
                    <tr>
                        <td>{{ form.dropoffitem }}</td>
                        <td><input type="submit" value="Add Item" class="btn btn-default btn-primary" /></td>
                    </tr>
                </table>
            </form>

1 个答案:

答案 0 :(得分:0)

要从查询集中排除/删除项目,您可以使用exclude

YourModel.objects.exclude(id=4)

要排除多个项目:

YourModel.objects.exclude(id__in=[4, 6, 10])

有关exclude on Django docs的更多信息。