Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to skip object if it has not a relative objects? #1444

Unanswered
heckad asked this question in Q&A
Discussion options

I have Appeal and Company model. Each Appeal has a foreign key to Company and some fields witch able for filtering.
Example models

class Company(models.Model):
 pass
 
class Appeal(models.Model):
 status = models.CharField(max_length=128)
 company = models.ForeignKey(Company, on_delete=models.SET_NULL)

Example DjangoObjectType

class AppealType(DjangoObjectType):
 class Meta:
 model = models.Appeal
 fields = "__all__"
 filter_fields = {
 "status ": ["exact", "in"],
 }
 interfaces = (relay.Node,)
class CompanyType(DjangoObjectType):
 class Meta:
 model = models.Company
 fields = "__all__"
 interfaces = (relay.Node,)

Query

class Query(graphene.ObjectType):
 # Relay node
 node = relay.Node.Field()
 appeals = DjangoFilterConnectionField(fields.AppealType)
 companies = DjangoFilterConnectionField(fields.CompanyType)

I want to write query which returns only objects with appeals which satisfy a condition

Example query

query Companies {
 companies {
 edges {
 cursor
 node {
 id
 appeals(status_In: ["in progress"]) {
 edges {
 cursor
 node {
 id
 status
 }
 }
 }
 }
 }
 }
}

Now this query returns all companies and some of them have empty appeals. I want to exclude such companies from response. How to do it?

You must be logged in to vote

Replies: 1 comment

Comment options

@heckad
I think that you will need to create this condition in companies resolve.

companies = DjangoFilterConnectionField(fields.CompanyType)
def resolve_companies(self, args):
 with_appeal_status = args.get('with_appeal_status')
 companies_with_approved_appeals = Company.objects.annotate(
 num_approved_appeals=Count('appeal', filter=Q(appeal__status__in=with_appeal_status))
 ).filter(num_approved_appeals__gt=0)
 return companies_with_approved_appeals
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /