how to save dates into database using django?












0















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










share|improve this question























  • 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











  • 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
















0















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










share|improve this question























  • 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











  • 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














0












0








0








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










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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











  • 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











  • 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

















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












1 Answer
1






active

oldest

votes


















0














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)





share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    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)





    share|improve this answer




























      0














      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)





      share|improve this answer


























        0












        0








        0







        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)





        share|improve this answer













        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)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 22:56









        ISRAEL MORALES RIVERAISRAEL MORALES RIVERA

        1




        1






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen