What Query should I make to prefetch my data from first model to get filled when my form for second model...
models.py
class Appname(models.Model):
name=models.CharField(max_length=150,blank=False,null=False,help_text='Add your new App')
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse("dashapp:view")
class Adspace(models.Model):
name=models.ForeignKey(Appname,related_name='adspaces', null=True, default=None,on_delete=models.CASCADE)
ad_space=models.CharField(max_length=150,blank=False,null=False)
def __str__(self):
return self.ad_space
def get_absolute_url(self):
return reverse("dashapp:view")
Query to make
I create a form using CreateView for both the models. For First model i save the appname and then I create a listview of app names Using ListView and display them. When i click on one of the app from the displayed list it takes me to the next form which is for the next model Adspace
. Now I want as my form opens for second model the name section gets autofill with the name of the app with which i clicked to reach on that form.What Query should i make to make it possible.Please Explain it with your answer.
django python-3.x django-models django-forms django-views
add a comment |
models.py
class Appname(models.Model):
name=models.CharField(max_length=150,blank=False,null=False,help_text='Add your new App')
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse("dashapp:view")
class Adspace(models.Model):
name=models.ForeignKey(Appname,related_name='adspaces', null=True, default=None,on_delete=models.CASCADE)
ad_space=models.CharField(max_length=150,blank=False,null=False)
def __str__(self):
return self.ad_space
def get_absolute_url(self):
return reverse("dashapp:view")
Query to make
I create a form using CreateView for both the models. For First model i save the appname and then I create a listview of app names Using ListView and display them. When i click on one of the app from the displayed list it takes me to the next form which is for the next model Adspace
. Now I want as my form opens for second model the name section gets autofill with the name of the app with which i clicked to reach on that form.What Query should i make to make it possible.Please Explain it with your answer.
django python-3.x django-models django-forms django-views
Show us the code for your view and form for creatingAdspace
. Are you using aModelForm
?
– dirkgroten
Nov 21 '18 at 10:48
No , I'm using CreateView for making Form. And to display App names I'm using generic ListView .
– Anoop Sharma
Nov 21 '18 at 11:06
So When I click on any of the app names I go the Next form which is also made by using CreateView. In that Section I want the name section of the second model gets autofill by the app name which we clicked to reach to this form.
– Anoop Sharma
Nov 21 '18 at 11:08
How do you pass the app name that was clicked on to theCreateView
? Is it in the URL?
– dirkgroten
Nov 21 '18 at 11:11
No ,that is what i want to know,I'm a beginner. So I'm not passing anything when the Appname is being clicked as I dont know things like that can be done or not.
– Anoop Sharma
Nov 21 '18 at 11:30
add a comment |
models.py
class Appname(models.Model):
name=models.CharField(max_length=150,blank=False,null=False,help_text='Add your new App')
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse("dashapp:view")
class Adspace(models.Model):
name=models.ForeignKey(Appname,related_name='adspaces', null=True, default=None,on_delete=models.CASCADE)
ad_space=models.CharField(max_length=150,blank=False,null=False)
def __str__(self):
return self.ad_space
def get_absolute_url(self):
return reverse("dashapp:view")
Query to make
I create a form using CreateView for both the models. For First model i save the appname and then I create a listview of app names Using ListView and display them. When i click on one of the app from the displayed list it takes me to the next form which is for the next model Adspace
. Now I want as my form opens for second model the name section gets autofill with the name of the app with which i clicked to reach on that form.What Query should i make to make it possible.Please Explain it with your answer.
django python-3.x django-models django-forms django-views
models.py
class Appname(models.Model):
name=models.CharField(max_length=150,blank=False,null=False,help_text='Add your new App')
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse("dashapp:view")
class Adspace(models.Model):
name=models.ForeignKey(Appname,related_name='adspaces', null=True, default=None,on_delete=models.CASCADE)
ad_space=models.CharField(max_length=150,blank=False,null=False)
def __str__(self):
return self.ad_space
def get_absolute_url(self):
return reverse("dashapp:view")
Query to make
I create a form using CreateView for both the models. For First model i save the appname and then I create a listview of app names Using ListView and display them. When i click on one of the app from the displayed list it takes me to the next form which is for the next model Adspace
. Now I want as my form opens for second model the name section gets autofill with the name of the app with which i clicked to reach on that form.What Query should i make to make it possible.Please Explain it with your answer.
django python-3.x django-models django-forms django-views
django python-3.x django-models django-forms django-views
asked Nov 21 '18 at 9:05


