Mam dwa modele, które są połączone przez inny model poprzez wiele do wielu relacji.Tastypie, filtrowanie wielu do wielu relacji
Oto same modele
class Posts(models.Model):
id = models.CharField(max_length=108, primary_key=True)
tags = models.ManyToManyField('Tags', through='PostTags')
class Tags(models.Model):
id = models.CharField(max_length=108, primary_key=True)
posts = models.ManyToManyField('Posts', through='PostTags')
class PostTags(models.Model):
id = models.CharField(max_length=108, primary_key=True)
deleted = models.IntegerField()
post_id = models.ForeignKey('Posts', db_column='post_field')
tag_id = models.ForeignKey('Tags', db_column='tag_field')
a tastypie zasoby
class PostsResource(ModelResource):
tags = fields.ToManyField('django_app.api.TagsResource', 'tags', null=True)
class Meta:
queryset = Posts.objects.filter(deleted=0)
resource_name = 'posts'
class TagsResource(ModelResource):
posts = fields.ToManyField('django_app.api.PostsResource', 'posts', null=True)
class Meta:
queryset = Tags.objects.filter(deleted=0)
resource_name = 'tags'
Na stole posttags jest usunięta flaga, czy to możliwe, aby powrócić połączonych tylko wyników, gdy usunięte flagi PostTags to jest 0?
Próbowałem filtr atrybutu this w tastypie, ale wydaje się, że zależy mu tylko na flagach w połączonej tabeli (tj. Znacznikach lub postach), a nie na faktycznej tabeli wykonującej łączenie.
Czy próbowałeś 'queryset = Posts.objects.filter (posttags__deleted = 0)' w Meta dla każdego zasobu? – astevanovic
To dziwne, że połowa działa. Tak jak w przypadku, gdy wypisuję queryset.query, wykonuje on poprawną instrukcję SQL (tak jak w miejscu gdzie usunięto = 0). Ale wyniki zwracane przez żądanie APi nadal wydają się ignorować flagę. – Shane