Django - 如何替换嵌套在列表列表中的模型中的值
Django - how replace values in a model nested in a list of lists
我有两个模型:
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 是包含在 Document 模型中的 dnom 列表(添加了 MultipleChoiceField 字段)。
我现在想在 essai 中有一个与 piece_jointes 匹配但值为 dphoto 的列表。
确实在我的模板中,我希望能够为每个 Attacher 名称显示 pieces_jointes 中包含的每个 dnom 以及相应的 dphoto 值。有人可以帮忙吗?我完全迷路了...非常感谢。
请用英文命名您的变量。这伤害了我的眼睛(我是土生土长的法国人),但最重要的是它会帮助阅读代码的任何人理解它。
正如评论中所指出的,您可能希望修改您的模型以使用多对多关系。它就是为此而设计的。
据我了解,您打算使用 pieces_jointes
和 essai
来存储 2 个相关的 dnom
和 dphoto
列表。同样,这不是数据库的正确用法。您几乎不应该复制数据。作为记录,您可能在 form
中有一些字段没有直接链接到数据库中的字段。
有了你的模特,要得到一个pieces_jointes
对应的照片列表,试试这个:
essai = Document.filter(dnom__in = pieces_jointes).values_list('dphoto')
希望对你有帮助
我有两个模型:
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 是包含在 Document 模型中的 dnom 列表(添加了 MultipleChoiceField 字段)。 我现在想在 essai 中有一个与 piece_jointes 匹配但值为 dphoto 的列表。
确实在我的模板中,我希望能够为每个 Attacher 名称显示 pieces_jointes 中包含的每个 dnom 以及相应的 dphoto 值。有人可以帮忙吗?我完全迷路了...非常感谢。
请用英文命名您的变量。这伤害了我的眼睛(我是土生土长的法国人),但最重要的是它会帮助阅读代码的任何人理解它。
正如评论中所指出的,您可能希望修改您的模型以使用多对多关系。它就是为此而设计的。
据我了解,您打算使用
pieces_jointes
和essai
来存储 2 个相关的dnom
和dphoto
列表。同样,这不是数据库的正确用法。您几乎不应该复制数据。作为记录,您可能在form
中有一些字段没有直接链接到数据库中的字段。有了你的模特,要得到一个
pieces_jointes
对应的照片列表,试试这个:essai = Document.filter(dnom__in = pieces_jointes).values_list('dphoto')
希望对你有帮助