JD_deepwater
JD_deepwater

Reputation: 25

How to construct value in django admin using custom forms?

How can I change checks field to custom fields that construct value from 3 ChoiceField's by joining them in one string?

Initialy in admin I have:

class QuestionAdmin(admin.ModelAdmin):   
    fieldsets = [
        (_('Question'),  {'fields': ['id','text'...etc]}),
        (_('Checks'),  {'fields': ['checks']}),
    ]

adminsite.register(Question, QuestionAdmin)

I understand that I need to do something in this direction within QuestionAdmin class (unusable code follows):

def get_form(request, obj=None, **kwargs):
    if not obj:
        #here I need to get my custom form with 3 choicefields and add them to fieldsets
        self.fieldsets.append((None, {fields: ['choice_part_1', 'choice_part_2', 'choice_part_3']}))
    return super(QuestionAdmin, self).get_form(request, obj=None, **kwargs)

Then I must ''.join selected values at some point and use resulting value on submit as checks field entry...

choices for choicefields are not in any model, so do I need to use simple form using forms.Form?

Can anyone please explain how to accomplish that?
(I need this approach, otherwise I end up with changing a lot of code)

Thanks.

Upvotes: 1

Views: 138

Answers (1)

ilvar
ilvar

Reputation: 5841

Take a look at MultiValueField, it allows to split one field in few widgets.

Upvotes: 1

Related Questions