Django admin - 一旦选择项目就允许内联整个
我正在尝试根据所选项目填充 Django 管理面板中的下拉列表。
我有一个客户模型
class Customer(BaseModel):
name = models.CharField(max_length=128)
company = models.ForeignKey("Company", models.PROTECT)
def __str__(self) -> str:
return f"{self.name}"
def save(self, **kwargs):
return super().save(**kwargs)
邀请模型
class Invite(BaseModel):
full_name = models.CharField(max_length=128, blank=True)
email = WIEmailField(unique=True)
customer = models.ForeignKey(
to="Customer",
on_delete=models.PROTECT,
related_name="invites",
)
客户邀请模型,定义邀请和邀请的客户
class CustomerLocationInvite(BaseModel):
location = models.ForeignKey(
to=Location
)
invite = models.ForeignKey(
to=Invite,
blank=True,
)
位置模型
class Location(BaseModel):
name = models.CharField(max_length=128)
company = models.ForeignKey(
to= Company,
on_delete=models.PROTECT
)
address = models.CharField(max_length=128)
内联
class CustomerInviteInline(admin.TabularInline):
model = CustomerLocationInvite
fields = ("invite", "location", "created_at", "modified_at")
readonly_fields = ("created_at", "modified_at")
extra = 0
创建新邀请时,是否可以:
- 选择公司后显示内联?
- 从内联中选择位置时,根据他们选择的公司过滤出位置?
I am trying to populate a dropdown in the Django admin panel based on a selected item.
I have a customer model
class Customer(BaseModel):
name = models.CharField(max_length=128)
company = models.ForeignKey("Company", models.PROTECT)
def __str__(self) -> str:
return f"{self.name}"
def save(self, **kwargs):
return super().save(**kwargs)
An invite model
class Invite(BaseModel):
full_name = models.CharField(max_length=128, blank=True)
email = WIEmailField(unique=True)
customer = models.ForeignKey(
to="Customer",
on_delete=models.PROTECT,
related_name="invites",
)
Customer Invite model that defines the invite and customer
class CustomerLocationInvite(BaseModel):
location = models.ForeignKey(
to=Location
)
invite = models.ForeignKey(
to=Invite,
blank=True,
)
Location Model
class Location(BaseModel):
name = models.CharField(max_length=128)
company = models.ForeignKey(
to= Company,
on_delete=models.PROTECT
)
address = models.CharField(max_length=128)
Inline for invite
class CustomerInviteInline(admin.TabularInline):
model = CustomerLocationInvite
fields = ("invite", "location", "created_at", "modified_at")
readonly_fields = ("created_at", "modified_at")
extra = 0
When creating a new Invite, Is it possible to:
- Display the inline once a company has been selected?
- When selecting a location from the inline, Filter out the locations based on the company they selected?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CustomerLocationInvite
获取位置,并从该位置获取公司?如果是这样,也许你可以尝试Where the location's id can be receive by
但是既然你说已经选择了该公司,你应该有它的ID - 这就是你将替换
companyid 与。然后,您可以从第一行收到的
CustomerLocationInvite
对象中检索内联管理。SO参考
文档参考
Company
模型。CustomerLocationInvite
, and get the company from the location? If that is so, maybe you can tryWhere the location's id can be received by
But then since you said that the company is already selected, you should have its ID -- that is the value you will replace
companyid
with. You can then retrieve the inline admin from theCustomerLocationInvite
object that you received in the first line.SO reference
Doc reference
Company
model as well.