我正在努力完成以下工作:
class DownloadContentFiles(models.Model):
download_content = models.ForeignKey('DownloadContent')
media_file = models.ForeignKey(MediaFile)
class DownloadContent(models.Model):
files = models.ManyToManyField(MediaFile, through=DownloadContentFiles)
class Meta:
abstract=True我明白为什么这个不起作用了。因为DownloadContent上的摘要。
是否有为contenttype字段指定中间模型的变通方法?
发布于 2013-04-04 17:33:12
通常,如果在创建字段(如选项列表)或具体的Django模型时需要更多信息(如您所做的那样),可以使用initialize_type。
class DownloadContent(models.Model):
@classmethod
def initialize_type(cls):
cls.add_to_class('files', ... your model field ...)MediaFileContent使用此方法添加type选择器:
https://github.com/feincms/feincms/blob/master/feincms/content/medialibrary/models.py#L58
但是,在你的例子中这是行不通的,因为你还必须动态地创建through模型。这是因为对于每个具体的DownloadContent,您都需要另一个具体的DownloadContentFiles模型。
您可以通过使用type内置动态创建新的DownloadContentFiles具体类来实现这一点(在将DownloadContent与不同的内容管理系统基础一起使用时,请注意名称冲突,例如page.Page和elephantblog.Entry)。
也许是实现你想要的东西的更简单的方法:
Downloads模型,并将files ManyToManyField添加到此类中,而不是只包含ForeignKey(Downloads)的DownloadContent
是的,你需要另一种型号。这可能是值得的,因为您可以为Downloads构建更好的编辑界面,而且页面编辑器也得到了简化,因为您只需选择一个现有的Downloads模型就可以在页面上显示它们。
发布于 2013-04-04 17:31:22
也许向create_content_type显式定义class_name可能对您有用。如下所示:
class DownloadContentFiles(models.Model):
download_content = models.ForeignKey('MyDownloadContent')
media_file = models.ForeignKey(MediaFile)
class DownloadContent(models.Model):
files = models.ManyToManyField(MediaFile, through=DownloadContentFiles)
class Meta:
abstract=True
Page.create_content_type(DownloadContent, class_name="MyDownloadContent")https://stackoverflow.com/questions/15787185
复制相似问题