Django form updating after adding data to database
I'm building a form for user's to run a query on a database. The model is called Sample and the user is selecting the names they want to include in the query results. In this case the name field is unique but other fields done the same way aren't.
Form.py
from django import forms
from .models import Sample
class SampleForm(forms.Form):
samples = Sample.objects.all()
names = [(s.id, s.name) for s in samples]
initial = [c[0] for c in names]
Rock_Names = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
choices=names,
initial=initial,
label='Name')
The problem I'm having is when a new sample has been added the form won't update with that sample name until I restart the server. I have a list view for all samples and it updates with the new sample fine. I've tried running Sample.objects.update()
after the new sample is saved, but that didn't help. Is there a way to force this to update? Is there a better way to build a query form?
Edit Okay, I thought through this and realized it is a more basic object oriented programming problem. The form is created using class attributes defined with the class definition (at server restart) and not when an instance is created (when the page reloads). I don't think I want to rerun the form creation every time the page loads (such as moving to instance attributes in an init method), so I moved the form creation to a function that is called at class definition and through a method I can call when I add data. The new code looks like:
from django import forms
from .models import Sample
def create_form():
samples = Sample.objects.all()
names = [(s.id, s.name) for s in samples]
initial = [c[0] for c in names]
Rock_Names = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
choices=names,
initial=initial,
label='Name')
return Rock_Names
class SampleForm(forms.Form):
Rock_Names = create_form()
def update_form():
Rock_Names = create_form()
django django-models django-forms
add a comment |
I'm building a form for user's to run a query on a database. The model is called Sample and the user is selecting the names they want to include in the query results. In this case the name field is unique but other fields done the same way aren't.
Form.py
from django import forms
from .models import Sample
class SampleForm(forms.Form):
samples = Sample.objects.all()
names = [(s.id, s.name) for s in samples]
initial = [c[0] for c in names]
Rock_Names = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
choices=names,
initial=initial,
label='Name')
The problem I'm having is when a new sample has been added the form won't update with that sample name until I restart the server. I have a list view for all samples and it updates with the new sample fine. I've tried running Sample.objects.update()
after the new sample is saved, but that didn't help. Is there a way to force this to update? Is there a better way to build a query form?
Edit Okay, I thought through this and realized it is a more basic object oriented programming problem. The form is created using class attributes defined with the class definition (at server restart) and not when an instance is created (when the page reloads). I don't think I want to rerun the form creation every time the page loads (such as moving to instance attributes in an init method), so I moved the form creation to a function that is called at class definition and through a method I can call when I add data. The new code looks like:
from django import forms
from .models import Sample
def create_form():
samples = Sample.objects.all()
names = [(s.id, s.name) for s in samples]
initial = [c[0] for c in names]
Rock_Names = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
choices=names,
initial=initial,
label='Name')
return Rock_Names
class SampleForm(forms.Form):
Rock_Names = create_form()
def update_form():
Rock_Names = create_form()
django django-models django-forms
add a comment |
I'm building a form for user's to run a query on a database. The model is called Sample and the user is selecting the names they want to include in the query results. In this case the name field is unique but other fields done the same way aren't.
Form.py
from django import forms
from .models import Sample
class SampleForm(forms.Form):
samples = Sample.objects.all()
names = [(s.id, s.name) for s in samples]
initial = [c[0] for c in names]
Rock_Names = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
choices=names,
initial=initial,
label='Name')
The problem I'm having is when a new sample has been added the form won't update with that sample name until I restart the server. I have a list view for all samples and it updates with the new sample fine. I've tried running Sample.objects.update()
after the new sample is saved, but that didn't help. Is there a way to force this to update? Is there a better way to build a query form?
Edit Okay, I thought through this and realized it is a more basic object oriented programming problem. The form is created using class attributes defined with the class definition (at server restart) and not when an instance is created (when the page reloads). I don't think I want to rerun the form creation every time the page loads (such as moving to instance attributes in an init method), so I moved the form creation to a function that is called at class definition and through a method I can call when I add data. The new code looks like:
from django import forms
from .models import Sample
def create_form():
samples = Sample.objects.all()
names = [(s.id, s.name) for s in samples]
initial = [c[0] for c in names]
Rock_Names = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
choices=names,
initial=initial,
label='Name')
return Rock_Names
class SampleForm(forms.Form):
Rock_Names = create_form()
def update_form():
Rock_Names = create_form()
django django-models django-forms
I'm building a form for user's to run a query on a database. The model is called Sample and the user is selecting the names they want to include in the query results. In this case the name field is unique but other fields done the same way aren't.
Form.py
from django import forms
from .models import Sample
class SampleForm(forms.Form):
samples = Sample.objects.all()
names = [(s.id, s.name) for s in samples]
initial = [c[0] for c in names]
Rock_Names = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
choices=names,
initial=initial,
label='Name')
The problem I'm having is when a new sample has been added the form won't update with that sample name until I restart the server. I have a list view for all samples and it updates with the new sample fine. I've tried running Sample.objects.update()
after the new sample is saved, but that didn't help. Is there a way to force this to update? Is there a better way to build a query form?
Edit Okay, I thought through this and realized it is a more basic object oriented programming problem. The form is created using class attributes defined with the class definition (at server restart) and not when an instance is created (when the page reloads). I don't think I want to rerun the form creation every time the page loads (such as moving to instance attributes in an init method), so I moved the form creation to a function that is called at class definition and through a method I can call when I add data. The new code looks like:
from django import forms
from .models import Sample
def create_form():
samples = Sample.objects.all()
names = [(s.id, s.name) for s in samples]
initial = [c[0] for c in names]
Rock_Names = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
choices=names,
initial=initial,
label='Name')
return Rock_Names
class SampleForm(forms.Form):
Rock_Names = create_form()
def update_form():
Rock_Names = create_form()
django django-models django-forms
django django-models django-forms
edited Nov 24 '18 at 17:14
user2236411
asked Nov 24 '18 at 15:48
user2236411user2236411
385
385
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
When you have a choice field with dynamic choices, setting them in the __init__
method is the recommended approach.
In your case, you can probably simply use a ModelMultipleChoiceField
instead of a MultipleChoiceField
:
class SampleForm(forms.Form):
rock_names = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
queryset=Sample.objects.all(),
label='Name')
To get Sample.name
as label, you either have to give Sample
a __str__
method:
class Sample(models.Model):
...
def __str__(self):
return self.name
Or create and use a subclass of ModelMultipleChoiceField
with a custom label_from_instance
method:
class SampleMultipleChoiceField(MultipleChoiceField):
def label_from_instance(self, obj):
return obj.name
class SampleForm(forms.Form):
rock_names = SampleMultipleChoiceField(widget=forms.CheckboxSelectMultiple,
queryset=Sample.objects.all(),
label='Name')
On a side note, the common convention is to write class attributes in lower case.
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%2f53459805%2fdjango-form-updating-after-adding-data-to-database%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
When you have a choice field with dynamic choices, setting them in the __init__
method is the recommended approach.
In your case, you can probably simply use a ModelMultipleChoiceField
instead of a MultipleChoiceField
:
class SampleForm(forms.Form):
rock_names = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
queryset=Sample.objects.all(),
label='Name')
To get Sample.name
as label, you either have to give Sample
a __str__
method:
class Sample(models.Model):
...
def __str__(self):
return self.name
Or create and use a subclass of ModelMultipleChoiceField
with a custom label_from_instance
method:
class SampleMultipleChoiceField(MultipleChoiceField):
def label_from_instance(self, obj):
return obj.name
class SampleForm(forms.Form):
rock_names = SampleMultipleChoiceField(widget=forms.CheckboxSelectMultiple,
queryset=Sample.objects.all(),
label='Name')
On a side note, the common convention is to write class attributes in lower case.
add a comment |
When you have a choice field with dynamic choices, setting them in the __init__
method is the recommended approach.
In your case, you can probably simply use a ModelMultipleChoiceField
instead of a MultipleChoiceField
:
class SampleForm(forms.Form):
rock_names = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
queryset=Sample.objects.all(),
label='Name')
To get Sample.name
as label, you either have to give Sample
a __str__
method:
class Sample(models.Model):
...
def __str__(self):
return self.name
Or create and use a subclass of ModelMultipleChoiceField
with a custom label_from_instance
method:
class SampleMultipleChoiceField(MultipleChoiceField):
def label_from_instance(self, obj):
return obj.name
class SampleForm(forms.Form):
rock_names = SampleMultipleChoiceField(widget=forms.CheckboxSelectMultiple,
queryset=Sample.objects.all(),
label='Name')
On a side note, the common convention is to write class attributes in lower case.
add a comment |
When you have a choice field with dynamic choices, setting them in the __init__
method is the recommended approach.
In your case, you can probably simply use a ModelMultipleChoiceField
instead of a MultipleChoiceField
:
class SampleForm(forms.Form):
rock_names = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
queryset=Sample.objects.all(),
label='Name')
To get Sample.name
as label, you either have to give Sample
a __str__
method:
class Sample(models.Model):
...
def __str__(self):
return self.name
Or create and use a subclass of ModelMultipleChoiceField
with a custom label_from_instance
method:
class SampleMultipleChoiceField(MultipleChoiceField):
def label_from_instance(self, obj):
return obj.name
class SampleForm(forms.Form):
rock_names = SampleMultipleChoiceField(widget=forms.CheckboxSelectMultiple,
queryset=Sample.objects.all(),
label='Name')
On a side note, the common convention is to write class attributes in lower case.
When you have a choice field with dynamic choices, setting them in the __init__
method is the recommended approach.
In your case, you can probably simply use a ModelMultipleChoiceField
instead of a MultipleChoiceField
:
class SampleForm(forms.Form):
rock_names = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
queryset=Sample.objects.all(),
label='Name')
To get Sample.name
as label, you either have to give Sample
a __str__
method:
class Sample(models.Model):
...
def __str__(self):
return self.name
Or create and use a subclass of ModelMultipleChoiceField
with a custom label_from_instance
method:
class SampleMultipleChoiceField(MultipleChoiceField):
def label_from_instance(self, obj):
return obj.name
class SampleForm(forms.Form):
rock_names = SampleMultipleChoiceField(widget=forms.CheckboxSelectMultiple,
queryset=Sample.objects.all(),
label='Name')
On a side note, the common convention is to write class attributes in lower case.
answered Nov 24 '18 at 17:50
Daniel HepperDaniel Hepper
17.2k44660
17.2k44660
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%2f53459805%2fdjango-form-updating-after-adding-data-to-database%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