Getting FieldError in modelformset factory in django view.py

61 views Asked by At

I am getting FieldError as :

Unknown field(s) (notedate) specified for AssistantNotes

When i call the page. It throws this error. I am using Django 1.9.5 and python 2.7. I have notedate field in the AssistantNotes table in my db. If i delete "notedate" from modelformset_factory row in my view, it works. I couldnt solve why it is not showing notedate although it is in DB and in model. And generating error. The field is already in the model.

My view is :

def edit_assistant_notes(request):
    isassistantsuperadmin = getUserPermissions(request) #Yes if 1, no if 0
    list = getUserType(request)
    userisassistant = list[2]
    if userisassistant == "YES" or isassistantsuperadmin ==1: 
        list = getUserType(request)
        type = list[0]
        usertype = list[1] #"Nöbetçi Muavin":1 , "Yetkili":2
        if request.method == 'GET':
            if AssistantNotes.objects.filter(notedate=nicosia_date(datetime.today()).date()).count() == 0:
                AssistantNotesFormsetFactory = modelformset_factory(AssistantNotes, fields=('time', 'notedate', 'categories', 'type', 'dailynote',))
            else:
                AssistantNotesFormsetFactory = modelformset_factory(AssistantNotes, fields=('time', 'notedate', 'categories', 'type', 'dailynote',), can_delete=True)
            if usertype == 1:
                formset = AssistantNotesFormsetFactory(queryset=AssistantNotes.objects.filter(notedate=nicosia_date(datetime.today()).date(), type=type))
            elif usertype == 2:
                formset = AssistantNotesFormsetFactory(queryset=AssistantNotes.objects.all().order_by("notedate", "time"))
            helper = TableInlineHelper()
            return render(request, 'edit-assistant-notes.html', {'formset': formset, 'helper': helper})

My model is :

    class AssistantNotes(BaseModel):
    categories = models.CharField(choices=CATEGORIES, default="GENERAL", max_length=100, verbose_name=_("CAT"))
    time = models.CharField(choices=TIME, default="-------------", max_length=20, verbose_name=_("Time"))
    dailynote = models.TextField(null=True, blank=True, verbose_name=_("Add Note"))
    writer = models.TextField(null=True, blank=True, verbose_name=_("Adder"))
    notedate = models.DateField(auto_now_add=True, db_index=True, verbose_name=_("Date"))
    type = models.CharField(choices=SCHOOLTYPE, default="---", max_length=100, verbose_name=_("SchoolType"))

    def __unicode__(self):
        return "%s / %s" % (self.dailynote, self.categories)

    class Meta:
        ordering = ['dailynote']
1

There are 1 answers

3
art On

How can i force this field to be editable ?

Instead of setting auto_now_add, you may override the save() method of the model, say

class AssistantNotes(BaseModel):
    ....
    notedate = models.DateField(db_index=True, verbose_name=_("Date"))
    def save(self, *args, **kwargs):
        if not self.id:
            self.notedate = timezone.now()
        return super(AssistantNotes, self).save(*args, **kwargs)