how to save dates into database using django?
this is my code
views.py
def guardar(request):
if request.method == "POST":
idpersona = int(request.POST.get('id'))
persona = personal.objects.get(id=idpersona)
idep = int(request.POST.get('dependencia'))
dep = dependencia.objects.get(id=idep)
idcon = int(request.POST.get('concepto'))
con = concepto.objects.get(id=idcon)
fecha = request.POST.get('fecha')
print(fecha)
db_registro = lista_registro(
personal_id=persona,
fecha_registro=fecha,
dependencia_id=dep,
concepto_id=con,
descripcion=request.POST.get('descripcion'),
monto=request.POST.get('monto'),
)
db_registro.save()
return render(request, 'registro/exito.html')
Models.py
class lista_registro(models.Model):
personal_id = models.ForeignKey(personal, on_delete=models.CASCADE)
dependencia_id = models.ForeignKey(dependencia, on_delete=models.CASCADE)
concepto_id = models.ForeignKey(concepto, on_delete=models.CASCADE)
fecha_realizado = models.DateField()
fecha_registro = models.DateField(auto_now_add=True)
descripcion = models.CharField(max_length=1000)
pago_id = models.ForeignKey(pago, on_delete=models.CASCADE, default=2)
monto = models.CharField(max_length=100)
def __str__(self):
return "===> " + self.descripcion + " <==="
well my problem is that I get a NOT NULL constraint error. I get that this is a date error but I don't know how to solve this
django date format
add a comment |
this is my code
views.py
def guardar(request):
if request.method == "POST":
idpersona = int(request.POST.get('id'))
persona = personal.objects.get(id=idpersona)
idep = int(request.POST.get('dependencia'))
dep = dependencia.objects.get(id=idep)
idcon = int(request.POST.get('concepto'))
con = concepto.objects.get(id=idcon)
fecha = request.POST.get('fecha')
print(fecha)
db_registro = lista_registro(
personal_id=persona,
fecha_registro=fecha,
dependencia_id=dep,
concepto_id=con,
descripcion=request.POST.get('descripcion'),
monto=request.POST.get('monto'),
)
db_registro.save()
return render(request, 'registro/exito.html')
Models.py
class lista_registro(models.Model):
personal_id = models.ForeignKey(personal, on_delete=models.CASCADE)
dependencia_id = models.ForeignKey(dependencia, on_delete=models.CASCADE)
concepto_id = models.ForeignKey(concepto, on_delete=models.CASCADE)
fecha_realizado = models.DateField()
fecha_registro = models.DateField(auto_now_add=True)
descripcion = models.CharField(max_length=1000)
pago_id = models.ForeignKey(pago, on_delete=models.CASCADE, default=2)
monto = models.CharField(max_length=100)
def __str__(self):
return "===> " + self.descripcion + " <==="
well my problem is that I get a NOT NULL constraint error. I get that this is a date error but I don't know how to solve this
django date format
Welcome to Stack Overflow. Please show the full traceback and error message when you ask questions - it will show exactly where the error is occuring. It looks like it is thefecha_realizado
field that is causing the problem. You can avoid the error by either settingnull=True
on the field, or by setting a value before you save it (e.g.fecha_realizado=fecha
).
– Alasdair
Nov 22 '18 at 20:13
Note that it's not a good idea to fetch data fromrequest.POST
like this. Django forms are very helpful for tasks like this, in particular model forms.
– Alasdair
Nov 22 '18 at 20:14
Instead of operating directly on the request parameters you should be cleaning and validating your data with a Django Form.
– coler-j
Nov 22 '18 at 20:58
I know it is not safe to fetch data from request.POST but in the django forms the default widget for a foreign key field is a select input and I have to handle like 300 data. I did not found other way. (English it is not my natural language so sorry if I can't explain myself well)
– Jorge Maidana
Nov 28 '18 at 18:02
add a comment |
this is my code
views.py
def guardar(request):
if request.method == "POST":
idpersona = int(request.POST.get('id'))
persona = personal.objects.get(id=idpersona)
idep = int(request.POST.get('dependencia'))
dep = dependencia.objects.get(id=idep)
idcon = int(request.POST.get('concepto'))
con = concepto.objects.get(id=idcon)
fecha = request.POST.get('fecha')
print(fecha)
db_registro = lista_registro(
personal_id=persona,
fecha_registro=fecha,
dependencia_id=dep,
concepto_id=con,
descripcion=request.POST.get('descripcion'),
monto=request.POST.get('monto'),
)
db_registro.save()
return render(request, 'registro/exito.html')
Models.py
class lista_registro(models.Model):
personal_id = models.ForeignKey(personal, on_delete=models.CASCADE)
dependencia_id = models.ForeignKey(dependencia, on_delete=models.CASCADE)
concepto_id = models.ForeignKey(concepto, on_delete=models.CASCADE)
fecha_realizado = models.DateField()
fecha_registro = models.DateField(auto_now_add=True)
descripcion = models.CharField(max_length=1000)
pago_id = models.ForeignKey(pago, on_delete=models.CASCADE, default=2)
monto = models.CharField(max_length=100)
def __str__(self):
return "===> " + self.descripcion + " <==="
well my problem is that I get a NOT NULL constraint error. I get that this is a date error but I don't know how to solve this
django date format
this is my code
views.py
def guardar(request):
if request.method == "POST":
idpersona = int(request.POST.get('id'))
persona = personal.objects.get(id=idpersona)
idep = int(request.POST.get('dependencia'))
dep = dependencia.objects.get(id=idep)
idcon = int(request.POST.get('concepto'))
con = concepto.objects.get(id=idcon)
fecha = request.POST.get('fecha')
print(fecha)
db_registro = lista_registro(
personal_id=persona,
fecha_registro=fecha,
dependencia_id=dep,
concepto_id=con,
descripcion=request.POST.get('descripcion'),
monto=request.POST.get('monto'),
)
db_registro.save()
return render(request, 'registro/exito.html')
Models.py
class lista_registro(models.Model):
personal_id = models.ForeignKey(personal, on_delete=models.CASCADE)
dependencia_id = models.ForeignKey(dependencia, on_delete=models.CASCADE)
concepto_id = models.ForeignKey(concepto, on_delete=models.CASCADE)
fecha_realizado = models.DateField()
fecha_registro = models.DateField(auto_now_add=True)
descripcion = models.CharField(max_length=1000)
pago_id = models.ForeignKey(pago, on_delete=models.CASCADE, default=2)
monto = models.CharField(max_length=100)
def __str__(self):
return "===> " + self.descripcion + " <==="
well my problem is that I get a NOT NULL constraint error. I get that this is a date error but I don't know how to solve this
django date format
django date format
asked Nov 22 '18 at 19:17
Jorge MaidanaJorge Maidana
12
12
Welcome to Stack Overflow. Please show the full traceback and error message when you ask questions - it will show exactly where the error is occuring. It looks like it is thefecha_realizado
field that is causing the problem. You can avoid the error by either settingnull=True
on the field, or by setting a value before you save it (e.g.fecha_realizado=fecha
).
– Alasdair
Nov 22 '18 at 20:13
Note that it's not a good idea to fetch data fromrequest.POST
like this. Django forms are very helpful for tasks like this, in particular model forms.
– Alasdair
Nov 22 '18 at 20:14
Instead of operating directly on the request parameters you should be cleaning and validating your data with a Django Form.
– coler-j
Nov 22 '18 at 20:58
I know it is not safe to fetch data from request.POST but in the django forms the default widget for a foreign key field is a select input and I have to handle like 300 data. I did not found other way. (English it is not my natural language so sorry if I can't explain myself well)
– Jorge Maidana
Nov 28 '18 at 18:02
add a comment |
Welcome to Stack Overflow. Please show the full traceback and error message when you ask questions - it will show exactly where the error is occuring. It looks like it is thefecha_realizado
field that is causing the problem. You can avoid the error by either settingnull=True
on the field, or by setting a value before you save it (e.g.fecha_realizado=fecha
).
– Alasdair
Nov 22 '18 at 20:13
Note that it's not a good idea to fetch data fromrequest.POST
like this. Django forms are very helpful for tasks like this, in particular model forms.
– Alasdair
Nov 22 '18 at 20:14
Instead of operating directly on the request parameters you should be cleaning and validating your data with a Django Form.
– coler-j
Nov 22 '18 at 20:58
I know it is not safe to fetch data from request.POST but in the django forms the default widget for a foreign key field is a select input and I have to handle like 300 data. I did not found other way. (English it is not my natural language so sorry if I can't explain myself well)
– Jorge Maidana
Nov 28 '18 at 18:02
Welcome to Stack Overflow. Please show the full traceback and error message when you ask questions - it will show exactly where the error is occuring. It looks like it is the
fecha_realizado
field that is causing the problem. You can avoid the error by either setting null=True
on the field, or by setting a value before you save it (e.g. fecha_realizado=fecha
).– Alasdair
Nov 22 '18 at 20:13
Welcome to Stack Overflow. Please show the full traceback and error message when you ask questions - it will show exactly where the error is occuring. It looks like it is the
fecha_realizado
field that is causing the problem. You can avoid the error by either setting null=True
on the field, or by setting a value before you save it (e.g. fecha_realizado=fecha
).– Alasdair
Nov 22 '18 at 20:13
Note that it's not a good idea to fetch data from
request.POST
like this. Django forms are very helpful for tasks like this, in particular model forms.– Alasdair
Nov 22 '18 at 20:14
Note that it's not a good idea to fetch data from
request.POST
like this. Django forms are very helpful for tasks like this, in particular model forms.– Alasdair
Nov 22 '18 at 20:14
Instead of operating directly on the request parameters you should be cleaning and validating your data with a Django Form.
– coler-j
Nov 22 '18 at 20:58
Instead of operating directly on the request parameters you should be cleaning and validating your data with a Django Form.
– coler-j
Nov 22 '18 at 20:58
I know it is not safe to fetch data from request.POST but in the django forms the default widget for a foreign key field is a select input and I have to handle like 300 data. I did not found other way. (English it is not my natural language so sorry if I can't explain myself well)
– Jorge Maidana
Nov 28 '18 at 18:02
I know it is not safe to fetch data from request.POST but in the django forms the default widget for a foreign key field is a select input and I have to handle like 300 data. I did not found other way. (English it is not my natural language so sorry if I can't explain myself well)
– Jorge Maidana
Nov 28 '18 at 18:02
add a comment |
1 Answer
1
active
oldest
votes
when you define a field that will not be required just set null=True, blank=True on your models file
fecha_realizado = models.DateField(null=True, blank=True)
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%2f53436971%2fhow-to-save-dates-into-database-using-django%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 define a field that will not be required just set null=True, blank=True on your models file
fecha_realizado = models.DateField(null=True, blank=True)
add a comment |
when you define a field that will not be required just set null=True, blank=True on your models file
fecha_realizado = models.DateField(null=True, blank=True)
add a comment |
when you define a field that will not be required just set null=True, blank=True on your models file
fecha_realizado = models.DateField(null=True, blank=True)
when you define a field that will not be required just set null=True, blank=True on your models file
fecha_realizado = models.DateField(null=True, blank=True)
answered Nov 22 '18 at 22:56
ISRAEL MORALES RIVERAISRAEL MORALES RIVERA
1
1
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%2f53436971%2fhow-to-save-dates-into-database-using-django%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
Welcome to Stack Overflow. Please show the full traceback and error message when you ask questions - it will show exactly where the error is occuring. It looks like it is the
fecha_realizado
field that is causing the problem. You can avoid the error by either settingnull=True
on the field, or by setting a value before you save it (e.g.fecha_realizado=fecha
).– Alasdair
Nov 22 '18 at 20:13
Note that it's not a good idea to fetch data from
request.POST
like this. Django forms are very helpful for tasks like this, in particular model forms.– Alasdair
Nov 22 '18 at 20:14
Instead of operating directly on the request parameters you should be cleaning and validating your data with a Django Form.
– coler-j
Nov 22 '18 at 20:58
I know it is not safe to fetch data from request.POST but in the django forms the default widget for a foreign key field is a select input and I have to handle like 300 data. I did not found other way. (English it is not my natural language so sorry if I can't explain myself well)
– Jorge Maidana
Nov 28 '18 at 18:02