Anoop SharmaAnoop Sharma
178
178
Show us the code for your view and form for creatingAdspace
. Are you using aModelForm
?
– dirkgroten
Nov 21 '18 at 10:48
No , I'm using CreateView for making Form. And to display App names I'm using generic ListView .
– Anoop Sharma
Nov 21 '18 at 11:06
So When I click on any of the app names I go the Next form which is also made by using CreateView. In that Section I want the name section of the second model gets autofill by the app name which we clicked to reach to this form.
– Anoop Sharma
Nov 21 '18 at 11:08
How do you pass the app name that was clicked on to theCreateView
? Is it in the URL?
– dirkgroten
Nov 21 '18 at 11:11
No ,that is what i want to know,I'm a beginner. So I'm not passing anything when the Appname is being clicked as I dont know things like that can be done or not.
– Anoop Sharma
Nov 21 '18 at 11:30
add a comment |
Show us the code for your view and form for creatingAdspace
. Are you using aModelForm
?
– dirkgroten
Nov 21 '18 at 10:48
No , I'm using CreateView for making Form. And to display App names I'm using generic ListView .
– Anoop Sharma
Nov 21 '18 at 11:06
So When I click on any of the app names I go the Next form which is also made by using CreateView. In that Section I want the name section of the second model gets autofill by the app name which we clicked to reach to this form.
– Anoop Sharma
Nov 21 '18 at 11:08
How do you pass the app name that was clicked on to theCreateView
? Is it in the URL?
– dirkgroten
Nov 21 '18 at 11:11
No ,that is what i want to know,I'm a beginner. So I'm not passing anything when the Appname is being clicked as I dont know things like that can be done or not.
– Anoop Sharma
Nov 21 '18 at 11:30
Show us the code for your view and form for creating
Adspace
. Are you using a ModelForm
?– dirkgroten
Nov 21 '18 at 10:48
Show us the code for your view and form for creating
Adspace
. Are you using a ModelForm
?– dirkgroten
Nov 21 '18 at 10:48
No , I'm using CreateView for making Form. And to display App names I'm using generic ListView .
– Anoop Sharma
Nov 21 '18 at 11:06
No , I'm using CreateView for making Form. And to display App names I'm using generic ListView .
– Anoop Sharma
Nov 21 '18 at 11:06
So When I click on any of the app names I go the Next form which is also made by using CreateView. In that Section I want the name section of the second model gets autofill by the app name which we clicked to reach to this form.
– Anoop Sharma
Nov 21 '18 at 11:08
So When I click on any of the app names I go the Next form which is also made by using CreateView. In that Section I want the name section of the second model gets autofill by the app name which we clicked to reach to this form.
– Anoop Sharma
Nov 21 '18 at 11:08
How do you pass the app name that was clicked on to the
CreateView
? Is it in the URL?– dirkgroten
Nov 21 '18 at 11:11
How do you pass the app name that was clicked on to the
CreateView
? Is it in the URL?– dirkgroten
Nov 21 '18 at 11:11
No ,that is what i want to know,I'm a beginner. So I'm not passing anything when the Appname is being clicked as I dont know things like that can be done or not.
– Anoop Sharma
Nov 21 '18 at 11:30
No ,that is what i want to know,I'm a beginner. So I'm not passing anything when the Appname is being clicked as I dont know things like that can be done or not.
– Anoop Sharma
Nov 21 '18 at 11:30
add a comment |
1 Answer
1
active
oldest
votes
If you're using a generic CreateView
, you can override the get_initial()
method to set initial values for the object that's being created:
def get_initial(self):
# assuming the Appname id is passed in the view's kwargs as "app_id"
try:
app = Appname.objects.get(id=self.kwargs['app_id'])
return super().get_initial().update({'name': app})
except Appname.DoesNotExist:
return super().get_initial()
How can I pass app_id in views kwargs .??
– Anoop Sharma
Nov 21 '18 at 11:34
Well, you'll need to create a url path that has theapp_id
as one of the parameters, so it'll be available to your view. Read the documentation. This is basic Django, please take some time to learn it properly (do the entire tutorial step by step).
– dirkgroten
Nov 21 '18 at 11:35
Ohkie Sir,Let me go through this, If i get stuck somewhere I'll ping you here.
– Anoop Sharma
Nov 21 '18 at 11:51
Ask a new question then, the answer to this question is to overrideget_initial()
– dirkgroten
Nov 21 '18 at 11:51
I used get_initial() method in my View and I'm getting the appname but it is not able to autofill appname in the next form. But I'm getting the appnames as scrollbox their not as autofill
– Anoop Sharma
Nov 21 '18 at 14:41
|
show 8 more comments
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%2f53408507%2fwhat-query-should-i-make-to-prefetch-my-data-from-first-model-to-get-filled-when%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
If you're using a generic CreateView
, you can override the get_initial()
method to set initial values for the object that's being created:
def get_initial(self):
# assuming the Appname id is passed in the view's kwargs as "app_id"
try:
app = Appname.objects.get(id=self.kwargs['app_id'])
return super().get_initial().update({'name': app})
except Appname.DoesNotExist:
return super().get_initial()
How can I pass app_id in views kwargs .??
– Anoop Sharma
Nov 21 '18 at 11:34
Well, you'll need to create a url path that has theapp_id
as one of the parameters, so it'll be available to your view. Read the documentation. This is basic Django, please take some time to learn it properly (do the entire tutorial step by step).
– dirkgroten
Nov 21 '18 at 11:35
Ohkie Sir,Let me go through this, If i get stuck somewhere I'll ping you here.
– Anoop Sharma
Nov 21 '18 at 11:51
Ask a new question then, the answer to this question is to overrideget_initial()
– dirkgroten
Nov 21 '18 at 11:51
I used get_initial() method in my View and I'm getting the appname but it is not able to autofill appname in the next form. But I'm getting the appnames as scrollbox their not as autofill
– Anoop Sharma
Nov 21 '18 at 14:41
|
show 8 more comments
If you're using a generic CreateView
, you can override the get_initial()
method to set initial values for the object that's being created:
def get_initial(self):
# assuming the Appname id is passed in the view's kwargs as "app_id"
try:
app = Appname.objects.get(id=self.kwargs['app_id'])
return super().get_initial().update({'name': app})
except Appname.DoesNotExist:
return super().get_initial()
How can I pass app_id in views kwargs .??
– Anoop Sharma
Nov 21 '18 at 11:34
Well, you'll need to create a url path that has theapp_id
as one of the parameters, so it'll be available to your view. Read the documentation. This is basic Django, please take some time to learn it properly (do the entire tutorial step by step).
– dirkgroten
Nov 21 '18 at 11:35
Ohkie Sir,Let me go through this, If i get stuck somewhere I'll ping you here.
– Anoop Sharma
Nov 21 '18 at 11:51
Ask a new question then, the answer to this question is to overrideget_initial()
– dirkgroten
Nov 21 '18 at 11:51
I used get_initial() method in my View and I'm getting the appname but it is not able to autofill appname in the next form. But I'm getting the appnames as scrollbox their not as autofill
– Anoop Sharma
Nov 21 '18 at 14:41
|
show 8 more comments
If you're using a generic CreateView
, you can override the get_initial()
method to set initial values for the object that's being created:
def get_initial(self):
# assuming the Appname id is passed in the view's kwargs as "app_id"
try:
app = Appname.objects.get(id=self.kwargs['app_id'])
return super().get_initial().update({'name': app})
except Appname.DoesNotExist:
return super().get_initial()
If you're using a generic CreateView
, you can override the get_initial()
method to set initial values for the object that's being created:
def get_initial(self):
# assuming the Appname id is passed in the view's kwargs as "app_id"
try:
app = Appname.objects.get(id=self.kwargs['app_id'])
return super().get_initial().update({'name': app})
except Appname.DoesNotExist:
return super().get_initial()
answered Nov 21 '18 at 11:16
dirkgrotendirkgroten
4,65811221
4,65811221
How can I pass app_id in views kwargs .??
– Anoop Sharma
Nov 21 '18 at 11:34
Well, you'll need to create a url path that has theapp_id
as one of the parameters, so it'll be available to your view. Read the documentation. This is basic Django, please take some time to learn it properly (do the entire tutorial step by step).
– dirkgroten
Nov 21 '18 at 11:35
Ohkie Sir,Let me go through this, If i get stuck somewhere I'll ping you here.
– Anoop Sharma
Nov 21 '18 at 11:51
Ask a new question then, the answer to this question is to overrideget_initial()
– dirkgroten
Nov 21 '18 at 11:51
I used get_initial() method in my View and I'm getting the appname but it is not able to autofill appname in the next form. But I'm getting the appnames as scrollbox their not as autofill
– Anoop Sharma
Nov 21 '18 at 14:41
|
show 8 more comments
How can I pass app_id in views kwargs .??
– Anoop Sharma
Nov 21 '18 at 11:34
Well, you'll need to create a url path that has theapp_id
as one of the parameters, so it'll be available to your view. Read the documentation. This is basic Django, please take some time to learn it properly (do the entire tutorial step by step).
– dirkgroten
Nov 21 '18 at 11:35
Ohkie Sir,Let me go through this, If i get stuck somewhere I'll ping you here.
– Anoop Sharma
Nov 21 '18 at 11:51
Ask a new question then, the answer to this question is to overrideget_initial()
– dirkgroten
Nov 21 '18 at 11:51
I used get_initial() method in my View and I'm getting the appname but it is not able to autofill appname in the next form. But I'm getting the appnames as scrollbox their not as autofill
– Anoop Sharma
Nov 21 '18 at 14:41
How can I pass app_id in views kwargs .??
– Anoop Sharma
Nov 21 '18 at 11:34
How can I pass app_id in views kwargs .??
– Anoop Sharma
Nov 21 '18 at 11:34
Well, you'll need to create a url path that has the
app_id
as one of the parameters, so it'll be available to your view. Read the documentation. This is basic Django, please take some time to learn it properly (do the entire tutorial step by step).– dirkgroten
Nov 21 '18 at 11:35
Well, you'll need to create a url path that has the
app_id
as one of the parameters, so it'll be available to your view. Read the documentation. This is basic Django, please take some time to learn it properly (do the entire tutorial step by step).– dirkgroten
Nov 21 '18 at 11:35
Ohkie Sir,Let me go through this, If i get stuck somewhere I'll ping you here.
– Anoop Sharma
Nov 21 '18 at 11:51
Ohkie Sir,Let me go through this, If i get stuck somewhere I'll ping you here.
– Anoop Sharma
Nov 21 '18 at 11:51
Ask a new question then, the answer to this question is to override
get_initial()
– dirkgroten
Nov 21 '18 at 11:51
Ask a new question then, the answer to this question is to override
get_initial()
– dirkgroten
Nov 21 '18 at 11:51
I used get_initial() method in my View and I'm getting the appname but it is not able to autofill appname in the next form. But I'm getting the appnames as scrollbox their not as autofill
– Anoop Sharma
Nov 21 '18 at 14:41
I used get_initial() method in my View and I'm getting the appname but it is not able to autofill appname in the next form. But I'm getting the appnames as scrollbox their not as autofill
– Anoop Sharma
Nov 21 '18 at 14:41
|
show 8 more comments
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%2f53408507%2fwhat-query-should-i-make-to-prefetch-my-data-from-first-model-to-get-filled-when%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
Show us the code for your view and form for creating
Adspace
. Are you using aModelForm
?– dirkgroten
Nov 21 '18 at 10:48
No , I'm using CreateView for making Form. And to display App names I'm using generic ListView .
– Anoop Sharma
Nov 21 '18 at 11:06
So When I click on any of the app names I go the Next form which is also made by using CreateView. In that Section I want the name section of the second model gets autofill by the app name which we clicked to reach to this form.
– Anoop Sharma
Nov 21 '18 at 11:08
How do you pass the app name that was clicked on to the
CreateView
? Is it in the URL?– dirkgroten
Nov 21 '18 at 11:11
No ,that is what i want to know,I'm a beginner. So I'm not passing anything when the Appname is being clicked as I dont know things like that can be done or not.
– Anoop Sharma
Nov 21 '18 at 11:30