简体   繁体   English

我如何为带有django中带有“添加项”选项的下拉菜单的外键字段创建表单?

[英]How would I create a form for a foreign key field that has a drop down menu with an 'add item' option in django?

I'll start with my model fields: 我将从模型字段开始:

class Store(models.Model):
    name = models.CharField(max_length=250)

    def __str__(self):
        return self.name


class Product(models.Model):
    type = models.CharField(max_length=250)

    def __str__(self):
        return self.type


class Receipt(models.Model):
    store = models.ForeignKey(Store)
    date = models.DateField()
    line_items = models.ManyToManyField(Product, through='ReceiptProduct')

    def __str__(self):
        return self.store.name + ': ' + str(self.date)


class ReceiptProduct(models.Model):
    receipt = models.ForeignKey(Receipt)
    product = models.ForeignKey(Product)
    price = models.FloatField()
    description = models.CharField(max_length=500, null=True, blank=True)

    def __str__(self):
        return self.product.type  

What I would like to do is create a form for the ReceiptProduct model. 我想做的是为ReceiptProduct模型创建一个表单。

class AddItemForm(ModelForm):
    class Meta:
        model = ReceiptProduct
        fields = ['product', 'price', 'description']

Done. 做完了 And the view? 和看法?

def add_receipt_product(request, receipt_id):
    current_receipt = Receipt.objects.get(id=receipt_id)
    if request.method != 'POST':
        # No data submitted; create a blank form.
        form = AddItemForm(initial={'receipt': current_receipt})
    else:
        # POST data submitted; process data.
        form = AddItemForm(data=request.POST)
        if form.is_valid():
            new_product = form.save(commit=False)
            new_product.receipt = current_receipt
            new_product.save()
            return HttpResponseRedirect(reverse('purchase_log:receipt_details', args=[receipt_id]))

    context = {'current_receipt': current_receipt, 'form': form}
    return render(request, 'purchase_log/add_receipt_product_form.html', context)  

Okay, so what I would like to do is, under the 'product' field (which is a drop down menu populated by the Product model), have an option called, maybe, 'custom product' or something, that the user can select to add an item to the Product model and will then appear in future drop down menus. 好的,所以我想做的是,在“产品”字段(这是产品模型填充的下拉菜单)下,有一个名为“定制产品”的选项,用户可以选择将产品添加到产品模型中,然后将出现在以后的下拉菜单中。 Is this do-able? 这可行吗?
Thank you all in advanced!! 谢谢大家先进!

Django implements this in terms of a "formset" . Django 通过“ formset”来实现这一点。 Check out this tutorial for additional information: http://whoisnicoleharris.com/2015/01/06/implementing-django-formsets.html I think the example there is fairly similar to yours. 请查看此教程以获取更多信息: http : //whoisnicoleharris.com/2015/01/06/implementing-django-formsets.html我认为那里的示例与您的示例非常相似。

In the Django admin interface, things are somewhat easier, and you can use an Inline . 在Django管理界面中,事情有些简单,您可以使用Inline

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何过滤使用下拉菜单放入django表单的foreignkey字段中可以看到的选项? - How to filter the options that can be seen in a foreignkey field that has been put into a django form using drop down menu? django中的下拉列表显示外键字段的对象 - drop down list in django shows object for foreign key field 在 Django Forms 下拉列表中,如何显示通过外键访问的文本? - In a Django Forms drop-down list, how do I show text accessed via a foreign key? 如何从 django 表单中的下拉列表中选择多个数据(字段在数据库中具有 M2M 关系)? - How to Select Multiple data from drop down in django form(Field has M2M relation in database)? 我将如何使用Selenium选择下拉菜单 - How would I use Selenium to select a drop-down menu Django下拉列表中的随机选项 - Random Option In Django Drop Down Form 如何使我的页面在从下拉菜单或文本字段获取数据之间进行选择? - How would I make my page choose between getting data from a drop-down menu or a text-field? 如何创建一个显示下拉菜单的表单,其中包含特定于该用户的项目? - How to create a form that displays a drop down menu with items specific to that user? 如何从 Django 中的外键字段中获取 Select 多个项目 - How to Select Multiple Item from a foreign key field in Django 如何在django中以下拉框的形式具有多个选择字段 - How to have a multiple select field in django in the form of a drop down box
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM