简体   繁体   English

如何基于另一个 model 的实例在 Django 中创建 model,但经过过滤

[英]How to create a model in Django based on the instance of another model, but filtered

I am new to Django, I am searching a way to create a model in Django based on the instance of another model, but filtered.我是 Django 的新手,我正在寻找一种基于另一个 Z20F93C83E374876A61DA0D4D16F36ABZ 的实例在 Django 中创建 model 的方法,但是过滤后的 model。

I have a table called Person, which has name and role, in my models.py:在我的 models.py 中有一个名为 Person 的表,它具有名称和角色:

class Person(models.Model):

id = models.UUIDField(primary_key=True) 
name = models.CharField(max_length=100) 
role = models.CharField(max_length=100) 
created_on = models.DateTimeField(auto_now_add=True)

class Meta:
    db_table = 'person'

What I want is to be able in my admin.py to call some subclass, which will display only actors (role="actors"):我想要的是能够在我的 admin.py 中调用一些子类,它只会显示演员(role="actors"):

@admin.register(Actor)
class Actor(admin.ModelAdmin):...

Is it possible?可能吗? Or do I have to create one more table only with actors?还是我必须只用演员再创建一张桌子?

I know that I can use list_filter = ('role',) , but this is not what I am searching for, I need actors exclusively.我知道我可以使用list_filter = ('role',) ,但这不是我要寻找的,我只需要演员。

I see solution as something like this:我认为解决方案是这样的:

class Actor(models.Model):
    objects = Person.objects.filter(role='actor')
    class Meta:
        verbose_name = 'actor'

or或者

class Actor(Person):
    self.objects.filter(role='actor')
    class Meta:
        verbose_name = 'actor'

That obviousely does not work.这显然是行不通的。

You can use a Proxy model for this:您可以为此使用代理 model:

class ActorManager(models.Manager):
    def get_queryset(self):
        return super().get_queryset().filter(role='actors')

class Actor(Person):
    objects = ActorManager()
    
    class Meta:
        proxy = True

Proxy models are just a proxy of the inherited model (Person here) ie no new table is created.代理模型只是继承的 model 的代理(Person here),即没有创建新表。 Here we have created a custom manager so that the roles are always filtered.在这里,我们创建了一个自定义管理器,以便始终过滤角色。 Now just register this model to the admin site.现在只需将此 model 注册到管理站点。 References: Proxy Models , Custom Managers参考: 代理模型自定义管理器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何基于Django中的属性值将模型实例复制到另一个模型 - How to copy a model instance to another model based on attribute value in Django 在创建另一个Django模型实例的同时创建一个Django模型实例 - Create a Django model instance concurrent with the creation of another Django model instance 如何将模型的实例传递给 Django 中的另一个模型? - How to pass instance of a model to another model in Django? 基于另一个模型的实例自动创建模型的实例 - Create instance of a model automatically based on instance of another model 创建另一个模型的实例时自动填充Django模型字段 - Autofill Django model fields when create instance of another model Django-如何在同一模型中基于另一个字段创建字段 - Django-How to create a field based on another field in the same model 如何在django中的另一个模型保存方法中更新模型实例? - How to update a model instance in another model save method in django? 在Django中从另一个模型的实例创建模型实例,而无需链接到原始实例源 - Create model instance from another model's instance in Django without link to original instance source 我如何基于 Django 中的另一个模型创建新的代理模型? - How do i create a new proxy model based on another model in django? 将Django模型实例保存到另一个模型中 - Saving django model instance into another model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM