I have the following two models:
Class Foo(models.model):
param1 = ...
param2 = ...
...
paramN = ...
Class Bar(models.model):
foo = models.ForeignKey(Foo)
...
...
GOAL: Compute a QuerySet of all instances of Foo such that more than 1 Bar instance is connected to it
I have been looking for a solution and this seems to work for everybody else
Foo.objects.annotate(num_bar=Count('bar')).filter(num_bar__gt=1)
This gave me a FieldError saying that 'bar' was not a possible field for Foo, I then tried 'bar_set' and also got the same error
Is there a chance I am implementing them wrong, or because they are old they are depreciated now? Any help would be appreciated!
traceback
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/ryan/.virtualenvs/project/local/lib/python2.7/site-packages/django/db/models/manager.py", line 127, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/ryan/.virtualenvs/project/local/lib/python2.7/site-packages/django/db/models/query.py", line 794, in annotate
obj.query.add_annotation(annotation, alias, is_summary=False)
File "/home/ryan/.virtualenvs/project/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 982, in add_annotation
summarize=is_summary)
File "/home/ryan/.virtualenvs/project/local/lib/python2.7/site-packages/django/db/models/aggregates.py", line 20, in resolve_expression
c = super(Aggregate, self).resolve_expression(query, allow_joins, reuse, summarize)
File "/home/ryan/.virtualenvs/project/local/lib/python2.7/site-packages/django/db/models/expressions.py", line 491, in resolve_expression
c.source_expressions[pos] = arg.resolve_expression(query, allow_joins, reuse, summarize, for_save)
File "/home/ryan/.virtualenvs/project/local/lib/python2.7/site-packages/django/db/models/expressions.py", line 448, in resolve_expression
return query.resolve_ref(self.name, allow_joins, reuse, summarize)
File "/home/ryan/.virtualenvs/project/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1532, in resolve_ref
self.get_initial_alias(), reuse)
File "/home/ryan/.virtualenvs/project/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1471, in setup_joins
names, opts, allow_many, fail_on_missing=True)
File "/home/ryan/.virtualenvs/project/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1396, in names_to_path
"Choices are: %s" % (name, ", ".join(available)))
FieldError: Cannot resolve keyword 'bar' into field. Choices are: param1, param2, param3, ..., paramN
version
my django version is 1.8.3
Aucun commentaire:
Enregistrer un commentaire