Django-如何替换嵌套在列表列表中的模型中的值

Django-如何替换嵌套在列表列表中的模型中的值,django,django-models,Django,Django Models,我有两种型号: class Document (models.Model): dnom = JSONField(blank=True, null=True) dphoto = models.ImageField(upload_to='profile_document', blank=False) ddate_ajout = models.CharField(max_length=128, unique=False) class Attacher (models.Mod

我有两种型号:

class Document (models.Model):
    dnom = JSONField(blank=True, null=True)
    dphoto = models.ImageField(upload_to='profile_document', blank=False)
    ddate_ajout = models.CharField(max_length=128, unique=False)


class Attacher (models.Model):
    name = models.CharField(max_length=128, unique=True)
    pieces_jointes = JSONField(blank=True, null=True)
    essai = JSONField(blank=True, null=True)
和两个相关表格:

class AttacherForm(forms.ModelForm):
    name = forms.CharField(max_length=128, help_text="Please enter the name.")
    pieces_jointes = forms.MultipleChoiceField(choices=[], widget=forms.CheckboxSelectMultiple)

    def __init__(self, *args, **kwargs):
        super(AttacherForm, self).__init__(*args, **kwargs)
        self.fields['pieces_jointes'].choices = [(document.dnom, document.dnom) for document in Document.objects.all()]

    class Meta:
        # Provide an association between the ModelForm and a model
        model = Attacher
        fields = ('name','pieces_jointes')

class DocumentForm(forms.ModelForm):
    dnom = forms.CharField(max_length=128, help_text="Please enter the  name.")
    dphoto = forms.FileField()
    ddate_ajout = forms.CharField(max_length=128, help_text="Please enter the name name.")

        # Provide an association between the ModelForm and a model
        model = Document
        fields = ('dnom','dphoto','ddate_ajout')
因此,Attacher中的模型字段pieces_jointes是文档模型中包含的dnom列表(添加了一个MultipleChiiceField字段)。 现在,我想在essai中有一个列表,该列表将匹配工件,但其值为dphoto

事实上,在我的模板中,我希望能够为每个附件名称显示包含在片段中的每个dnom与相应的dphoto值。有人能帮忙吗?我完全迷路了。。。非常感谢

  • 请用英语命名你的变量。这会伤害我的眼睛(我是法国人),但最重要的是,这会帮助任何阅读代码的人理解代码

  • 正如注释中所指出的,您可能希望修改模型以使用多个关系。它的目标就是这样

  • 如果我理解,您计划使用
    pieces\u jointes
    essai
    存储两个相关
    dnom
    dphoto
    列表。同样,这不是数据库的正确用法。您几乎不应该复制数据。对于记录,您可能有
    格式的字段
    ,这些字段未直接链接到数据库中的字段

  • 对于您的模型,要获取与
    片段\u jointes
    对应的照片列表,请尝试以下操作:

    essai=Document.filter(dnom\uuuu in=pieces\ujointes).值列表('dphoto')


  • 我希望这能有所帮助

    您的问题对我来说并不清楚:您所说的是“包含在片段中的dnom”—如果要在数据中正确表示这种关系,拥有一个外键或多个关系不是更好吗?