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

Matthew Dumas

I'm having a bit of trouble with customizing the Django admin pages to be able to use custom forms or non-model fields.

The site I'm working on will be used as a data storage utility for reporting on some manufacturing processes. The database will have three main tables, two of which will have a ForeignKey in a fourth table. Each row in the fourth table is essentially a single line from a CSV file that needs to be uploaded. The model for the fourth table looks like this:

bandwidthrawid = models.IntegerField(...) 
testdate = models.DateTimeField(...)
frequency = models.FloatField(...)
power = models.FloatField(...)
uncalibratedpower = models.FloatField(...)

I'm letting Django automatically generate the id field. The test date will be static for every entry from the same test. The Frequency, Power, and UncalibratedPower fields will all contain a single value from the same line of the CSV file. The BandwidthRawId field will contain integers in sequential order, many times over again indicating which specific test this was. So, for example, let's assume a user tests the same module twice, over a frequency range of 1 to 60 Hz in steps of 1 Hz, a CSV file will be generated that looks similar to this:

1,0,0
2,0,0
3,0,0
4,0,0
5,0,0
...
60,0,0

The second test will also generate a similar file. When these files are uploaded into the system, the date of the test will be recorded, and it will be assigned a BandwidthRawId which is an incremented value of the current highest value in that column. So, the table entry for the two files will look like this:

Id, Date, BWID, F, P, UP
1,  Date1, 1,   1, 0, 0
2,  Date1, 1,   2, 0, 0
...
60,  Date1, 1,   60, 0, 0
61,  Date2, 2,   1, 0, 0
62,  Date2, 2,   2, 0, 0
...
120,  Date2, 2,   60, 0, 0

This is where things start to get tricky. Sometimes the files are not all in the same order, so the CSV Importer won't work. Sometimes the files don't contain all of the right information. The files NEVER have a date in them, and NEVER have any header lines. In order to combat all of these things, I created a widget/field that creates a CSV Preview and allows the user to select which actual field each column belongs to. I called this CSVPreviewField.

In order to test this field/widget in the "field" I overrode the BandwidthRawID field in the model and was using that to handle all of the processing. Originally I thought it would be easy to override that field, do the processing, and then fill in that field with the correct integer, then pass that off for processing. Unfortunately, that doesn't work. I get the error that the BandwidthRawID field is of invalid type (in the database it's an integer, in the model it's CSVPreviewField/FileInput).

Next, I tried to add a CSVPreviewField (called it BandwidthRawFile) to a ModelForm that uses BandwidthRaw as a model and overrode the form field of the ModelAdmin class I wrote to handle the bandwidth_raw table. Unfortunately, no matter what I do, I can't display this field, the error I get is "BandwidthRawFile is not found in form fields" or something similar. I've also gotten an error which indicates that there's no column in the database that corresponds with "BandwidthRawFile."

After that, I learned about inlines, and attempted the same thing with an inline form, which also fails for similar reasons. Either it fails because I don't specify a model (was hoping I could do a custom form), the model doesn't contain the BandwidthRawFile field, or the database doesn't contain the field.

At this point, I've been working on this for two days and have run out of ideas completely. Basically, the inline form would be the best solution. If I could some-how put my widget inline in the other two admin pages that require the BandwidthRawId and then return that value as a result of the widget processing, that would be the ideal situation. Right now I'd be happy just to have the little green + next to the foreign key launch a custom form that does all the processing and returns the BandwidthRawId.

Matthew Dumas

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

Custom form fields in Django

From Dev

django model form fields = () behaviour

From Dev

django model form fields = () behaviour

From Dev

Django UpdatView - custom form fields

From Dev

Django: How to custom save a model/form based on a non-persistent field of a form?

From Dev

Django: How to custom save a model/form based on a non-persistent field of a form?

From Dev

Django unique model fields validation in form

From Dev

Django model form not saving all fields to db

From Dev

json django model with custom fields representation

From Dev

Automatically connect signals to custom Django model fields

From Dev

Django not rendering field in non model form

From Dev

Django Form, with Class Meta model based in class model, and more fields

From Dev

Django login form not working for custom user model

From Dev

Django: Custom User Model fields not appearing in Django admin

From Dev

Magento Admin Edit Form Fields - Custom Model Field(s)

From Dev

Extend Django user model with custom fields, receivers and backend

From Dev

Django REST serializer and extra attributes from custom model fields

From Dev

Custom User model fields (AbstractUser) not showing in django admin

From Dev

fields.E300 error with custom user model in Django

From Dev

Custom fields in form_for

From Dev

Custom form field for Foreign key model field in Django

From Dev

Dynamic model fields in Django

From Dev

Django form fields will not display

From Dev

Customize Django Form fields

From Dev

Django form fields will not display

From Dev

Custom Errors in Fields, on Django

From Dev

Extending django.contrib.auth.models.User model with custom fields in Django

From Dev

Django model extending Default User model (one-to-one field), how to create reg form for all combined model fields

Related Related

  1. 1

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

  2. 2

    Custom form fields in Django

  3. 3

    django model form fields = () behaviour

  4. 4

    django model form fields = () behaviour

  5. 5

    Django UpdatView - custom form fields

  6. 6

    Django: How to custom save a model/form based on a non-persistent field of a form?

  7. 7

    Django: How to custom save a model/form based on a non-persistent field of a form?

  8. 8

    Django unique model fields validation in form

  9. 9

    Django model form not saving all fields to db

  10. 10

    json django model with custom fields representation

  11. 11

    Automatically connect signals to custom Django model fields

  12. 12

    Django not rendering field in non model form

  13. 13

    Django Form, with Class Meta model based in class model, and more fields

  14. 14

    Django login form not working for custom user model

  15. 15

    Django: Custom User Model fields not appearing in Django admin

  16. 16

    Magento Admin Edit Form Fields - Custom Model Field(s)

  17. 17

    Extend Django user model with custom fields, receivers and backend

  18. 18

    Django REST serializer and extra attributes from custom model fields

  19. 19

    Custom User model fields (AbstractUser) not showing in django admin

  20. 20

    fields.E300 error with custom user model in Django

  21. 21

    Custom fields in form_for

  22. 22

    Custom form field for Foreign key model field in Django

  23. 23

    Dynamic model fields in Django

  24. 24

    Django form fields will not display

  25. 25

    Customize Django Form fields

  26. 26

    Django form fields will not display

  27. 27

    Custom Errors in Fields, on Django

  28. 28

    Extending django.contrib.auth.models.User model with custom fields in Django

  29. 29

    Django model extending Default User model (one-to-one field), how to create reg form for all combined model fields

HotTag

Archive