Django 表单中的数据验证(admin/auth/user)
在每个人页面的用户管理中,除了默认的 django 字段集之外,我还有两个其他内联(UserProfile 和一个名为“Extension”的模型)。 但是,每当我修改扩展内联中的字段时,我也希望能够处理/验证所有这些字段。
UserProfile:
user = models.ForeignKey(User, unique=True)
client = models.ForeignKey(Client)
分机号:
user = models.ForeignKey(User)
date_created = models.DateTimeField(auto_now_add=True, auto_now=True)
number = models.CharField(max_length=16, unique=False)
例如,当我编辑用户的分机号时,我希望能够获取每个字段内的所有值(这些值是动态的)。现在我像这样使用 self.data[""] :
extension_fields = [self.data["extension_set-0-number"],
self.data["extension_set-1-number"],
self.data["extension_set-2-number"]]
这带来的一个问题是我假设总是只有 3 个字段用于扩展,但这并不总是正确的。如何正确循环每个内联字段?
in my User administration for each individual's page, besides the default django fieldsets, I also have two other inlines (UserProfile and a model called "Extension").
However, whenever I modify fields in the Extension's inline, I want to be able to process/validate all these fields too.
UserProfile:
user = models.ForeignKey(User, unique=True)
client = models.ForeignKey(Client)
Extension:
user = models.ForeignKey(User)
date_created = models.DateTimeField(auto_now_add=True, auto_now=True)
number = models.CharField(max_length=16, unique=False)
For example, when I'm editing a user's extension numbers, I want to be able to grab all values inside each field (which are dynamic). Right now I'm using self.data[""] like this:
extension_fields = [self.data["extension_set-0-number"],
self.data["extension_set-1-number"],
self.data["extension_set-2-number"]]
One problem this poses is that I'm assuming there always will only be 3 fields for extension, which is not always true. How can I loop through each inline field correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 列表理解 来循环它们:
我不完全相信然而,这是实现这一点的正确方法。您没有提供足够的示例代码来准确解释您在做什么。如果您尝试处理和验证此数据,Django 应该为您执行此操作(可能使用 ModelForm)。您不必自己对此进行硬编码。
You can use a list comprehension to loop through them:
I'm not entirely convinced this is the correct way to implement this, however. You haven't provided enough example code to explain what you're doing exactly. If you're trying to process and validate this data, Django should be doing this for you (probably using a ModelForm). You shouldn't have to be hardcoding this yourself.