Django - 如何为模型的外键制作表单?

发布于 2024-11-02 11:07:57 字数 1069 浏览 0 评论 0原文

这就是我正在尝试做的事情。我想知道是否有人可以提出一个好的方法:

models.py:

class Color(models.Model):
    name = models.CharField(...

class Speed(models.Model):
    name = models.CharField(...

class Dog(models.Model):
    name = models.CharField(...
    color = models.ForeignKey(Color...
    speed = models.ForeignKey(Speed...

class DogRequest(models.Model):
    dog = models.ForeignKey(Dog...
    request_time = models.DateTimeField()

现在我想要一个页面,用户可以在其中输入或编辑 DogRequest,但表单应该如下所示:

Enter Dog Request:
---------------------
color (dropdown V)
speed (dropdown V)
|save|

那么我将如何设计该表单看法?问题是用户将输入狗的属性,而不是狗的属性。所以我需要根据用户输入的内容查找或创建一条狗记录。

到目前为止,我已经尝试过这样的事情:

class DogRequestForm(ModelForm):
    class Meta:
        model = DogRequest
        fields = ('request_time','color','speed')
    color = forms.ModelChoiceField(queryset=Color.objects.all())
    speed = forms.ModelChoiceField(queryset=Speed.objects.all())

现在假设即使有效,我应该在哪里放置代码来根据输入的颜色和速度来找出要使用(或创建)的狗记录?如何填写 DogRequest 中的表格?

Here's what I'm trying to do. I'm wondering if someone can suggest a good approach:

models.py:

class Color(models.Model):
    name = models.CharField(...

class Speed(models.Model):
    name = models.CharField(...

class Dog(models.Model):
    name = models.CharField(...
    color = models.ForeignKey(Color...
    speed = models.ForeignKey(Speed...

class DogRequest(models.Model):
    dog = models.ForeignKey(Dog...
    request_time = models.DateTimeField()

Now I want to have a page where a user can enter or edit a DogRequest, but the form should look like this:

Enter Dog Request:
---------------------
color (dropdown V)
speed (dropdown V)
|save|

So how would I design that form in the view? The problem is the user will be entering properties of a dog, but not a dog. So I need to find or create a dog record depending on what the user enters.

So far I've tried something like this:

class DogRequestForm(ModelForm):
    class Meta:
        model = DogRequest
        fields = ('request_time','color','speed')
    color = forms.ModelChoiceField(queryset=Color.objects.all())
    speed = forms.ModelChoiceField(queryset=Speed.objects.all())

Now assuming that even works, where do I put the code to figure out the Dog record to use (or create) depending on what's entered for color and speed? And how do I fill the form from a DogRequest?

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

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

发布评论

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

评论(1

铜锣湾横着走 2024-11-09 11:07:57

我认为您不想在这里使用 ModelForm。如果没有一些黑客攻击,它永远不会有效,因为在调用 is_valid() 之前您不会找到或创建狗对象。

相反,我只使用常规表单,然后重写 DogRequest 的 save 方法来查找或创建狗。

更新:回应评论中的后续问题,我还没有对此进行测试,但类似的东西应该可以工作。

class DogRequestForm(forms.Form):
    id = forms.IntegerField(required=False, widget=forms.HiddenInput())
    color = forms.ModelChoiceField(queryset=Color.objects.all())
    speed = forms.ModelChoiceField(queryset=Speed.objects.all())

然后,当您为编辑视图创建此表单的实例时,您需要使用如下内容实例化它:

data = {
    'id': dog_request_id,
    'color': dog_color,
    'speed': dog_speed,
}
form = DogRequestForm(data)

在其中填充当前的 dog_request_iddog_color来自现有 DogRequest 对象的dog_speed

I don't think you want to use a ModelForm here. It will never be valid without some hackery, since you won't have found or created the dog object before calling is_valid().

Instead, I'd just use a regular form, and then override the save method of DogRequest to find or create the dog.

Update: Responding to the followup question in the comment, I haven't tested this, but something like it should work.

class DogRequestForm(forms.Form):
    id = forms.IntegerField(required=False, widget=forms.HiddenInput())
    color = forms.ModelChoiceField(queryset=Color.objects.all())
    speed = forms.ModelChoiceField(queryset=Speed.objects.all())

and then when you create an instance of this form for your edit view you need to instantiate it with something like this:

data = {
    'id': dog_request_id,
    'color': dog_color,
    'speed': dog_speed,
}
form = DogRequestForm(data)

where you populate the current dog_request_id, dog_color and dog_speed from your existing DogRequest object.

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