Django admin - 一旦选择项目就允许内联整个

发布于 2025-01-21 02:30:04 字数 1326 浏览 2 评论 0原文

我正在尝试根据所选项目填充 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

创建新邀请时,是否可以:

  1. 选择公司后显示内联?
  2. 从内联中选择位置时,根据他们选择的公司过滤出位置?

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:

  1. Display the inline once a company has been selected?
  2. When selecting a location from the inline, Filter out the locations based on the company they selected?

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

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

发布评论

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

评论(1

最初的梦 2025-01-28 02:30:04
  1. 我假设您想从 CustomerLocationInvite 获取位置,并从该位置获取公司?如果是这样,也许你可以尝试
CustomerLocationInvite.objects.filter(location__id=locationid)

Where the location's id can be receive by

locationid = Location.objects.filter(company__id=companyid).id

但是既然你说已经选择了该公司,你应该有它的ID - 这就是你将替换companyid 与。然后,您可以从第一行收到的 CustomerLocationInvite 对象中检索内联管理。

SO参考
文档参考

  1. 我是不确定你的意思。您是否获得了公司列表,或者您是否获得了地点列表?也就是说,请同时粘贴您的 Company 模型。
  1. I assume you want to get the location from CustomerLocationInvite, and get the company from the location? If that is so, maybe you can try
CustomerLocationInvite.objects.filter(location__id=locationid)

Where the location's id can be received by

locationid = Location.objects.filter(company__id=companyid).id

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 the CustomerLocationInvite object that you received in the first line.

SO reference
Doc reference

  1. I'm not sure what you mean. Are you getting the list of companies, or are you getting the list of locations? That being said, please paste your Company model as well.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文