I updated to PyCharm 4.0.5 yesterday and since then it has been marking some perfectly valid code as being an error. Basically the code is just iterating over a ManyToMany relationship. The code is shown below.
songs = []
for album in order.album_products.all():
album_songs = Song.objects.filter(album__exact=album.album_product.id)
if not album_songs:
for song in album_songs:
songs.append(song)
The line that is showing the error is this one:
for album in order.album_products.all():
and the error that is shown is this one:
Call 'all' directly on 'many-to-many with intermediate'. method 'all' can't be used with many-to-many relations if intermediate model is used.
I'm stumped on this one. This code has worked fine in Django for as long as I can remember. I've been using Django since version 1.0 or 1.1 and have always iterated over ManyToMany relationships using this method. Also looking at Stackoverflow answers shows the same code used by many other people as well.
Does anyone have any suggestions at all?
Seems like an error to me. I don't know where
method 'all' can't be used with many-to-many relations if intermediate model is used.
is coming from, but I'm not finding it in Django docs. In fact, Django docs uses it in an example, in the section "Extra fields on many-to-many relationships":
>>> beatles.members.all()
This bug is already reported in PyCharm.
Curious to real the answer, as https://docs.djangoproject.com/en/1.7/topics/db/models/#extra-fields-on-many-to-many-relationships shows the same code as beatles.members.all()
. But in this case, using the intermediate model
as mentioned in the error could probably be more efficient.
But be aware you do two queries per album of album_products, as album.album_product.id is a query to.
album_songs = Song.objects.filter(album__album_product=album.album_product_id)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With