简体   繁体   English

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

[英]Reference objects using foreign keys in Django forms

I did a lot of search for an issue I am facing, but couldn't find a suitable solution. 我做了很多搜索来寻找自己面临的问题,但是找不到合适的解决方案。 I am a Django beginner 我是Django初学者

I am creating a project in which an User will be able to ask a wish, and other users will be assigned that wish, which they can then draw and submit. 我正在创建一个项目,在该项目中,用户将能够提出一个愿望,其他用户将被分配该愿望,然后他们可以绘制并提交。

I created views for asking and getting a wish, but facing issue while submitting the sketch for the wish. 我创建了用于询问和获得愿望的视图,但是在提交该草图时遇到了问题。 I do not know how to show only those wishes in the add_sketch form for the current user and then update the sketch model with this new sketch. 我不知道如何在add_sketch表单中仅向当前用户显示这些愿望,然后使用此新草图更新草图模型。 Right now I am just using a charField for the uploaded sketch. 现在,我只是将charField用于上传的草图。 Here is the code 这是代码

models.py 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 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})

And here is the forms.py 这是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')

UPDATE: 更新:

I edited the code according to @almalki's suggestion 我根据@almalki的建议编辑了代码

forms.py 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 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})

I still get the error init () got an unexpected keyword argument 'wish_qs' 我仍然收到错误init (),得到了意外的关键字参数'wish_qs'

UPDATE 2: 更新2:

forms.py remains same as above, here is what I think the views.py should be 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})

When I choose a wish, and click submit, the error is: annot assign "": "Sketch.wish" must be a "Wish" instance. 当我选择一个愿望,然后单击提交时,错误是:取消分配“”:“ Sketch.wish”必须是“ Wish”实例。 I know this is because the model is expecting a Wish instance, but we are giving a Sketch instance, but I don't know how to achieve what I need. 我知道这是因为模型期望使用Wish实例,但是我们提供了Sketch实例,但是我不知道如何实现所需的功能。 I think some change has to be made in the models.py, connecting Wish and Sketch reversibly. 我认为必须对model.py进行一些更改,以可逆方式连接Wish和Sketch。

You need to override the field query set in form initialization: 您需要覆盖表单初始化中的字段查询集:

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')

And in your view, you need to pass a queryset filtered based on current logged in user: 并且在您的视图中,您需要传递基于当前登录用户过滤的查询集:

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

and: 和:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM