社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

Django模型MIXIN不像预期的那样工作

Milano • 5 年前 • 1267 次点击  

PipedriveSync GenericForeignKey 所以任何模特都可以 对象相关。

class PipedriveSync(TimeStampedModel):
    ...
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')

GenericRelation user.pipedrivesyncs.all()

看一看 User

class User(AbstractUser):
    pipedrivesyncs = GenericRelation('pipedrive.PipedriveSync')

因为我必须说明 pipedrivesyncs 对于很多模型,我决定创建一个MIXIN(有两种方法,但现在没关系)。

class PipedriveSyncRelatedMixin():
    pipedrivesyncs = GenericRelation('pipedrive.PipedriveSync')

class User(PipedriveSyncRelatedMixin,AbstractUser):
    pass

问题是 Mixin

具体情况 管道驱动同步 手动:

> u = User.objects.first()
> u.pipedrivesyncs.first()
> <PipedriveSync: PipedriveSync object (20)>

混合蛋白

> u = User.objects.first()
> u.pipedrivesyncs.first()
> AttributeError: 'GenericRelation' object has no attribute 'first'

混合蛋白

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/53906
 
1267 次点击  
文章 [ 1 ]  |  最新文章 5 年前
Sigdev
Reply   •   1 楼
Sigdev    6 年前

你的混血必须是抽象的,遗传应该来自模型。

class PipedriveSyncRelatedMixin(models.Model):
    pipedrivesyncs = GenericRelation('pipedrive.PipedriveSync')

    class Meta:
        abstract = True