简体   繁体   English

管理页面django上的动态生成ForeignKey字段

[英]Dynamic generation ForeignKey fields on admin page django

I have models: 我有模特:

class CompanyInfo(models.Model):
    name = models.CharField('Имя компании',max_length=250)


class Staff(models.Model):
    company_name = models.ForeignKey(CompanyInfo)
    date = models.DateField( )
    name = models.CharField( max_length=30, )

class Relation(models.Model):
    company_name = models.ForeignKey(CompanyInfo)
    who = models.ForeignKey(Staff, related_name="who")
    with_whom = models.ForeignKey(Staff, related_name="with_whom")
    info = models.CharField( max_length=30, )

How I can create dynamic generation fields for WHO and WITH_WHOM form element on the admin-page? 如何创建动态发生区域WHOWITH_WHOM管理员,网页上表单元素? I chose COMPANY_NAME , and fields WHO and WITH_WHOM that show only people from that company. 我选择COMPANY_NAME ,并在WHOWITH_WHOM字段中仅显示该公司的人员。

Can you please elaborate in a bit more detail on what you mean by dynamic generation fields ? 您能否详细说明一下动态生成字段的含义? Otherwise, I'm afraid it's a bit difficult to help you because it's not really clear what your problem is. 否则,恐怕很难为您提供帮助,因为目前尚不清楚您的问题所在。

Besides that, let me tell you that your model design is rather odd, especially your Relation model. 除此之外,让我告诉您,您的模型设计非常奇怪,尤其是您的Relation模型。 If you want to establish a many-to-one relationship between two instances of the same model (I think that's what you are trying to accomplish here), then you should write it like that and get rid of your Relation model: 如果要在同一模型的两个实例之间建立多对一关系(我想这就是您要在此处完成的工作),则应这样编写并摆脱您的Relation模型:

class Staff(models.Model):
    with_whom = models.ForeignKey('self')

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM