django admin 中的外键表单字段

发布于 2024-12-18 07:18:27 字数 954 浏览 2 评论 0原文

我想根据 django admin 调整我的应用程序,使其更加用户友好。

添加优惠页面

以下管理页面适合以下情况:用户向客户创建报价,该报价已在系统中注册。但如果客户是新客户,则用户必须单击 + 并在弹出窗口中提交“添加新客户”表单。这太令人困惑了。

如何更改此行为以使用内联方式填充同一页面上的客户端数据,而不是在弹出窗口中?

这是我的 admin.py。

class OptionOffer(admin.ModelAdmin):
   fieldsets = (('', {'fields': (('client'), 'startDate', 'regNumber')}),)

class OptionClient(admin.ModelAdmin):
   fieldsets = (('', {'fields': ('code')}),)

和 model.py

class Client(models.Model):
    code = models.CharField(verbose_name=_("Code"), max_length=11)

class Offer(models.Model):
    client = models.ForeignKey(Client, verbose_name = _("Client"))
    startDate = models.DateTimeField(verbose_name = _("Start date"))
    regNumber = models.CharField(verbose_name=_("Registration number"), max_length=6)

I want to adjust my app based on django admin to be more userfriendly.

Add offer page

The following admin page is good when the user creates an Offer to the client, which is already registered in the system. But if the customer is a new one, then user has to click on a + and submit the 'Add new client' form in the popup. This is so confusing.

How can I change this behaviour to fill in the client data on the same page with inline but not in the popup?

Here is my admin.py.

class OptionOffer(admin.ModelAdmin):
   fieldsets = (('', {'fields': (('client'), 'startDate', 'regNumber')}),)

class OptionClient(admin.ModelAdmin):
   fieldsets = (('', {'fields': ('code')}),)

and the model.py

class Client(models.Model):
    code = models.CharField(verbose_name=_("Code"), max_length=11)

class Offer(models.Model):
    client = models.ForeignKey(Client, verbose_name = _("Client"))
    startDate = models.DateTimeField(verbose_name = _("Start date"))
    regNumber = models.CharField(verbose_name=_("Registration number"), max_length=6)

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

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

发布评论

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

评论(2

叹沉浮 2024-12-25 07:18:27

您可以使用一些 Javascript 框架(如 jQuery),并使用 iframe 或类似的东西在编辑页面中加载弹出窗口。或者使用一些灯箱样式的弹出窗口而不是 Django 标准弹出窗口。

You can use some Javascript framework like jQuery and load the popup in the edit page using an iframe or something similar. Or use some lightbox style popup instead of the Django standard one.

聽兲甴掵 2024-12-25 07:18:27

如果不对 django 管理进行一些严重的黑客攻击,你就不会得到更好的解决方案。您可能会遇到(或被指出)InlineModelFormsets,它在当前表单的底部提供一个表单,允许您内联添加客户。虽然这看起来像是您所需要的,但您很快就会发现它不适合您的用例,因为它需要从客户到报价的外键关系,而不是您拥有的来自报价的外键关系。向客户报价

You're not going to get a better solution without doing some serious hacking of the django admin. You might come across (or be pointed to) InlineModelFormsets, which provide a form at the bottom of your current form allowing you to add clients inline. While this might seem like what you need, you will soon see that it doesn't fit your use-case, as it requires a foreign key relationship from the Client to the Offer as opposed to what you have which is a ForeignKey relationship from the Offer to the Client

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