0

私を に追加する方法ItemBasket?

class Item(models.Model):
    name = models.CharField(max_length=50)
    category = models.ManyToManyField(Category)
    price = models.DecimalField(max_digits=5, decimal_places=2)

    def __unicode__(self):
        return self.name


class Basket(models.Model):
    owner = models.ForeignKey(User, related_name='user_basket', verbose_name='Owner')
    name = models.CharField("Basket name", max_length=120)
    items = models.ManyToManyField('Item', blank=True, null=True)

私の試み:

def add_item_to_basket(request, product_id):
    if request.method == 'GET':
        form = BasketForm(request.GET)
        if form.is_valid():
            quantity = form.cleaned_data['quantity']
            items = Item.objects.get(id=item_id)
            .........?
4

1 に答える 1

0
your_basket.items.add(your_item)

または、こちらをご覧ください: https://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships

于 2012-09-26T16:34:12.690 に答える