python - Django custom form with non-model fields in Admin/AdminInline -
i'm having bit of problem customizing django admin pages able utilize custom forms or non-model fields.
the site i'm working on used info storage utility reporting on manufacturing processes. database have 3 main tables, 2 of have foreignkey in 4th table. each row in 4th table single line csv file needs uploaded. model 4th table looks this:
bandwidthrawid = models.integerfield(...) testdate = models.datetimefield(...) frequency = models.floatfield(...) powerfulness = models.floatfield(...) uncalibratedpower = models.floatfield(...)
i'm letting django automatically generate id field. test date static every entry same test. frequency, power, , uncalibratedpower fields contain single value same line of csv file. bandwidthrawid field contain integers in sequential order, many times on 1 time again indicating specific test was. so, example, let's assume user tests same module twice, on frequency range of 1 60 hz in steps of 1 hz, csv file generated looks similar this:
1,0,0 2,0,0 3,0,0 4,0,0 5,0,0 ... 60,0,0
the sec test generate similar file. when these files uploaded system, date of test recorded, , assigned bandwidthrawid incremented value of current highest value in column. so, table entry 2 files this:
id, date, bwid, f, p, 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 things start tricky. files not in same order, csv importer won't work. files don't contain of right information. files never have date in them, , never have header lines. in order combat of these things, created widget/field creates csv preview , allows user select actual field each column belongs to. called csvpreviewfield.
in order test field/widget in "field" overrode bandwidthrawid field in model , using handle of processing. thought easy override field, processing, , fill in field right integer, pass off processing. unfortunately, doesn't work. error bandwidthrawid field of invalid type (in database it's integer, in model it's csvpreviewfield/fileinput).
next, tried add together csvpreviewfield (called bandwidthrawfile) modelform uses bandwidthraw model , overrode form field of modeladmin class wrote handle bandwidth_raw table. unfortunately, no matter do, can't display field, error "bandwidthrawfile not found in form fields" or similar. i've gotten error indicates there's no column in database corresponds "bandwidthrawfile."
after that, learned inlines, , attempted same thing inline form, fails similar reasons. either fails because don't specify model (was hoping custom form), model doesn't contain bandwidthrawfile field, or database doesn't contain field.
at point, i've been working on 2 days , have run out of ideas completely. basically, inline form best solution. if some-how set widget inline in other 2 admin pages require bandwidthrawid , homecoming value result of widget processing, ideal situation. right i'd happy have little greenish + next foreign key launch custom form processing , returns bandwidthrawid.
since looks nobody has reply this, i'm going go ahead , "answer" it.
the reply i've found cannot have non-model fields in model-form. ignored framework. adding own forms inline doesn't seem work well, same errors end occurring.
the way fixed issue was:
create csv preview widget implement widget csv field write field/widget logic create own form -- not model-form create form handlers create view host form override add_view in models homecoming custom form override get_form function in models add together csv widget formi don't -like- answer, don't have improve solution.
python django django-models django-forms django-admin
No comments:
Post a Comment