在Django表单中使用外键引用对象

时间:2015-02-16 10:35:30

标签: python django django-models django-forms django-views

我做了很多寻找我面临的问题,但无法找到合适的解决方案。我是Django的初学者

我正在创建一个项目,用户可以在其中提出愿望,并为其他用户分配该愿望,然后他们可以绘制并提交。

我为询问和获得愿望创建了视图,但在提交愿望草图时面临问题。我不知道如何在当前用户的add_sketch表单中只显示那些愿望,然后使用这个新草图更新草图模型。 现在我只是使用charField来上传草图。这是代码

models.py

class Wish(models.Model):

    content = models.CharField(max_length=500)
    wisher = models.ForeignKey(User)
    created_on = models.DateTimeField(auto_now_add=True)
    locked = models.BooleanField(default=False)

    class Meta():
        verbose_name_plural = 'Wishes'

    def __unicode__(self):
        return self.content



class Sketch(models.Model):

    wish = models.ForeignKey(Wish)
    sketcher = models.ForeignKey(User)
    image_temp = models.CharField(max_length=128)
    likes = models.IntegerField(default=0)
    assigned_on = models.DateTimeField(auto_now_add=True)
    submitted_on = models.DateTimeField(auto_now=True)

    class Meta():
        verbose_name_plural = 'Sketches'

    def __unicode__(self):
        return "Sketch for \""+ self.wish.content + "\""

views.py

@login_required
def add_sketch(request):
    if request.method == "POST":
        sketch_form = SketchForm(request.POST)

        if sketch_form.is_valid():
            add_sketch = sketch_form.save(commit=False)
            add_sketch.save()

            return sketchawish(request)

        else:
            print sketch_form.errors

    else:
        sketch_form = SketchForm()

    return render(request, 'saw/add_sketch.html', {'sketch_form': sketch_form})

这是forms.py

class GetWishForm(forms.ModelForm):
    wish = forms.ModelChoiceField(queryset= Wish.objects.filter(pk__in = Wish.objects.filter(locked=False)[:3].values_list('pk')), initial=0)

    class Meta:
        model = Sketch
        fields = ('wish',)

class SketchForm(forms.ModelForm):
    wish = forms.ModelChoiceField(queryset= Sketch.objects.all(), initial=0)
    image_temp = forms.CharField(help_text='Imagine this is an upload button for image, write anything')

    class Meta:
        model = Sketch
        fields = ('wish', 'image_temp')

更新

我根据@ almalki的建议编辑了代码

forms.py

class SketchForm(forms.ModelForm):
    wish = forms.ModelChoiceField(queryset= Sketch.objects.all(), initial=0)
    image_temp = forms.CharField(help_text='Imagine this is an upload button for image, write anything')

    def __init__(self, *args, **kwargs):
        super(SketchForm, self).__init__(*args, **kwargs)
        self.fields['wish'].queryset = kwargs.pop('wish_qs')

    class Meta:
        model = Sketch
        fields = ('wish', 'image_temp')

views.py

@login_required
def add_sketch(request):
    if request.method == "POST":
        sketch_form = SketchForm(request.POST)

        if sketch_form.is_valid():
            add_sketch = sketch_form.save(commit=False)
            add_sketch.save()

            return sketchawish(request)

        else:
            print sketch_form.errors

    else:
        sketch_form = SketchForm(wish_qs=Wish.objects.filter(wisher=request.user))


    return render(request, 'saw/add_sketch.html', {'sketch_form': sketch_form})

我仍然收到错误 init ()得到了一个意外的关键字参数' wish_qs'

更新2:

forms.py与上面相同,这是我认为views.py应该是

@login_required
def add_sketch(request):
    if request.method == "POST":
        sketch_form = SketchForm(request.POST, wish_qs=Sketch.objects.filter(sketcher=request.user))

        if sketch_form.is_valid():
            add_sketch = sketch_form.save(commit=False)
            add_sketch.save()

            return sketchawish(request)

        else:
            print sketch_form.errors

    else:
        sketch_form = SketchForm(wish_qs=Sketch.objects.filter(sketcher=request.user))


    return render(request, 'saw/add_sketch.html', {'sketch_form': sketch_form})

当我选择一个愿望,然后点击提交时,错误是:annot assign"":" Sketch.wish"必须是"希望"实例。 我知道这是因为模型期待一个Wish实例,但我们正在给一个Sketch实例,但我不知道如何实现我需要的东西。我认为必须在models.py中做出一些改变,可逆地连接Wish和Sketch。

1 个答案:

答案 0 :(得分:1)

您需要覆盖表单初始化中的字段查询集:

class SketchForm(forms.ModelForm):
    wish = forms.ModelChoiceField(queryset= Sketch.objects.all(), initial=0)
    image_temp = forms.CharField(help_text='Imagine this is an upload button for image, write anything')

    def __init__(self, *args, **kwargs):
        wish_qs = kwargs.pop('wish_qs')
        super(SketchForm, self).__init__(*args, **kwargs)
        self.fields['wish'].queryset = wish_qs

    class Meta:
        model = Sketch
        fields = ('wish', 'image_temp')

在您的视图中,您需要传递基于当前登录用户筛选的查询集:

sketch_form = SketchForm(request.POST, wish_qs=Wish.objects.filter(wisher=request.user))

sketch_form = SketchForm(wish_qs=Wish.objects.filter(wisher=request.user))