如何在Django中使用动态外键?

如何在Django中使用动态外键?,第1张

如何在Django中使用动态外键?

这是我的方法

from django.contrib.contenttypes.models import ContentTypefrom django.contrib.contenttypes import fieldsclass Photo(models.Model):    picture = models.ImageField(null=True, upload_to='./images/')    caption = models.CharField(_("Optional caption"),max_length=100,null=True, blank=True)    content_type = models.ForeignKey(ContentType)    object_id = models.PositiveIntegerField()    content_object = fields.GenericForeignKey('content_type', 'object_id')class Article(models.Model):    ....    images     = fields.GenericRelation(Photo)

您将添加类似

    content_type = models.ForeignKey(ContentType)    object_id = models.PositiveIntegerField()    content_object = fields.GenericForeignKey('content_type', 'object_id')

喜爱和

    fields.GenericRelation(Faves)

到文章和演员表

contenttypes
docs



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5655725.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存