I'm trying to write a query that when an artist id is searched, that artist's info and all of their pieces are the result, basically a join query between Artist
and ArtistPiece
#models.py
class Artist(models.Model):
name = models.CharField(max_length=100)
artist = models.CharField(max_length=150)
created_by = models.ForeignKey(User)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class ArtistPiece(models.Model):
artist = models.ForeignKey(Artist, on_delete = models.CASCADE)
piece_name = models.CharField(max_length=50)
details = models.CharField(max_length=100)
#views.py
from .models import Artist, ArtistPiece
class EditArtistViewSet(viewsets.ViewSet):
def edit(self,request,artist_id):
artist = Artist.objects.select_related().get(pk=artist_id)
data = model_to_dict(artist)
return render(
request, 'ArtistInfo/edit.html', {
'editing': 'true',
'edit_data': json.dumps(data),
})
def submit_edit(self,request):
return render(request, 'ArtistInfo/edit.html')
The above works, but it doesn't select any of the data from the ArtistPiece model, despite the primary key. Then after reading this post about using select_related
I realized that rather than looking up the artist, I'd have to look up ArtistPiece by the foreign_key, and use select_related to find the info about the artist.
I then tried this
def edit(self, request, artist_id):
artist = ArtistPiece.objects.get()
data = model_to_dict(artist)
Which resulted in the following error get()
returned more than one ArtistPiece -- it returned 5!. Searching, I found this post which states that I need to use filter rather than get. I then tried this
def edit(self, request, artist_id):
artist = ArtistPiece.objects.select_related('artist').filter(artist=artist_id)
data = model_to_dict(artist)
Which gave the following error 'QuerySet' object has no attribute '_meta'
. I tried searching for this error and found this which basically suggests to use get, which seems to go against everything else I found.
To summarize, I just want to select an artist and their pieces but I can't seem to figure out how.
Edit - I tried a few things and made some progress, but it's still not working correctly
Just to be clear I'm trying to write a query that will look up an artist by their id, and the result will be the artist's info and the artist's pieces. I can query and retrieve the Artist
model based on an artist_id or the ArtistPiece
model, but not one query that will return both Artist
and all of the ArtistPiece
entries associated to that artist.
Adding related_name='pieces'
to my model as @bignose has suggested, I then tried artist = Artist.objects.select_related('pieces').filter(pk=artist_id)
but that still gave the no attribute.... error above. I then changed data = model_to_dict(artist)
to data = [model_to_dict(a) for a in artist]
and it worked, however the data was only the artist and nothing from the ArtistPiece
model.
Instead of querying the Artist
model I instead tried to query the ArtistPiece
model as such: artist = ArtistPiece.objects.select_related('artist').filter(artist=artist_id)
, but then only the artist's pieces were returned and no info from the Artist
model.