2
我有兩個型號,我想找出值的公共集的IDS的基礎上兩個django模型的交集?
第一種模式:
from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User
# WE ARE AT MODELS/UNIVERSITIES
class Universities(models.Model):
id = models.IntegerField(db_column="id", max_length=11, help_text="")
name = models.CharField(db_column="name", max_length=255, help_text="")
abbreviation = models.CharField(db_column="abbreviation", max_length=255, help_text="")
address = models.CharField(db_column="address", max_length=255, help_text="")
status = models.BooleanField(db_column="status", default=False, help_text="")
createdAt = models.DateTimeField(db_column='createdAt', auto_now=True, help_text="")
modifiedAt = models.DateTimeField(db_column='modifiedAt', auto_now=True, help_text="")
updatedBy = models.ForeignKey(User,db_column="updatedBy",help_text="Logged in user updated by ......")
class Meta:
managed = False
get_latest_by = 'createdAt'
db_table = 'universities'
而其他型號:
from __future__ import unicode_literals
from django.db import models
class NewsUniversityMappings(models.Model):
id = models.IntegerField(db_column="id", max_length=11, help_text="")
newsMappingId = models.IntegerField(db_column='newsMappingId' ,max_length=11, help_text="")
universityId = models.IntegerField(db_column='universityId', max_length=11, help_text="")
createdAt = models.DateTimeField(db_column='createdAt', auto_now=True, help_text="")
class Meta:
managed = False
db_table = 'news_university_mappings'
現在,在我看來,我已經爲兩種模型創建了對象:
def add_promoter_news(request, mapping_id=None, news_virtual_id=None):
try:
university_all_list = Universities.objects.using("cms").all
published_university_list = NewsUniversityMappings.objects.using("cms").filter(newsMappingId=news_virtual_id).all()
我想找到在Universities
模型和universityId
的NewsUniversityMappings
模型的基礎上的兩個模型的共同點。任何幫助,將不勝感激。 在此先感謝
這是我需要的,但在這裏university_list = university_all_list.filter(id__in = published_university_IDS_list)它顯示錯誤說'功能'對象沒有屬性'過濾器' –
正確,忘記在'university_all_list = Universities.objects.using(「 cms「)。all()',當然函數'all'沒有'filter'。嘗試編輯答案。 –