How can I update a Model and do something with this object or other?
My model has 7 fields, but I want update a only field. I'm using CBC(generic.UpdateView
) with model and form_class
. My forms works normally, but,
I want do things depending on value for this Field.
I don't know what method override.
ModelA:
atribute_a
atribute_b
atribute_c
I need change only atribute_a
, but atribute_b
is a dateTime
, and a I want update atribute_b
with timezone.now()
when update atribute_a
, and after that I want update a another object with ForeignKey with atribute_c
for example.
basically in a UpdateView, I want this:
In a some method(save, post, dispatch or other)
atribute_b = timezone.now()
if atribute_a == 'abc':
ModelB.objects.filter(atribute_c=atribute_c).update(atribute_c = atribute_c+1)
python django django-models django-views
add a comment |
My model has 7 fields, but I want update a only field. I'm using CBC(generic.UpdateView
) with model and form_class
. My forms works normally, but,
I want do things depending on value for this Field.
I don't know what method override.
ModelA:
atribute_a
atribute_b
atribute_c
I need change only atribute_a
, but atribute_b
is a dateTime
, and a I want update atribute_b
with timezone.now()
when update atribute_a
, and after that I want update a another object with ForeignKey with atribute_c
for example.
basically in a UpdateView, I want this:
In a some method(save, post, dispatch or other)
atribute_b = timezone.now()
if atribute_a == 'abc':
ModelB.objects.filter(atribute_c=atribute_c).update(atribute_c = atribute_c+1)
python django django-models django-views
Did you look at Django Signals, I would check out thepost_save
andpre_save
signals. You can also override the model'ssave
method, but I don't think that's good practice
– robotHamster
Nov 20 '18 at 4:06
you can addauto_now
to attribute_b
– ruddra
Nov 20 '18 at 4:26
This a update, auto_now when object is created, I want the time when object will change. The signal is when, always object have changes, created or updated, I want make changes only in this casa?
– Matheus Soares
Nov 20 '18 at 11:16
How can I make Update with TemplateView??
– Matheus Soares
Nov 20 '18 at 11:17
add a comment |
My model has 7 fields, but I want update a only field. I'm using CBC(generic.UpdateView
) with model and form_class
. My forms works normally, but,
I want do things depending on value for this Field.
I don't know what method override.
ModelA:
atribute_a
atribute_b
atribute_c
I need change only atribute_a
, but atribute_b
is a dateTime
, and a I want update atribute_b
with timezone.now()
when update atribute_a
, and after that I want update a another object with ForeignKey with atribute_c
for example.
basically in a UpdateView, I want this:
In a some method(save, post, dispatch or other)
atribute_b = timezone.now()
if atribute_a == 'abc':
ModelB.objects.filter(atribute_c=atribute_c).update(atribute_c = atribute_c+1)
python django django-models django-views
My model has 7 fields, but I want update a only field. I'm using CBC(generic.UpdateView
) with model and form_class
. My forms works normally, but,
I want do things depending on value for this Field.
I don't know what method override.
ModelA:
atribute_a
atribute_b
atribute_c
I need change only atribute_a
, but atribute_b
is a dateTime
, and a I want update atribute_b
with timezone.now()
when update atribute_a
, and after that I want update a another object with ForeignKey with atribute_c
for example.
basically in a UpdateView, I want this:
In a some method(save, post, dispatch or other)
atribute_b = timezone.now()
if atribute_a == 'abc':
ModelB.objects.filter(atribute_c=atribute_c).update(atribute_c = atribute_c+1)
python django django-models django-views
python django django-models django-views
edited Nov 20 '18 at 6:36
Aqueous Carlos
364314
364314
asked Nov 20 '18 at 3:23
Matheus SoaresMatheus Soares
417
417
Did you look at Django Signals, I would check out thepost_save
andpre_save
signals. You can also override the model'ssave
method, but I don't think that's good practice
– robotHamster
Nov 20 '18 at 4:06
you can addauto_now
to attribute_b
– ruddra
Nov 20 '18 at 4:26
This a update, auto_now when object is created, I want the time when object will change. The signal is when, always object have changes, created or updated, I want make changes only in this casa?
– Matheus Soares
Nov 20 '18 at 11:16
How can I make Update with TemplateView??
– Matheus Soares
Nov 20 '18 at 11:17
add a comment |
Did you look at Django Signals, I would check out thepost_save
andpre_save
signals. You can also override the model'ssave
method, but I don't think that's good practice
– robotHamster
Nov 20 '18 at 4:06
you can addauto_now
to attribute_b
– ruddra
Nov 20 '18 at 4:26
This a update, auto_now when object is created, I want the time when object will change. The signal is when, always object have changes, created or updated, I want make changes only in this casa?
– Matheus Soares
Nov 20 '18 at 11:16
How can I make Update with TemplateView??
– Matheus Soares
Nov 20 '18 at 11:17
Did you look at Django Signals, I would check out the
post_save
and pre_save
signals. You can also override the model's save
method, but I don't think that's good practice– robotHamster
Nov 20 '18 at 4:06
Did you look at Django Signals, I would check out the
post_save
and pre_save
signals. You can also override the model's save
method, but I don't think that's good practice– robotHamster
Nov 20 '18 at 4:06
you can add
auto_now
to attribute_b– ruddra
Nov 20 '18 at 4:26
you can add
auto_now
to attribute_b– ruddra
Nov 20 '18 at 4:26
This a update, auto_now when object is created, I want the time when object will change. The signal is when, always object have changes, created or updated, I want make changes only in this casa?
– Matheus Soares
Nov 20 '18 at 11:16
This a update, auto_now when object is created, I want the time when object will change. The signal is when, always object have changes, created or updated, I want make changes only in this casa?
– Matheus Soares
Nov 20 '18 at 11:16
How can I make Update with TemplateView??
– Matheus Soares
Nov 20 '18 at 11:17
How can I make Update with TemplateView??
– Matheus Soares
Nov 20 '18 at 11:17
add a comment |
1 Answer
1
active
oldest
votes
I used def save(self, commit=True):
in my forms.py
, and in my view.py
I used generic.UpdateView
with model
and form_class
.
code from my forms.
class MyForm(ModelForm):
class Meta:
fields = ['atribute_a']
exclude = ['atribute_b', 'atribute_c']
def save(self, commit=True):
instance = super(MyForm, self).save(commit=False)
instance.atribute_b = timezone.now()
# do something with FK
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%2f53385758%2fhow-can-i-update-a-model-and-do-something-with-this-object-or-other%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
I used def save(self, commit=True):
in my forms.py
, and in my view.py
I used generic.UpdateView
with model
and form_class
.
code from my forms.
class MyForm(ModelForm):
class Meta:
fields = ['atribute_a']
exclude = ['atribute_b', 'atribute_c']
def save(self, commit=True):
instance = super(MyForm, self).save(commit=False)
instance.atribute_b = timezone.now()
# do something with FK
add a comment |
I used def save(self, commit=True):
in my forms.py
, and in my view.py
I used generic.UpdateView
with model
and form_class
.
code from my forms.
class MyForm(ModelForm):
class Meta:
fields = ['atribute_a']
exclude = ['atribute_b', 'atribute_c']
def save(self, commit=True):
instance = super(MyForm, self).save(commit=False)
instance.atribute_b = timezone.now()
# do something with FK
add a comment |
I used def save(self, commit=True):
in my forms.py
, and in my view.py
I used generic.UpdateView
with model
and form_class
.
code from my forms.
class MyForm(ModelForm):
class Meta:
fields = ['atribute_a']
exclude = ['atribute_b', 'atribute_c']
def save(self, commit=True):
instance = super(MyForm, self).save(commit=False)
instance.atribute_b = timezone.now()
# do something with FK
I used def save(self, commit=True):
in my forms.py
, and in my view.py
I used generic.UpdateView
with model
and form_class
.
code from my forms.
class MyForm(ModelForm):
class Meta:
fields = ['atribute_a']
exclude = ['atribute_b', 'atribute_c']
def save(self, commit=True):
instance = super(MyForm, self).save(commit=False)
instance.atribute_b = timezone.now()
# do something with FK
answered Nov 20 '18 at 17:22
Matheus SoaresMatheus Soares
417
417
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%2f53385758%2fhow-can-i-update-a-model-and-do-something-with-this-object-or-other%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
Did you look at Django Signals, I would check out the
post_save
andpre_save
signals. You can also override the model'ssave
method, but I don't think that's good practice– robotHamster
Nov 20 '18 at 4:06
you can add
auto_now
to attribute_b– ruddra
Nov 20 '18 at 4:26
This a update, auto_now when object is created, I want the time when object will change. The signal is when, always object have changes, created or updated, I want make changes only in this casa?
– Matheus Soares
Nov 20 '18 at 11:16
How can I make Update with TemplateView??
– Matheus Soares
Nov 20 '18 at 11:17