0

While using Django Forms I have no solution for converting a decimal number to an integer.

Use case: input field on a HTML form generated by Django should allow for decimals. The database will only allow for integers. As Django builds the form using javascript for this is not so straightforward.

I found Django creating a custom model field but with so many new tools around with Django haven't been able to get it working.

Has anybody an advice of how to get this done with a custom model field?

Thanks, Gert

--- update ---

currently everything works as it should. Though I'm re-writing this app in Django form another framework. In that framework I used JavaScript to tackle converting decimals to integers.

I'd prefer just to have integer or text values in the database as using the date in different systems might give me problems if I start to use decimals.

What I currently have:

models.py
-----------

from django.db import models
from django.urls import reverse
from datetime import date


CHOICES_PERSONEN = (
    (1, "1"),
    (2, "2"),
)

class Nota(models.Model):
    name = models.CharField(max_length=255)
    d_s = models.PositiveSmallIntegerField(
        choices = CHOICES_PERSONEN,
        default = 2,
    )
    # amount = models.IntegerField(null=True, blank=True)
    amount = models.CurrencyField()


views.py
-----------
from django.contrib.admin.widgets import AdminDateWidget
from django.views.generic import ListView, DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView 
from django.urls import reverse_lazy
from bootstrap_datepicker_plus.widgets import DatePickerInput


from .models import Nota

# Create your views here.
class NotaListView(ListView):
    model = Nota
    template_name = "home_nota.html"

class NotaDetailView(DetailView):
    model = Nota
    template_name = "nota_detail.html"

class NotaCreateView(CreateView):
    model = Nota
    template_name = "nota_new.html"
    fields = ["name"]

class NotaUpdateView(UpdateView):
    model = Nota
    template_name = "nota_edit.html"
    fields = ["name", "d_s", "amount"] 


class NotaDeleteView(DeleteView):
    model = Nota
    template_name = "nota_delete.html"
    success_url = reverse_lazy("home_nota")
Gert
  • 25
  • 5
  • the context is not clear, could you post models, forms you have so far. Do you have a Floatfield in your model that needs to be converted to Int before saving to db? which db setup do you have? do you access a db that is originally not created by migrations from your django app? – Razenstein Apr 13 '23 at 16:31

0 Answers0