Home > OS >  How to make the instance inside Django form take multiple values?
How to make the instance inside Django form take multiple values?

Time:02-17

I'm making a Django blog and I want to make the user edit his post. I have 2 forms to render, Post Form that includes (title, post image, content, category) and another separated form for the post tags that includes (tag name). To edit the tags I have to get all the tags related to the post and set them to the instance attribute which takes only one object (and I have multiple tags for one post). Here are my Models:

class PostTags(models.Model):
    tag_name = models.CharField(max_length=100)

    def __str__(self):
        return self.tag_name

class Post(models.Model):
    title = models.CharField(max_length=50)
    picture = models.ImageField(null=True,upload_to='images/')
    content = models.CharField(max_length=255)
    likes = models.ManyToManyField(User,blank=True,related_name='likes')
    dislikes = models.ManyToManyField(User,blank=True,related_name='dislikes')
    date_of_publish = models.DateTimeField(auto_now_add=True,null=True,blank=True)
    user = models.ForeignKey(User,on_delete=models.CASCADE)
    category = models.ForeignKey(Category,on_delete=models.CASCADE)
    tag = models.ManyToManyField(PostTags,blank=True)
      
    def __str__(self):
        return self.title

Here are my Forms:

class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ['title','picture','content','category']
        widgets = {
            'title': forms.TextInput(attrs={'class': 'form-control'}),
            'picture': forms.FileInput(attrs={'class': 'form-control'}),
            'content':forms.TextInput(attrs={'class': 'form-control'}),
            'category' : forms.Select(attrs={'class':'form-control'}),
        }

class TagsForm(forms.ModelForm):
    class Meta:
        model = PostTags
        fields = ['tag_name']
        widgets = {
            'tag_name': forms.TextInput(attrs={'class': 'form-control', 'data-role': 'tagsinput'})
        }

and here is my try to get all tags in the tags form in views.py

def editPost(request,post_id):
    post = Post.objects.get(id= post_id)
    post_form = PostForm(instance=post)
    # tagInstance = []
    for tag in post.tag.all():
        print(tag)
        newTag = PostTags.objects.get(tag_name=tag)
        tag_form = TagsForm(instance=newTag)
        # tagInstance.append(newTag)
 
    # print(tagInstance)
    
    
    # if request.method == 'POST':
    #     form = PostForm(request.POST,instance=post)
    #     if form.is_valid():
    #         form.save()
    #         return redirect('post')
    context = {"post_form":post_form,'tag_form':tag_form}
    return render (request,"dj_admin/editpost.html",context)

The above try resulted in only the last tag rendered to the tags form which is expected

CodePudding user response:

If I understood you correctly, you want to render all tag_forms related to the post inside your template.

You can achieve what you want using django model inline_formsets

forms.py:

from django.forms import inlineformset_factory
from .models import Post, PostTags

class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ['title','picture','content','category']
        widgets = {
            'title': forms.TextInput(attrs={'class': 'form-control'}),
            'picture': forms.FileInput(attrs={'class': 'form-control'}),
            'content':forms.TextInput(attrs={'class': 'form-control'}),
            'category' : forms.Select(attrs={'class':'form-control'}),
        }

class TagsForm(forms.ModelForm):
    class Meta:
        model = PostTags
        fields = ['tag_name']
        widgets = {
            'tag_name': forms.TextInput(attrs={'class': 'form-control', 'data-role': 'tagsinput'})
        }

TagFormSet = inlineformset_factory(PostTags, Post, fields=('tag_name',))

views.py:

from .forms import PostForm, TagsForm, TagFormSet

def editPost(request,post_id):
    post = Post.objects.get(id= post_id)
    post_form = PostForm(instance=post)
    tag_formset = TagFormSet(instance=post)     
    
    if request.method == 'POST':
        form = PostForm(request.POST, instance=post)
        tag_formset = TagFormSet(request.POST, instance=post)
        if form.is_valid() and tag_formset.is_valid():
            form.save()
            tag_formset.save()
            return redirect('post')
    context = {'post_form': post_form, 'tag_formset': tag_formset}
    return render (request, 'dj_admin/editpost.html', context)

and then inside your template file you can simply add {{ tag_formset }} like this:

<form method="post">
    {{ formset }}
</form>

CodePudding user response:

There is probably a cleaner solution but this should work:

def editPost(request,post_id):
    post = Post.objects.get(id= post_id)
    post_form = PostForm(instance=post)
    context = {}
    context['post_form'] = post_form
    count = 0
    for tag in post.tag.all():
        print(tag)
        newTag = PostTags.objects.get(tag_name=tag)
        tag_form = TagsForm(instance=newTag)
        count  = 1
        context['tag_form' str(count)] = TagsForm(instance=newTag)
    context['tag_form_counter'] = count
    return render (request,"dj_admin/editpost.html",context)

So, in your template you must to check the tag_form_counter variable to know how many tag_forms you have. And make a forloop to show them.

  • Related