use*_*631 5 django django-forms django-widget
我有两个型号分类和产品.
例:
models.py
class Product:
categories = models.ManyToManyField(Category)
name = models.CharField(max_length=255)
class Category:
categories = models.ForeignKey(self)
name = models.CharField(max_length=255)
Run Code Online (Sandbox Code Playgroud)
作为表单我使用ModelForm:
class ProductForm(ModelForm):
class Meta:
model = Product
fields = ['categories', 'name', 'short_description', 'description']
widgets = {
'categories': MyWidget,
}
Run Code Online (Sandbox Code Playgroud)
我想要实现的目标:
我想实现条件选择(窄选项)在产品表单创建上:
用户选择1级类别(A1,C1).如果父母有孩子,他的孩子会出现一个新的选择框(2级A2)
选项我想:
实用,假设我有7个选择框,每个框的值都是:
如何告诉Django浏览器提交(以及其他数据)发送给ManyToMany的所有三个中的最后一个Child
我可以用Javascript收集它们,但我必须告诉Django获取这些数据,这就是你需要的.
我需要一些帮助作为代码和指示如何做到这一点.
Django 允许您将引用的模型定义为ForeignKey或ManyToManyField作为\'<app_name>.<model_name>\'字符串,而不必导入模型并直接分配它。这解决了很多问题,特别是循环导入。
categories假设您拥有带有Category模型和products模型的应用程序Product,则:
products/models.py:
class Product:\n categories = models.ManyToManyField(Category)\n name = models.CharField(max_length=255)\nRun Code Online (Sandbox Code Playgroud)\n\ncategories/models.py:
class Category:\n categories = models.ManyToManyField(self)\n name = models.CharField(max_length=255)\nRun Code Online (Sandbox Code Playgroud)\n\n可以直接翻译成你需要的:
\n\nproducts/models.py:
class Product:\n categories = models.ManyToManyField(\'categories.Category\')\n name = models.CharField(max_length=255)\nRun Code Online (Sandbox Code Playgroud)\n\ncategories/models.py:
class Category:\n categories = models.ManyToManyField(\'self\')\n name = models.CharField(max_length=255)\nRun Code Online (Sandbox Code Playgroud)\n\n这样,您就可以拥有任意数量的类别:
\n\ncategory_one = Category.create(name=\'Category one\')\ncategory_two = Category.create(name=\'Category two\')\ncategory_three = Category.create(name=\'Category three\')\ncategory_three.categories.add(category_one, category_two)\nsome_product = Product.create(name=\'Test Product\')\nsome_product.categories.add(category_three)\nRun Code Online (Sandbox Code Playgroud)\n\n(多对多字段文档)
\n\n同样重要的是要注意,对于任何 Python 类,self类本身不是 \xe2\x80\x93,而是实例。所以你不能在实例方法之外引用它,这里有一个很好的解释。该字符串在这里起作用的唯一原因\'self\'是 Django 将其转换为它所在的类,categories.Category\xe2\x80\x93\xe2\x80\x93 因此,将其替换为显式可能是一个好self主意\'categories.Category\'。
| 归档时间: |
|
| 查看次数: |
496 次 |
| 最近记录: |