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:
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']})