简体   繁体   中英

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

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. Right now I am just using a charField for the uploaded sketch. Here is the code

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

And here is the 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

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

I still get the error init () got an unexpected keyword argument 'wish_qs'

UPDATE 2:

forms.py remains same as above, here is what I think the views.py should be

@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. 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. I think some change has to be made in the models.py, connecting Wish and Sketch reversibly.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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