[Django]-Django custom form with non-model fields in Admin/AdminInline

3👍

Since it looks like nobody has an answer to this, I’m going to go ahead and “answer” it.

The answer I’ve found is that you cannot have non-model fields in a model-form. They are simply ignored by the framework. Adding your own forms inline also doesn’t seem to work well, the same errors end up occurring.

The way I fixed this issue was:

  1. Create A CSV Preview Widget
  2. Implement the Widget into a CSV Field
  3. Write the field/widget logic
  4. Create my own form — NOT a model-form
  5. Create Form handlers
  6. Create view to host the form
  7. Override add_view in models to return the custom form
  8. Override the get_form function in models to add the csv widget to the form

I don’t really -Like- this answer, but I don’t have any better solution.

0👍

If you are not too picky on the implementation you might want to look at something like Postgres Foreign data wrappers (file_fdw is a csv interface). Since the files are very uniform, you will get a nice ORM interface and save a ton of headaches on the import side.

Leave a comment