get_object_or_404继承的类

时间:2019-07-16 17:52:49

标签: django django-models django-views

我有一个抽象类Type,它有3个继承的类Type1 Type2 Type3,这3个类的属性中有70%是相同的。

我想要这样;

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

但是Type是抽象类,我无法获取对象或过滤器

views.py(现在)

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,)

1 个答案:

答案 0 :(得分:0)

您不能混合使用这三个模型,因为它们是数据库中的不同模型和不同表。您需要创建自己的get_object_or_404接受查询集,然后在其中输入混合查询集, 或分别测试所有3个模型,并在没有任何对象的情况下自行提高404。

或者如果此类中70%的数据相同,也许您可​​以只使用一个大模型将它们全部保留?只需将Type1 Type2 Type3合并到一个通用Type模型。 IMO比在名称中创建3个具有数字的类似模型更合理