0

I'm writing a simple Django powered notetaking app, and currently I'm implementing basic tagging system for my notes. The requirement is to have a Tag model with added_by field on it as a ForeignKey to User. First I have tried to modify django-taggit source, as well as extend its Tag model, however haven't had any success. So, I decided to implement my own tagging system from scratch. For this purpose I'm using a ManyToManyField:

class Note(models.Model):
    slug = models.SlugField()
    title = models.CharField(max_length=255)
    [...]
    tags = models.ManyToManyField(Tag)

This is my form:

class NoteForm(forms.ModelForm):
    class Meta:
        model = Note
        fields = [
            'title',
            'description',
            'notebook',
            'tags'
        ]
        widgets = {
            'tags': forms.TextInput()
        }

Everything renders nicely, but when I try to submit the create form, I get an error:

screenshot

So, inside that TextInput is a comma separated string. I assume that's not what Django expects, but I don't know how to convert that into something Django would accept.

I have tried to debug this, but it seems that my form_valid isn't getting executed at all. I have also tried to put some debug statements in clean_tags in my ModelForm, which is also not getting executed.

How can I overcome the issue?

My view (that currently doesn't handle any logic related to tags):

class NoteCreateView(LoginRequiredMixin, CreateView):
    model = Note
    form_class = NoteForm

    def form_valid(self, form):
        print(self.request.POST)
        form.instance.added_by = self.request.user
        return super().form_valid(form)

    def get_initial(self):
        return {'notebook': self.kwargs.get('pk')}

    def get_success_url(self):
        return reverse('notes:note-list', kwargs={'pk': self.kwargs['pk']})
adder
  • 3,512
  • 1
  • 16
  • 28
  • Why not handle tags with an ArrayField? https://docs.djangoproject.com/en/2.2/ref/contrib/postgres/fields/#arrayfield Actually, there is a sample with that use case in the official documentation. – Cartucho Nov 22 '19 at 18:30
  • If you override `form_invalid()` in your view you can print `form.errors` and `form.non_field_errors`. Alternatively you can include the errors in your template when the page is rendered again. This should give you a better idea of the error. – bdoubleu Nov 22 '19 at 18:31
  • @Cartucho I'm using SQLite. – adder Nov 22 '19 at 18:33
  • It's likely that your form is looking for an integer that matches an existing tag id. – bdoubleu Nov 22 '19 at 18:33
  • @bdoubleu I have included errors in my template. That string 'Enter a list of values.' is the error I'm getting. – adder Nov 22 '19 at 18:33
  • @bdoubleu nope, it's the same when I input `1`, which is an existing tag id. – adder Nov 22 '19 at 18:34
  • can you provide your `view` ? – Subham Nov 22 '19 at 18:38
  • @Subham Yeah, sure. I've updated the question. – adder Nov 22 '19 at 18:42
  • You can check this https://stackoverflow.com/questions/16465560/manytomanyfield-in-class-based-generic-views-and-save-override – Subham Nov 22 '19 at 18:50

0 Answers0