Django | Decimal Field is required even tho i set it to NULL=True

CruZer0

im trying to play a little bit around with django but i have run into problems... I have a Decimal Field which is not required so i set it to "blank=True" and "null=True". But it still says its required :(

I also did all the migrations.

Here is my models.py

from django.db import models

weightUnit = {
    ('kg' , 'kilogram'),
    ('g', 'gram'),
    ('t', 'tons'),
    ('n', '-'),
}

class Product(models.Model):
    pname = models.CharField(
        max_length=50,
    )
    pdesc = models.TextField(
        max_length=5000,
    )
    pprice = models.DecimalField(
        max_digits=6,
        decimal_places=2,
    )
    psn = models.CharField(
        max_length = 30,
        null=True,
        blank=True,
    )
    pweightunit = models.CharField(
        choices=weightUnit,
        default='n',
        null=True,
        blank=True,
        max_length=5,

    )
    pweight = models.DecimalField(
        null=True,
        blank = True,
        max_digits=10000,
        decimal_places=2,
    )

    plimage = models.ImageField(
        blank=True,
        null=True,
    )

Here is my forms.py

from django import forms
from .models import weightUnit

class RawProductForm(forms.Form):
    name = forms.CharField(label="Name")
    desc = forms.CharField(label="Beschreibung")
    price = forms.DecimalField(label="Stückpreis")
    sn = forms.CharField(label="Seriennummer")
    weightunit = forms.ChoiceField(choices=weightUnit, label="Gewichteinheit")
    weight = forms.DecimalField(label="Gewicht")
    image = forms.ImageField(label="Bild")

Here is my views.py

def product_add(request):
    pf = RawProductForm()
    if request.method == "POST":
        pf = RawProductForm(request.POST)

        if pf.is_valid():
            print(pf.cleaned_data)
            Product.objects.create(**pf.cleaned_data)
        else:
            print(pf.errors)
    
    context = {
        "productform" : pf,
    }

    return render(request, "product_add.html", context)
Willem Van Onsem

You are working with a simple Form, not a ModelForm [Django-doc], so that means that it will not inspect the model at all. It will simply render a form. A ModelForm will inspect the model and construct a form based on that that you can customize.

class RawProductForm(forms.ModelForm):
    class Meta:
        model = Product
        labels = {
            'name': 'Name',
            'desc': 'Beschreibung',
            'price': 'Stückpreis',
            'sn': 'Seriennummer',
            'weightunit': 'Gewichteinheit',
            'weight': 'Gewicht',
            'image': 'Bild',
        }

A ModelForm also has a .save(…) method [Django-doc] which creates a model object based on the data in the form and saves it to the database.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Django ForeignKey field required despite blank=True and null=True

From Dev

SqlCommand returns null even tho there is data in the database

From Dev

Django: Not null constraint even though null=True?

From Dev

Django - ManyToManyField does not let me save, "This field is required" even though I select something

From Dev

Element is not stretching to fit the widow even tho it is set to 100% width

From Dev

Element is not stretching to fit the widow even tho it is set to 100% width

From Dev

Django TextField always is required, despite blank=True,Null=True

From Dev

Why can't I set my Django Model's USPhoneNumberField to null=True, blank=True?

From Dev

Django file field update causing error even though not required

From Dev

how to set mongo objectId field to null or empty or even delete the field?

From Dev

DbValidationError: The FieldName field is required, but i set that field as Allow Nulls in the database

From Dev

DbValidationError: The FieldName field is required, but i set that field as Allow Nulls in the database

From Dev

Validation: how to set a field that is not required to 'null' when input is empty

From Dev

Reading Avro file gives AvroTypeException: missing required field error (even though the new field is declared null in schema)

From Dev

Set html required if field is required

From Dev

Django ModelForm won't allow Null value for required field?

From Dev

Can't keep field empty even if it is set to null and blank

From Dev

Set reCaptcha field as required

From Dev

Set field required in acumatica

From Dev

ZipFile.isSplitArchive is returning false even though I set to true

From Dev

React Typescript - Math.random gives wrong result even tho min value and max value is set

From Dev

django field is required validators

From Dev

Django: Email field in form returning 'this field cannot be null/this field cannot be blank' even though there's an address in it

From Dev

How to set Many to Many field blank=True on both sides in django

From Dev

Symfony2: form field gets "required"even if is not set as it in form or Twig template

From Dev

Spring @Autowired(required = true) is null

From Dev

Spring @Autowired(required = true) is null

From Dev

Django form saying "this field is required" (even text fields) although it's been filled

From Dev

SQL field does not have a default value - Even though field is set to default NULL

Related Related

  1. 1

    Django ForeignKey field required despite blank=True and null=True

  2. 2

    SqlCommand returns null even tho there is data in the database

  3. 3

    Django: Not null constraint even though null=True?

  4. 4

    Django - ManyToManyField does not let me save, "This field is required" even though I select something

  5. 5

    Element is not stretching to fit the widow even tho it is set to 100% width

  6. 6

    Element is not stretching to fit the widow even tho it is set to 100% width

  7. 7

    Django TextField always is required, despite blank=True,Null=True

  8. 8

    Why can't I set my Django Model's USPhoneNumberField to null=True, blank=True?

  9. 9

    Django file field update causing error even though not required

  10. 10

    how to set mongo objectId field to null or empty or even delete the field?

  11. 11

    DbValidationError: The FieldName field is required, but i set that field as Allow Nulls in the database

  12. 12

    DbValidationError: The FieldName field is required, but i set that field as Allow Nulls in the database

  13. 13

    Validation: how to set a field that is not required to 'null' when input is empty

  14. 14

    Reading Avro file gives AvroTypeException: missing required field error (even though the new field is declared null in schema)

  15. 15

    Set html required if field is required

  16. 16

    Django ModelForm won't allow Null value for required field?

  17. 17

    Can't keep field empty even if it is set to null and blank

  18. 18

    Set reCaptcha field as required

  19. 19

    Set field required in acumatica

  20. 20

    ZipFile.isSplitArchive is returning false even though I set to true

  21. 21

    React Typescript - Math.random gives wrong result even tho min value and max value is set

  22. 22

    django field is required validators

  23. 23

    Django: Email field in form returning 'this field cannot be null/this field cannot be blank' even though there's an address in it

  24. 24

    How to set Many to Many field blank=True on both sides in django

  25. 25

    Symfony2: form field gets "required"even if is not set as it in form or Twig template

  26. 26

    Spring @Autowired(required = true) is null

  27. 27

    Spring @Autowired(required = true) is null

  28. 28

    Django form saying "this field is required" (even text fields) although it's been filled

  29. 29

    SQL field does not have a default value - Even though field is set to default NULL

HotTag

Archive