Django模型外键的内联动态默认值

发布于 2024-10-18 22:18:35 字数 1165 浏览 1 评论 0原文

有这 3 个模型

class Customer(models.Model):
    name = models.CharField(max_length=50)
    ....

class Agent(django.contrib.auth.models.User):
    regions = models.CharField(max_length=50, choices={...})
    ....

class Payment(models.Model):
    added_by = models.ForeignKey(Agent)
    customer = models.ForeignKey(Customer)
    date = models.DateField(default=datetime.date.today())
    amount = models.IntegerField(default=0)

我在 models.py和 admin.py 中

class PaymentInline(admin.TabularInline):
     model = Payment
     extra = 0

class CustomerAdmin(admin.ModelAdmin):
    inlines = [PaymentInline,]

,我有这些类:问题#1: 是否可以在客户更改页面的“PaymentInline”中将“代理”字段预设为当前登录的代理。 我们可以预设代理字段的值,就像 django 对“客户”字段所做的那样,该字段已经在内联中隐藏了。

问题#2: 或者有没有办法构建一个链接,并将 customer_id 和 agent_id “硬编码”在 url 中;在添加付款页面中,我们可以为客户字段和代理字段设置默认且不可编辑的值 例如, 此 url[1] 会将我们链接到正常的添加页面,但将 customer_field 和 agent_feild 设置为 Agent.objects.get(id=1) 和 Customer.objects.get(id=1)。 (或者我们可以隐藏这两个字段,因为它们无论如何都是不可编辑的)

[1]http://localhost:8000/admin/my_app/ payment/add/?customer_id=1&agent_id=1

有什么想法吗?

谢谢马克斯

I have these 3 models in models.py

class Customer(models.Model):
    name = models.CharField(max_length=50)
    ....

class Agent(django.contrib.auth.models.User):
    regions = models.CharField(max_length=50, choices={...})
    ....

class Payment(models.Model):
    added_by = models.ForeignKey(Agent)
    customer = models.ForeignKey(Customer)
    date = models.DateField(default=datetime.date.today())
    amount = models.IntegerField(default=0)

and also in my admin.py, I have these classes:

class PaymentInline(admin.TabularInline):
     model = Payment
     extra = 0

class CustomerAdmin(admin.ModelAdmin):
    inlines = [PaymentInline,]

Question#1:
is it possible to have the 'agent' field pre-set to the current logged in Agent in 'PaymentInline' in Customer change page.
we can preset the value for the agent field, just like the way that django does for 'Customer' field, which is hidden from the inline already.

Question#2:
or is there a way to construct a link, with the customer_id and agent_id "hardcoded" in the url; and in the add payment page, we could have the default and non-editable values for customer field and agent field
for example,
this url[1] will link us to the normal add page, but with the customer_field and agent_feild set to Agent.objects.get(id=1) and Customer.objects.get(id=1). (Or we can hide these 2 fields since they are non-editable anyway)

[1]http://localhost:8000/admin/my_app/payment/add/?customer_id=1&agent_id=1

Any thoughts?

Thanks

Max

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

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

发布评论

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

评论(2

浅笑轻吟梦一曲 2024-10-25 22:18:35

TabularInline 是一个 InlineModelAdmin,因此让我们定义 formfield_for_foreignkey():

class PaymentInline(admin.TabularInline):
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == 'added_by':
            kwargs['initial'] = request.user.id
            return db_field.formfield(**kwargs)
        return super(MyModelAdmin, self).formfield_for_foreignkey(
            db_field, request, **kwargs
        )

A TabularInline is an InlineModelAdmin and as such let's you define formfield_for_foreignkey():

class PaymentInline(admin.TabularInline):
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == 'added_by':
            kwargs['initial'] = request.user.id
            return db_field.formfield(**kwargs)
        return super(MyModelAdmin, self).formfield_for_foreignkey(
            db_field, request, **kwargs
        )
海之角 2024-10-25 22:18:35

问题一:
我的猜测是编辑内联模板,隐藏代理列并将其默认设置为登录用户。
问题2:
默认情况下,GET 参数会传递到表单。
如果您想让某些字段不可编辑,我认为您需要更改模板以检查这些参数是否存在,然后隐藏(或不隐藏)字段。或者,您可以将不同的表单传递给 ModelAdmin 视图(也在检查 GET 选项是否存在之后)。

托莫斯

Q1:
My guess would be to edit the inline template, hide the Agent column and set it by default to the logged in user.
Q2:
GET arguments are passed to the form by default.
If you want to make some fields not non-editable I think you need to alter the template to check for presence of those arguments and then hide the fields (or not). Alternatively you could pass different form to the ModelAdmin view (also after checking for the presence of GET options).

Tomus

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