简体   繁体   English

根据另一个ModelMultipleChoice中先前选择的选项,在ModelMultipleChoice中显示不同的QuerySet

[英]Show different QuerySet in a ModelMultipleChoice depending in previously selected option in another ModelMultipleChoice

I'm trying to build a simple Django webApp that stores documents in the server and it makes a relationship between the document and a project. 我正在尝试构建一个简单的Django webApp,该服务器将文档存储在服务器中,并在文档和项目之间建立关系。

Here are my models: 这是我的模型:

class Project(models.Model):
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete='models.CASCADE', related_name='project_members')
    project_members = models.ManyToManyField(settings.AUTH_USER_MODEL)
    name = models.CharField(max_length=20, blank=False, null=False, unique=True)

    def __str__(self):
        return str(self.name)

class Document(models.Model):
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    path = models.FileField(upload_to='documents/')
    name = models.CharField(max_length=50)
    folders = models.ManyToManyField(Folder)
    projects = models.ManyToManyField(Project)


    def __str__(self):
        return str(self.name)

Here is the code of the form: 这是表格的代码:

class RemoveFileProject(forms.Form):
    project_name = forms.ModelMultipleChoiceField(queryset=Project.objects.none())
    document_name = forms.ModelMultipleChoiceField(queryset=Document.objects.none(), widget=FilteredSelectMultiple("Documentos", is_stacked=False))

    def __init__(self, *args, **kwargs):
        user = kwargs.pop('user', None)
        super(RemoveFileProject, self).__init__(*args, **kwargs)
        if user:
            proyectos = Project.objects.filter(project_members=user)
            print(proyectos)
            self.fields['project_name'] = forms.ModelMultipleChoiceField(queryset=proyectos)
            docs = Document.objects.filter(projects=proyectos).select_related()
            self.fields['document_name'] = forms.ModelMultipleChoiceField(queryset=docs, widget=FilteredSelectMultiple("Documentos", is_stacked=False))

Now I have 2 problems: 现在我有两个问题:

  1. I'm receiving a "ValueError: The QuerySet value for an exact lookup must be limited to one result using slicing". 我收到“ ValueError:用于精确查找的QuerySet值必须限制为使用切片的一个结果”。 I understand that this is because proyectos is a QuerySet and they behave in unique ways but I'm having trouble to grasp how can I make a "join" in django. 我知道这是因为proyectos是QuerySet,它们的行为方式独特,但是我很难掌握如何在Django中进行“ join”。

  2. I want to only show the documents that belong to each project, so If I choose the project "proyect 1", the second ModelMultipleChoiceFied should only display the documents that are related to "project 1". 我只想显示属于每个项目的文档,因此,如果选择项目“ proyect 1”,则第二个ModelMultipleChoiceFied应该只显示与“项目1”相关的文档。

Thanks in Advance 提前致谢

I would recommend looking into the Django Autocomplete Light (DAL) library, and specifically the forward attribute for widgets ( https://django-autocomplete-light.readthedocs.io/en/master/tutorial.html?highlight=forward#filtering-results-based-on-the-value-of-other-fields-in-the-form ). 我建议您查看Django Autocomplete Light(DAL)库,尤其是小部件的forward属性( https://django-autocomplete-light.readthedocs.io/en/master/tutorial.html?highlight=forward#filtering-基于表单中其他字段的值的结果 )。 Works very well for what you're asking! 非常适合您的要求!

暂无
暂无

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

相关问题 使用queryset在表格内“过滤”以显示来自数据库的结果,具体取决于所选的选项 - Using queryset to 'filter' within form to display results from DB depending on option selected Django根据另一个查询注释查询集 - Django annotate queryset depending on another query 将Python与Tkinter结合使用,如何根据选项菜单中选择的选项,使按钮按下做不同的事情? - Using Python with Tkinter, how can I make a button press do a different thing depending on which option is selected in the option menu? 使用另一个查询集过滤查询集 - Filtering a QuerySet With another QuerySet 如何根据 Django 中的 UserGroup 显示不同的内容? - How to show different content depending on UserGroup in Django? 由Query + Current Selected Option构成的Django Edit Form Queryset-如何获取? - Django Edit Form Queryset made of Query + Current Selected Option - How to get? Python Turtle:我想制作一个函数,该函数可以根据先前执行的函数执行另一个函数 - Python Turtle: I want to make a function that executes another function depending on what the previously executed function was 基于另一个查询集过滤一个查询集 - filtering a queryset based on another queryset 根据 Django 中另一个字段中选择的选项动态更新字段的值 - Dynamically updating values of a field depending on the choice selected in another field in Django 用另一个对象(来自不同的查询集)覆盖对象元素(来自查询集)以进行JSON编码 - Overwrite object element (from queryset) with another object (from a different queryset) for json encoding
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM