Django - How to make a form for a model's foreign keys?

≯℡__Kan透↙ 提交于 2020-05-25 04:31:50

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/5720287/django-how-to-make-a-form-for-a-models-foreign-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!