Django 管理站点反向外键内联

发布于 2024-08-16 00:50:33 字数 501 浏览 1 评论 0原文

我有这些模型:(

伪代码)

Course:  
  ForeignKey(Outline, null=True, blank=True)  
  ForeignKey(OutlineFile, null=True, blank=True)

Outline:    
  //data

OutlineFile:    
  //different data

情况是任何课程都可以有与之关联的大纲,和/或大纲文件,或两者都没有。一个大纲可以与多个课程关联,同样,一个大纲文件可以与多个课程关联。然而,一门课程最多只能有其中一个。

我想要的是让课程更改管理页面显示所有课程字段,以及每个大纲和大纲文件的下拉列表。如果随后选择其中一个,我希望显示该大纲的字段并可对其进行修改,就像内联字段一样。

我应该以某种方式重组我的模型,还是它们的结构已经足够了?无论如何,有没有办法在当前内联系统的范围内做我想做的事情?

最后,如果不可能,我从哪里开始以定制方式进行呢?

I have these models:

(pseudocode)

Course:  
  ForeignKey(Outline, null=True, blank=True)  
  ForeignKey(OutlineFile, null=True, blank=True)

Outline:    
  //data

OutlineFile:    
  //different data

The situation is that any course can have an Outline associated with it, and/or an OutlineFile, or neither. An Outline can be associated with multiple courses, similarly an OutlineFile can be associated with multiple courses. However, a course will only ever have at most one of each.

What I want is to have the Course change admin page show all the Course fields, and a drop down for each of Outline and OutlineFile. If one is then selected, I want the fields for that Outline to be displayed and modifiable, just like an inline field.

Should I be restructuring my models somehow, or are they structured adequately already? Is there anyway to do what I want within the confines of the current inlines system?

Lastly, if it's not possible, where do I start in doing it in a custom fashion?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一曲琵琶半遮面シ 2024-08-23 00:50:33

您正在以相反的方式进行操作:

class Course(models.Model):  
  # Foreign key is defined only in related fields

class Outline(models.Model):    
  course = models.ForeignKey(Course,
    related_name='outlines', # Or whatever you choose
    null=True, # These two mean your FK relation is basically optional
    blank=True
    )

class OutlineFile(models.Model):    
  # Same structure as above

当您创建 std 表单时,此模型结构将创建一个像您默认指定的下拉列表。

You are doing it the other way around:

class Course(models.Model):  
  # Foreign key is defined only in related fields

class Outline(models.Model):    
  course = models.ForeignKey(Course,
    related_name='outlines', # Or whatever you choose
    null=True, # These two mean your FK relation is basically optional
    blank=True
    )

class OutlineFile(models.Model):    
  # Same structure as above

When you create the std forms, this model structure will create a dropdown like you specify by default.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文