0

I'm trying to access a slug field from a different urls.py file, and I'm getting this error

FieldError at /new-api/tournaments/fifa-world-cup/teams/ Unsupported lookup 'custom_url' for ForeignKey or join on the field not permitted. 

I'm wondering if the reason I'm getting this error is because you cant do that or if it's another reason

I have 2 files for urls, one includes the other in it,

urls.py (tournament)

urlpatterns = [ path("", views.getNewTournaments, name="tournaments"), path("<slug:custom_url>/", views.getNewTournament, name="tournament"), path("create/", views.postNewTournament, name="post-tournament"), path("<slug:custom_url>/teams/", include("teams.urls"), name="tournament-teams"), ] 

urls.py (teams)

urlpatterns = [ path("", views.teams.as_view(), name="teams"), ] 

Here are the views.py files

views.py (tournaments)

@api_view(["GET"]) def getNewTournaments(request): tournaments = NewTournament.objects.all() serializer = NewTournamentSerializer(tournaments, many=True) return Response(serializer.data) 

views.py (teams)

class teams(APIView): def get(self, request, custom_url): teams = Team.objects.filter(tournament__custom_url=custom_url) serializer = TeamSerializer(teams, many=True) return Response(serializer.data) 
5
  • This looks like DRF and I'm not yet familiar with DRF. Perhaps, you are trying to map your app urls to views before including it. I'm thinking path("<slug:custom_url>/teams/", include("teams.urls"), name="tournament-teams"), should be included in your project urls.py, just below path(admin/, admin.site.urls), Commented Jul 27, 2024 at 20:00
  • 1
    @16171413 I edited the post to add the DRF tag... sorry about that haha. But I edited the teams api to be like this ` class teams(APIView): def get(self, request, custom_url): # teams = Team.objects.filter(tournament__custom_url=custom_url) teams = Team.objects.all() serializer = TeamSerializer(teams, many=True) return Response(serializer.data)` And it let me see the data, so I'm assuming the problem is with the view trying to get the data Commented Jul 27, 2024 at 20:13
  • Note that create/ will never be called, since that is also a slug, so it will take the second path, not the third. Commented Jul 27, 2024 at 20:47
  • @willeM_VanOnsem so should I move it above the slug url? Also thanks for letting me know about that Commented Jul 28, 2024 at 1:41
  • @willeM_VanOnsem so I just tested it and your right, thanks for letting me know about that Commented Jul 28, 2024 at 4:25

1 Answer 1

0

So I realized that my error was my model for the teams had the past model version of the tournaments

Old

class Team(models.Model): tournament = models.ForeignKey(Tournament, on_delete=models.CASCADE) 

New

class Team(models.Model): tournament = models.ForeignKey(NewTournament, on_delete=models.CASCADE) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.