Questions About ModelForm

26 views
Skip to first unread message

Mark Phillips

unread,
Aug 20, 2017, 9:21:25 PM8/20/17
to django users
I am trying to create a form for a django-viewflow workflow. I have 2 models, Document and DocumentOCR as shown:

class Document(models.Model):
    document_id = models.AutoField(primary_key=True)
    document_category = models.IntegerField(category=DOCUMENT_CATEGORY)
    document_state = models.IntegerField(state=DOCUMENT_STATE, default=PRIVATE)
    documentType_id = models.ForeignKey(DocumentType, on_delete=models.CASCADE,)
    title = models.CharField('title', max_length=200)
    description = models.TextField('description')
    created = models.DateTimeField(editable=False)
    updated = models.DateTimeField(editable=False)
    storage_file_name = models.FileField('File name', upload_to=unique_file_path)
    original_file_name = models.CharField(editable=False, max_length=200)
    computed_sha256 = models.CharField(editable=False, max_length=64)
    
class DocumentOCR(models.Model):
    document_id = models.ForeignKey(Document, on_delete=models.CASCADE,)
    text_file_name = models.FileField('File name', upload_to=unique_file_path)
    text = models.TextField()

Documents are uploaded and saved to the local drive through the admin portal. That works. The workflow is used to review those documents that have text in them (DOCUMENT_CATEGORY), and for a user to decide if they can be OCRed and the text saved in the text field of the DocumentOCR model, or if they need to be transcribed by the user into the text field. Some of the documents are hand written notes that have been scanned into a pdf.

The first stage of the workflow is supposed to show a table with one document per row. The first cell has a radio button, and each succeeding cell has information from the fields in the Document and DocumentOCR tables. The user selects the document he/she wants to work on by clicking on the appropriate radio button.

Is there something in the ModelForm class that will create this table, or do I have to create the HTML for this table. From my reading, I gather I need to put the database query for the data set for the table in the view and pass that to the form. Is that the appropriate way to do something like this?

Thanks,

Mark

James Schneider

unread,
Aug 22, 2017, 1:01:28 PM8/22/17
to django...@googlegroups.com

Documents are uploaded and saved to the local drive through the admin portal. That works. The workflow is used to review those documents that have text in them (DOCUMENT_CATEGORY), and for a user to decide if they can be OCRed and the text saved in the text field of the DocumentOCR model, or if they need to be transcribed by the user into the text field. Some of the documents are hand written notes that have been scanned into a pdf.

The first stage of the workflow is supposed to show a table with one document per row. The first cell has a radio button, and each succeeding cell has information from the fields in the Document and DocumentOCR tables. The user selects the document he/she wants to work on by clicking on the appropriate radio button.

Is there something in the ModelForm class that will create this table, or do I have to create the HTML for this table. From my reading, I gather I need to put the database query for the data set for the table in the view and pass that to the form. Is that the appropriate way to do something like this?


There's a couple of ways to go about this. 

What you are probably thinking of is a custom ModelForm using a ModelChoice form field with a different widget such as RadioSelect. You may need to override the 'option_template_name' with a custom template to get the table layout you desire.


Another option is to not use a form at all, but rather a standard ListView (or similar view). Using a form requires having a series of radio buttons and forcing the user to select one, then click Submit. Too many clicks, especially if your users will do this a lot (you'd be surprised at the amount of time saved for highly repetitive tasks like that). Using a ListView, the docs could still be rendered in a table with all of the information you mentioned, but they would simply click on the title of any of the documents to go to an editing form (as an example, you can also have a column with a link to the editing forms as well). Less clicking, and less mucking with forms and validation. Everyone wins. Of course, you may have other items in the form you are generating, so that may not be an option. 

-James
Reply all
Reply to author
Forward
0 new messages