문제

3 개의 수업이 쿼리하고 싶습니다.데이터베이스가 작업을 수행하도록하고 싶습니다.사용자 개체에서 단일 쿼리를 실행하여 일치하는 모든 관련 필드를 가져올 수 있습니까?내 코드에서 세 테이블에 가입하지 않으려 고 노력하고 있습니다.코드에서 해당하는 경우 세 가지 클래스 모두를 쿼리 한 다음 일치 항목을 유지하면서 중복을 제거합니다.

Query: Get all users whose name contains "William", category is "Single" and alias is "Bill". 

class ModelA(models.Model):
    user = models.ForeignKey(User,related_name="%(class)s",null=False)
    name = models.CharField(max_length=70)

    def __unicode__(self):
        return u'%s' % (self.name)

class ModelB(models.Model):
    user = models.ForeignKey(User,related_name="%(class)s",null=False)
    category = models.CharField(max_length=70)

    def __unicode__(self):
        return u'%s' % (self.category)

class ModelC(models.Model):
    user = models.ForeignKey(User,related_name="%(class)s",null=False)
    alias = models.CharField(max_length=70)

    def __unicode__(self):
        return u'%s' % (self.alias)

Note: this is an example only, and I am not looking to combine all the info in a single table.
.

도움이 되었습니까?

해결책

세 테이블에 가입하지 않고이 작업을 수행 할 수 없습니다.

User.objects.filter(modela__name=u'William', modelb__category=u'Single',
  modelc__alias=u'Bill')
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top