Ssr*_*tya 4 django django-forms django-views chained-select
class Library(forms.ModelForm):
author = forms.ChoiceField(
widget = forms.Select(),
choices = ([
('Jk Rowling','Jk Rowling'),
('agatha christie','agatha christie'),
('mark twain','mark twain'),
]),
initial = '1',
required = True,
)
books = forms.ChoiceField(
widget = forms.Select(),
choices = ([
('Harry Potter 1','Harry Potter 1'), # 1
('Harry Potter 2','Harry Potter 2'), # 2
('Harry Potter 3','Harry Potter 3'), # 3
('Harry Potter 4','Harry Potter 4'), # 4
('The A.B.C. Murders','The A.B.C. Murders'), # 5
('Dumb Witness','Dumb Witness'), # 6
('Death on the Nile','Death on the Nile'), # 7
('Murder Is Easy','Murder Is Easy'), # 8
('Roughing It','Roughing It'), # 9
(' The Gilded Age ',' The Gilded Age '), # 10
('Adventures of Tom Sawyer','Adventures of Tom Sawyer'), # 11
]),
initial = '1',
required = True,
)
Run Code Online (Sandbox Code Playgroud)
如果用户选择“作者为Jk罗琳”,则必须在“书籍选择”字段中填充“哈利·波特系列”(1至4个选择)
如果用户选择作者为Agatha Christie,则“图书选择”字段中仅需填充(5至8)
如果用户选择“作者为马克·吐温”,那么只有(8至11)选择必须填充在“书籍选择”字段的选择小部件中
我想过滤选择,任何人都可以帮忙吗?
保留author并保存books在数据库中。
models.py
Class Author(models.Model):
name = models.CharField(max_length=50)
......
class Books(models.Model):
....
author = models.ForeignField(Author)
....
Class Library(models.Model):
.....
author = models.ForeignKey(Author)
books = models.ForeignKey(Books)
.....
Run Code Online (Sandbox Code Playgroud)
表格
class Library(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(Library, self).__init__(*args, **kwargs)
self.fields['author'].choices = list(Author.objects.values_list('id', 'name'))
self.fields['books'].choices = list(Books.objects.values_list('id', 'name'))
class Meta:
Model: Library
Run Code Online (Sandbox Code Playgroud)
根据Author选择,从ajax调用触发并获取所有对应的books。
| 归档时间: |
|
| 查看次数: |
3537 次 |
| 最近记录: |