Creating ability on a web page for a user to upload a note against a watchlist item, see code below. Error – (1054, “Unknown column ‘created_by_id’ in ‘field list'”).
I am using ModelForms, all my models are managed=False which is why I am specifying the PK fields in the models. I have run migrations multiple times and it doesnt detect any changes.
Any help to understand why I am getting this error would be very appreciated.
models.py – related portion
class UserNotes(models.Model):
note_id = models.AutoField(primary_key=True)
watchlist = models.ForeignKey('Watchlist', on_delete=models.DO_NOTHING)
created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.DO_NOTHING)
title = models.CharField(max_length=100, blank=True, null=True)
note = models.TextField(max_length=1000, blank=True, null=True)
class Meta:
managed = False
db_table = 'user_uploads'
def __str__(self):
return f"Note title: {self.title}"
views.py – related portion
if request.method == "POST":
add_note_form = UserNote(request.POST)
if add_note_form.is_valid():
add_note = add_note_form.save(commit=False)
print(f"add_note: {add_note}")
add_note.created_by = request.user
add_note.save()
print(f"note saved: {add_note_form}")
return HttpResponseRedirect(request.path_info)
forms.py – related portion
class UserNote(forms.ModelForm):
class Meta:
model = UserNotes
fields = ['title', 'note']
widgets = {
"note": forms.Textarea(attrs={'class': 'user_note'})
}
labels = {
'title': 'Title',
'note': 'Note'
}
I thought the form only needed to have fields that were to be presented to the html template included in the Meta? Im trying to add the user after .save(commit=False) so that it is added to the other form fields that are being saved through the model.