How to pass form field without filling in Django? And add extra field to for answer?

Viewed 27

I wanna make patient information register form. I did model.py and form.py but, when i come to use, i couldn't pass the form without filling. Forexample, in this image evlilik tarihi means marriage date, evlilik tarihi 2 means second marriage date, when i filling the form i want to leave empty the evlilik tarihi 2 field. But always i took this error "TypeError: init() got an unexpected keyword argument 'blank'" I used null=True, blank=True, but nothing. How can i pass the form field without filling.

And second question is, i made three form field for multiple marriage but is there any way how can i add button to this marriage field, and someone had second or third marriage, he can easily add button and give marriage date to me. How can i do this?

Form.py

class HastaBilgiForm(forms.ModelForm):
    class Meta: 
        model = HastaBilgi
        fields = '__all__'

        widgets = {
            'hasta_ad_soyad' : forms.TextInput(attrs={'class': 'form-control'}),
            'hasta_dogum_tarihi' : forms.TextInput(attrs={'class': 'form-control'}), 
            'hasta_dogum_yeri' : forms.TextInput(attrs={'class': 'form-control'}),
            'evlilik_tarihi_1' : forms.TextInput(attrs={'class': 'form-control'}),
            'evlilik_tarihi_2' : forms.TextInput(attrs={'class': 'form-control'}),
            'evlilik_tarihi_3' : forms.TextInput(attrs={'class': 'form-control'}),
            'bosanma_tarihi_1' : forms.TextInput(attrs={'class': 'form-control'}),
            'bosanma_tarihi_2' : forms.TextInput(attrs={'class': 'form-control'}),
            'bosanma_tarihi_3' : forms.TextInput(attrs={'class': 'form-control'}),
            'ogrenim_durumu' : forms.Select(choices=choices2, attrs={'class': 'form-control'}),
            'meslek' : forms.TextInput(attrs={'class': 'form-control'}),
            'kan_grubu' : forms.Select(choices=choices1, attrs={'class': 'form-control'}),
            'boy' : forms.TextInput(attrs={'class': 'form-control'}),
            'kilo' : forms.TextInput(attrs={'class': 'form-control'}),
            'max_kilo' : forms.TextInput(attrs={'class': 'form-control'}),
            'max_kilo_tarih' : forms.TextInput(attrs={'class': 'form-control'}),
            'nereden_geldi' : forms.Textarea(attrs={'class': 'form-control'}),
            'onceki_homeopati_tedavisi' : forms.Textarea(attrs={'class': 'form-control'}),
            'gelis_sikayeti' : forms.Textarea(attrs={'class': 'form-control'}),

        
        }  

Model.py

class HastaBilgi(models.Model):
    hasta_ad_soyad = models.CharField(max_length=255)
    hasta_dogum_tarihi = models.CharField(max_length=255)
    hasta_dogum_yeri = models.CharField(max_length=255)
    evlilik_tarihi_1 = models.CharField(max_length=255)
    evlilik_tarihi_2 = models.DateField()
    evlilik_tarihi_3 = models.DateField()
    bosanma_tarihi_1 = models.DateField()
    bosanma_tarihi_2 = models.DateField()
    bosanma_tarihi_3 = models.DateField()
    ogrenim_durumu = models.CharField(max_length=255)
    meslek = models.CharField(max_length=255)
    kan_grubu = models.CharField(max_length=255)
    boy = models.CharField(max_length=255)
    kilo = models.CharField(max_length=255)
    max_kilo = models.CharField(max_length=255)
    max_kilo_tarih = models.CharField(max_length=255)
    nereden_geldi = models.CharField(max_length=255)
    onceki_homeopati_tedavisi = models.CharField(max_length=255)
    gelis_sikayeti = models.CharField(max_length=255)

    def __str__(self):
        return self.hasta_ad_soyad 

   

enter image description here

2 Answers

It sounds like you need to update your model.

evlilik_tarihi_2 = models.DateField(null=True, blank=True)

etc. When you have made those changes you will need to run makemigrations and migrate.

You should allow all fields from "red box" from image to be blank and nullable. In your case:

class HastaBilgi(models.Model):
    ... # some staff before
    evlilik_tarihi_1 = models.CharField(max_length=255, null=True, blank=True)
    evlilik_tarihi_2 = models.DateField(null=True, blank=True)
    evlilik_tarihi_3 = models.DateField(null=True, blank=True)
    bosanma_tarihi_1 = models.DateField(null=True, blank=True)
    bosanma_tarihi_2 = models.DateField(null=True, blank=True)
    ... # some staff after

don't forget to run this command in command line:

python manage.py makemigrations

and after it:

python manage.py migrate

By the way, you mix in evlilik_tarihi data and text fields. It can be really confusing after some time, especially if you override it to text-widget instead of data-widget.

Related