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”—如果要在数据中正确表示这种关系,拥有一个外键或多个关系不是更好吗?