我遇到的问题是将两个模型的链式搜索传递给模板,这两个数据库通过 custom_id 链接,如下所示:
注:这是一个原模型交互的例子,很长
模型:
from django.db import models
class Model (models.Model):
custom_id = (max_length = 10)
data = models.CharField (max_length = 100)
Model1 class (models.Model):
models.IntegerField custom_id = (max_length = 10)
Model2 class (models.Model):
relation2 = models.OneToOneField (Model1)
class UserProfile (models.Model):
models.ForeignKey user = (User, unique = True)
relation1 = models.ManyToManyField (Model2)
这向我显示了页面上所有这些 custom_id 用户登录
custom = Model1.objects.filter (model2__userprofile__user__username = user)
问题是如何解雇与此 custom_id 相关的所有数据?
在终端中向我显示了数据以及与此的两个交互:
for element in custom:
customid = str(elemento.custom_id)
data = Model.objects.filter(customid=customid)
但我只在数据中保留一个。