嘿,我想从数据库中查询manyToManyField
的每个人似乎都行不通。我想从数据库模型中获取所有框架不兼容的列表。但是当我进行查询时,我什么也没得到。
这是我的模特:
class Framework(models.Model):
name = models.CharField(max_length=200)
language = models.ManyToManyField(Language)
version = models.CharField(max_length=20)
typeOfFramework = models.ForeignKey(TypeOfProgram, on_delete=models.CASCADE)
pros = models.ManyToManyField(Pro)
nombreAvantage = models.IntegerField(default = 0)
cons = models.ManyToManyField(Con)
nombreDesavantage = models.IntegerField(default = 0)
additionalInformation = models.ForeignKey(web,blank=True,null=True ,default = None ,on_delete=models.CASCADE)
incompatibility = models.ManyToManyField("self",blank=True)
def __str__(self):
template = '{0.name} {0.version} {0.typeOfFramework} {0.nombreAvantage} {0.nombreDesavantage} {0.additionalInformation}'
return template.format(self)
def getLanguage(self):
return ", ".join([p.name for p in self.language.all()])
def getPros(self):
return ", ".join([p.advantage for p in self.pros.all()])
def getCons(self):
return ", ".join([p.disadvantage for p in self.cons.all()])
def getIncompatibility(self):
return ", ".join([(p.name + ' '+ p.version) for p in self.incompatibility.all()])
def __iter__(self):
return [ self.name,
self.language,
self.version,
self.typeOfFramework,
self.pros,
self.cons,
self.additionalInformation
]
class DataPro(models.Model):
advantage = models.CharField(max_length=200)
def __str__(self):
template = '{0.advantage}'
return template.format(self)
class DataCon(models.Model):
disadvantage = models.CharField(max_length=200)
def __str__(self):
template = '{0.disadvantage}'
return template.format(self)
class Database(models.Model):
name = models.CharField(max_length=200)
version = models.CharField(max_length=20)
pros = models.ManyToManyField(DataPro)
nombreAvantage = models.IntegerField(default = 0)
cons = models.ManyToManyField(DataCon)
nombreDesavantage = models.IntegerField(default = 0)
frameworkIncompatibility = models.ManyToManyField(Framework, blank=True)
def __str__(self):
template = '{0.name} {0.version} {0.nombreAvantage} {0.nombreDesavantage}'
return template.format(self)
def getDataPros(self):
return ", ".join([p.advantage for p in self.pros.all()])
def getDataCons(self):
return ", ".join([p.disadvantage for p in self.cons.all()])
def getFrameworkIncompatibility(self):
return ", ".join([p.name + " "+ p.version for p in self.frameworkIncompatibility.all()])
这是我要获取
列表的视图 listOfDatabase = Database.objects.values_list()
databaseIncompatibility = []
for s in listOfDatabase:
databaseIncompatibility.append(s.objects.filter(frameworkIncompatibility__in=Framework.objects.all()))
listDatabase = [(q[0],q[1],q[2],q[3],q[4])for q in listOfDatabase]
#ID, name, version, con, pro
很抱歉,如果这是一个愚蠢的问题,那是我的第一个django
项目,我对如何做一些事情有些迷茫。
谢谢大家的回答。