In a forms of an inlineformset how can I know which are my default form fields and which are the ones added by Django?Django: multiple models in one template using formsMake inlineformset in django requiredDjango inline formset - allow blank dynamically generated formsDynamically Delete form from model formset djangoDjango ModelChoiceField and performance with >100 formset formsHow django inlineformset works?How to use Django inlineformset_factory with custom formsetDjango Inlineformset with editable Foreign Key RelationDjango - Modify Inlineformset Delete buttonHow to create an input page for a parent model whose child has multiple foreign keys in Django?
Would Brexit have gone ahead by now if Gina Miller had not forced the Government to involve Parliament?
Should breaking down something like a door be adjudicated as an attempt to beat its AC and HP, or as an ability check against a set DC?
Popcorn is the only acceptable snack to consume while watching a movie
Is it possible to play as a necromancer skeleton?
Does Nitrogen inside commercial airliner wheels prevent blowouts on touchdown?
Adding spaces to string based on list
What does this symbol on the box of power supply mean?
Installed Electric Tankless Water Heater - Internet loss when active
Why do Ryanair allow me to book connecting itineraries through a third party, but not through their own website?
How to illustrate the Mean Value theorem?
Are these reasonable traits for someone with autism?
Make 24 using exactly three 3s
Why do most published works in medical imaging try to reduce false positives?
keyval - function for keyB should act dependent on value of keyA - how to do this?
Why aren't space telescopes put in GEO?
Defining the standard model of PA so that a space alien could understand
What will be the real voltage along the line with a voltage source and a capacitor?
What was the idiom for something that we take without a doubt?
How can I tell if I'm being too picky as a referee?
What is the environment variable XDG_VTNR?
How to respond to an upset student?
Is CD audio quality good enough?
Why are C64 games inconsistent with which joystick port they use?
Can I install both XCode & Android Studio on MacBook Air with only 8 GB of Ram
In a forms of an inlineformset how can I know which are my default form fields and which are the ones added by Django?
Django: multiple models in one template using formsMake inlineformset in django requiredDjango inline formset - allow blank dynamically generated formsDynamically Delete form from model formset djangoDjango ModelChoiceField and performance with >100 formset formsHow django inlineformset works?How to use Django inlineformset_factory with custom formsetDjango Inlineformset with editable Foreign Key RelationDjango - Modify Inlineformset Delete buttonHow to create an input page for a parent model whose child has multiple foreign keys in Django?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a specific formset (inlineformset), and I want to make some customization regarding the default design.
I loop thru it this way:
% for form in formset %
<div class="ct-formset">
% if form.errors %<div> form.errors </div>% endif %
% for field in form % field % endfor %
% endfor %
For each form Django add two other fields the Foreign key field and the Delete Field.
Because I want to use the same code for multiple formsets, In the loop I don't request the field
by name
I need to know in the:
% for field in form % field % endfor %
How can I know which are my default form fields and which are the ones added by Django ?
django django-forms
add a comment |
I have a specific formset (inlineformset), and I want to make some customization regarding the default design.
I loop thru it this way:
% for form in formset %
<div class="ct-formset">
% if form.errors %<div> form.errors </div>% endif %
% for field in form % field % endfor %
% endfor %
For each form Django add two other fields the Foreign key field and the Delete Field.
Because I want to use the same code for multiple formsets, In the loop I don't request the field
by name
I need to know in the:
% for field in form % field % endfor %
How can I know which are my default form fields and which are the ones added by Django ?
django django-forms
add a comment |
I have a specific formset (inlineformset), and I want to make some customization regarding the default design.
I loop thru it this way:
% for form in formset %
<div class="ct-formset">
% if form.errors %<div> form.errors </div>% endif %
% for field in form % field % endfor %
% endfor %
For each form Django add two other fields the Foreign key field and the Delete Field.
Because I want to use the same code for multiple formsets, In the loop I don't request the field
by name
I need to know in the:
% for field in form % field % endfor %
How can I know which are my default form fields and which are the ones added by Django ?
django django-forms
I have a specific formset (inlineformset), and I want to make some customization regarding the default design.
I loop thru it this way:
% for form in formset %
<div class="ct-formset">
% if form.errors %<div> form.errors </div>% endif %
% for field in form % field % endfor %
% endfor %
For each form Django add two other fields the Foreign key field and the Delete Field.
Because I want to use the same code for multiple formsets, In the loop I don't request the field
by name
I need to know in the:
% for field in form % field % endfor %
How can I know which are my default form fields and which are the ones added by Django ?
django django-forms
django django-forms
asked Mar 17 at 12:59
user3541631user3541631
1,22631837
1,22631837
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
While working with the form instance, you can't tell for sure what fields are added originally in the class or after instantiation.
Though, here you have some approaches on how to prevent those fields you don't want to be rendered in the resulting formset.
If you have access to the form's class ...
... at the moment you need to "filter" which fields where the original fields of such form you could:
>>> class MyForm(forms.Form):
>>> title = forms.CharField()
>>> class_dict = vars(MyForm)
>>> class_dict['declared_fields']
OrderedDict([('title', <django.forms.fields.CharField at 0x7f496ce067d0>)])
About *-DELETE and *-ORDER fields
When dealing with formsets, you must to have into account that not only Django can add extra fields to your forms, you also can do that.
First, we have to understand why these fields are there.
The case for the *-DELETE
and *-ORDER
fields are added just if you enable them using:
formset = formset_factory(..., can_delete=True, can_order=True)
Django uses BaseFormSet.add_fields in order to add that extra fields it needs to enable ordering or deletion, you could use it to add your own additional fields too.
What can you do?
You can just pass False
there.
An interesting experiment ...
I perform an experiment in order to illustrate the effect overwritting this can have:
from django import forms
class MyForm(forms.Form):
title = forms.CharField()
class MyBaseFormSet(forms.BaseFormSet):
def add_fields(self, form, i):
# Avoiding FormSets using this to add additional fields
return form
# Create a form set with deletion and ordering enabled.
# pay attention to the argument: formset=MyBaseFormSet
MyFormSet = forms.formset_factory(MyForm, formset=MyBaseFormSet, can_delete=True, can_order=True)
fs = MyFormSet()
for form in fs:
print(form.as_table())
The result, no DELETE or ORDER fields added to the resulting forms.
>>> <tr><th><label for="id_form-0-title">Title:</label></th><td><input type="text" name="form-0-title" id="id_form-0-title" /></td></tr>
What about ForeignKey
That's something you can solve in the forms. If you don't want ForeignKey
to be displayed, you could use forms.HiddenInput
widget for such fields in your forms.
Also if you're working with ModelForms you can select the fields to use for generating the form.
Conclusion
With this info, I hope you to be able to plan how to implement your requirement of having a generic template for those formsets.
Starting points:
- Pass
can_delete
or/andstart_order
as False toformset_factory
. - Set
forms.HiddenInput
widget for ForeignKey fields in your forms. - Find a way to get the form class and use vars in order to find out the
original fields of the form. - If you're using ModelForms, use the
fields
orexclude
meta configuration in order to state which fields are used to build the form.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55207317%2fin-a-forms-of-an-inlineformset-how-can-i-know-which-are-my-default-form-fields-a%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
While working with the form instance, you can't tell for sure what fields are added originally in the class or after instantiation.
Though, here you have some approaches on how to prevent those fields you don't want to be rendered in the resulting formset.
If you have access to the form's class ...
... at the moment you need to "filter" which fields where the original fields of such form you could:
>>> class MyForm(forms.Form):
>>> title = forms.CharField()
>>> class_dict = vars(MyForm)
>>> class_dict['declared_fields']
OrderedDict([('title', <django.forms.fields.CharField at 0x7f496ce067d0>)])
About *-DELETE and *-ORDER fields
When dealing with formsets, you must to have into account that not only Django can add extra fields to your forms, you also can do that.
First, we have to understand why these fields are there.
The case for the *-DELETE
and *-ORDER
fields are added just if you enable them using:
formset = formset_factory(..., can_delete=True, can_order=True)
Django uses BaseFormSet.add_fields in order to add that extra fields it needs to enable ordering or deletion, you could use it to add your own additional fields too.
What can you do?
You can just pass False
there.
An interesting experiment ...
I perform an experiment in order to illustrate the effect overwritting this can have:
from django import forms
class MyForm(forms.Form):
title = forms.CharField()
class MyBaseFormSet(forms.BaseFormSet):
def add_fields(self, form, i):
# Avoiding FormSets using this to add additional fields
return form
# Create a form set with deletion and ordering enabled.
# pay attention to the argument: formset=MyBaseFormSet
MyFormSet = forms.formset_factory(MyForm, formset=MyBaseFormSet, can_delete=True, can_order=True)
fs = MyFormSet()
for form in fs:
print(form.as_table())
The result, no DELETE or ORDER fields added to the resulting forms.
>>> <tr><th><label for="id_form-0-title">Title:</label></th><td><input type="text" name="form-0-title" id="id_form-0-title" /></td></tr>
What about ForeignKey
That's something you can solve in the forms. If you don't want ForeignKey
to be displayed, you could use forms.HiddenInput
widget for such fields in your forms.
Also if you're working with ModelForms you can select the fields to use for generating the form.
Conclusion
With this info, I hope you to be able to plan how to implement your requirement of having a generic template for those formsets.
Starting points:
- Pass
can_delete
or/andstart_order
as False toformset_factory
. - Set
forms.HiddenInput
widget for ForeignKey fields in your forms. - Find a way to get the form class and use vars in order to find out the
original fields of the form. - If you're using ModelForms, use the
fields
orexclude
meta configuration in order to state which fields are used to build the form.
add a comment |
While working with the form instance, you can't tell for sure what fields are added originally in the class or after instantiation.
Though, here you have some approaches on how to prevent those fields you don't want to be rendered in the resulting formset.
If you have access to the form's class ...
... at the moment you need to "filter" which fields where the original fields of such form you could:
>>> class MyForm(forms.Form):
>>> title = forms.CharField()
>>> class_dict = vars(MyForm)
>>> class_dict['declared_fields']
OrderedDict([('title', <django.forms.fields.CharField at 0x7f496ce067d0>)])
About *-DELETE and *-ORDER fields
When dealing with formsets, you must to have into account that not only Django can add extra fields to your forms, you also can do that.
First, we have to understand why these fields are there.
The case for the *-DELETE
and *-ORDER
fields are added just if you enable them using:
formset = formset_factory(..., can_delete=True, can_order=True)
Django uses BaseFormSet.add_fields in order to add that extra fields it needs to enable ordering or deletion, you could use it to add your own additional fields too.
What can you do?
You can just pass False
there.
An interesting experiment ...
I perform an experiment in order to illustrate the effect overwritting this can have:
from django import forms
class MyForm(forms.Form):
title = forms.CharField()
class MyBaseFormSet(forms.BaseFormSet):
def add_fields(self, form, i):
# Avoiding FormSets using this to add additional fields
return form
# Create a form set with deletion and ordering enabled.
# pay attention to the argument: formset=MyBaseFormSet
MyFormSet = forms.formset_factory(MyForm, formset=MyBaseFormSet, can_delete=True, can_order=True)
fs = MyFormSet()
for form in fs:
print(form.as_table())
The result, no DELETE or ORDER fields added to the resulting forms.
>>> <tr><th><label for="id_form-0-title">Title:</label></th><td><input type="text" name="form-0-title" id="id_form-0-title" /></td></tr>
What about ForeignKey
That's something you can solve in the forms. If you don't want ForeignKey
to be displayed, you could use forms.HiddenInput
widget for such fields in your forms.
Also if you're working with ModelForms you can select the fields to use for generating the form.
Conclusion
With this info, I hope you to be able to plan how to implement your requirement of having a generic template for those formsets.
Starting points:
- Pass
can_delete
or/andstart_order
as False toformset_factory
. - Set
forms.HiddenInput
widget for ForeignKey fields in your forms. - Find a way to get the form class and use vars in order to find out the
original fields of the form. - If you're using ModelForms, use the
fields
orexclude
meta configuration in order to state which fields are used to build the form.
add a comment |
While working with the form instance, you can't tell for sure what fields are added originally in the class or after instantiation.
Though, here you have some approaches on how to prevent those fields you don't want to be rendered in the resulting formset.
If you have access to the form's class ...
... at the moment you need to "filter" which fields where the original fields of such form you could:
>>> class MyForm(forms.Form):
>>> title = forms.CharField()
>>> class_dict = vars(MyForm)
>>> class_dict['declared_fields']
OrderedDict([('title', <django.forms.fields.CharField at 0x7f496ce067d0>)])
About *-DELETE and *-ORDER fields
When dealing with formsets, you must to have into account that not only Django can add extra fields to your forms, you also can do that.
First, we have to understand why these fields are there.
The case for the *-DELETE
and *-ORDER
fields are added just if you enable them using:
formset = formset_factory(..., can_delete=True, can_order=True)
Django uses BaseFormSet.add_fields in order to add that extra fields it needs to enable ordering or deletion, you could use it to add your own additional fields too.
What can you do?
You can just pass False
there.
An interesting experiment ...
I perform an experiment in order to illustrate the effect overwritting this can have:
from django import forms
class MyForm(forms.Form):
title = forms.CharField()
class MyBaseFormSet(forms.BaseFormSet):
def add_fields(self, form, i):
# Avoiding FormSets using this to add additional fields
return form
# Create a form set with deletion and ordering enabled.
# pay attention to the argument: formset=MyBaseFormSet
MyFormSet = forms.formset_factory(MyForm, formset=MyBaseFormSet, can_delete=True, can_order=True)
fs = MyFormSet()
for form in fs:
print(form.as_table())
The result, no DELETE or ORDER fields added to the resulting forms.
>>> <tr><th><label for="id_form-0-title">Title:</label></th><td><input type="text" name="form-0-title" id="id_form-0-title" /></td></tr>
What about ForeignKey
That's something you can solve in the forms. If you don't want ForeignKey
to be displayed, you could use forms.HiddenInput
widget for such fields in your forms.
Also if you're working with ModelForms you can select the fields to use for generating the form.
Conclusion
With this info, I hope you to be able to plan how to implement your requirement of having a generic template for those formsets.
Starting points:
- Pass
can_delete
or/andstart_order
as False toformset_factory
. - Set
forms.HiddenInput
widget for ForeignKey fields in your forms. - Find a way to get the form class and use vars in order to find out the
original fields of the form. - If you're using ModelForms, use the
fields
orexclude
meta configuration in order to state which fields are used to build the form.
While working with the form instance, you can't tell for sure what fields are added originally in the class or after instantiation.
Though, here you have some approaches on how to prevent those fields you don't want to be rendered in the resulting formset.
If you have access to the form's class ...
... at the moment you need to "filter" which fields where the original fields of such form you could:
>>> class MyForm(forms.Form):
>>> title = forms.CharField()
>>> class_dict = vars(MyForm)
>>> class_dict['declared_fields']
OrderedDict([('title', <django.forms.fields.CharField at 0x7f496ce067d0>)])
About *-DELETE and *-ORDER fields
When dealing with formsets, you must to have into account that not only Django can add extra fields to your forms, you also can do that.
First, we have to understand why these fields are there.
The case for the *-DELETE
and *-ORDER
fields are added just if you enable them using:
formset = formset_factory(..., can_delete=True, can_order=True)
Django uses BaseFormSet.add_fields in order to add that extra fields it needs to enable ordering or deletion, you could use it to add your own additional fields too.
What can you do?
You can just pass False
there.
An interesting experiment ...
I perform an experiment in order to illustrate the effect overwritting this can have:
from django import forms
class MyForm(forms.Form):
title = forms.CharField()
class MyBaseFormSet(forms.BaseFormSet):
def add_fields(self, form, i):
# Avoiding FormSets using this to add additional fields
return form
# Create a form set with deletion and ordering enabled.
# pay attention to the argument: formset=MyBaseFormSet
MyFormSet = forms.formset_factory(MyForm, formset=MyBaseFormSet, can_delete=True, can_order=True)
fs = MyFormSet()
for form in fs:
print(form.as_table())
The result, no DELETE or ORDER fields added to the resulting forms.
>>> <tr><th><label for="id_form-0-title">Title:</label></th><td><input type="text" name="form-0-title" id="id_form-0-title" /></td></tr>
What about ForeignKey
That's something you can solve in the forms. If you don't want ForeignKey
to be displayed, you could use forms.HiddenInput
widget for such fields in your forms.
Also if you're working with ModelForms you can select the fields to use for generating the form.
Conclusion
With this info, I hope you to be able to plan how to implement your requirement of having a generic template for those formsets.
Starting points:
- Pass
can_delete
or/andstart_order
as False toformset_factory
. - Set
forms.HiddenInput
widget for ForeignKey fields in your forms. - Find a way to get the form class and use vars in order to find out the
original fields of the form. - If you're using ModelForms, use the
fields
orexclude
meta configuration in order to state which fields are used to build the form.
edited Apr 27 at 2:19
answered Mar 29 at 17:01
Raydel MirandaRaydel Miranda
10.8k22249
10.8k22249
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55207317%2fin-a-forms-of-an-inlineformset-how-can-i-know-which-are-my-default-form-fields-a%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown