Enes
Enes

Reputation: 341

get_object_or_404 inherited classes

I have a abstract class Type and it have 3 inherited classes Type1 Type2 Type3 70% of these 3 class properties are the same.

i want like this;

Tip=get_object_or_404(Type,tournament__slug=tournamentslug,slug=slug,game__slug=gameslug,) 

However Type is abstract class i cannot get object or filter

views.py (now)

def game_detail(request,tournamentslug,slug,gameslug):
        tip1=get_object_or_404(Type1, tournament__slug=tournamentslug,slug=slug,game__slug=gameslug,)
        tip2=get_object_or_404(Type2,tournament__slug=tournamentslug,slug=slug,game__slug=gameslug,)
        tip3=get_object_or_404(Type3,tournament__slug=tournamentslug,slug=slug,game__slug=gameslug,)

Upvotes: 0

Views: 100

Answers (1)

Lunder4
Lunder4

Reputation: 56

You cannot mix those 3 models, because they are a different models and different tables in database. You need to create your own get_object_or_404 what accepts queryset, and input there a mixed queryset, or test all 3 models separately and raise 404 by yourself when there's no any object.

Or if 70% of data in this classes are that same, maybe you can just keep them all using one big model? Just merge Type1 Type2 Type3 to one universal Type model. IMO it's more reasonable than creating 3 similar models with number inside the name

Upvotes: 1

Related Questions