Pole Django modelform nie jest wymagane
Mam taką formę:
class My_Form(ModelForm):
class Meta:
model = My_Class
fields = ('first_name', 'last_name' , 'address')
Jak mogę umieścić pole adresu jako opcjonalne?
7 answers
Twój model wygląda tak:
class My_Class(models.Model):
address = models.CharField()
Twój formularz dla wersji Django
class My_Form(ModelForm):
address = forms.CharField(required=False)
class Meta:
model = My_Class
fields = ('first_name', 'last_name' , 'address')
Twój formularz dla wersji Django > 1.8:
class My_Form(ModelForm):
address = forms.CharField(blank=True)
class Meta:
model = My_Class
fields = ('first_name', 'last_name' , 'address')
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-08-14 07:36:08
class My_Form(forms.ModelForm):
class Meta:
model = My_Class
fields = ('first_name', 'last_name' , 'address')
def __init__(self, *args, **kwargs):
super(My_Form, self).__init__(*args, **kwargs)
self.fields['address'].required = False
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-22 18:54:43
field = models.CharField(max_length=9, default='', blank=True)
Po prostu dodaj blank=True
w polu modelu i nie będzie to wymagane, gdy używasz formularzy modelowych.
Źródło: https://docs.djangoproject.com/en/3.1/topics/forms/modelforms/#field-types
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-10-29 12:23:36
Trzeba by dodać:
address = forms.CharField(required=False)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-03-26 03:31:04
Rozwiązanie@Anentropic z komentarza odpowiedź@Atma zadziałało dla mnie. I myślę, że jest najlepszy.
Jego komentarz:
Null=True, blank = True spowoduje, że pole ModelForm będzie wymagane = False
Ustawiłem go na moim wielotomowym polu w klasie UserProfile
i zadziałał bez zarzutu.
Moja UserProfile
klasa wygląda teraz tak (zwróć uwagę na pole friends
):
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
friends = models.ManyToManyField('self', null=True, blank=True)
Uważam też, że jest to najpiękniejsze rozwiązanie ponieważ robisz to samo, umieścić null
i blank
na True
, pogoda masz proste char
pole lub, jak ja, ManyToMany
pole.
Jeszcze raz wielkie dzięki @Anentropic. :)
P. S. Napisałem to jako post, ponieważ nie mogłem skomentować (mam mniej niż 50 reputacji), ale także dlatego, że myślę, że jego komentarz wymaga większej ekspozycji.
P. P. S. Jeśli ta odpowiedź ci pomogła, proszę o poprawienie jego komentarza.
Pozdrawiam :) [12]}Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-10 10:12:47
Rozwiązanie:
użyj obu blank=True
, null=True
.
my_field = models.PositiveIntegerField(blank=True, null=True)
Wyjaśnienie:
Jeśli używasz null=True
`my_field = models.PositiveIntegerField(null=True)`
Wtedy my_field jest wymagane, z * przeciwko niemu w postaci, nie można podać pustej wartości.
Jeśli używasz blank=True
`my_field = models.PositiveIntegerField(blank=True)`
Wtedy my_field nie jest wymagane, nie * przeciwko niemu w formie, nie można podać wartości. Ale dostanie pole null niedozwolone.
Uwaga:
1) marking as not required and
2) allowing null field are two different things.
Pro Tip:
Read the error more carefully than documentation.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-06-27 09:53:45
Powyższe odpowiedzi są poprawne; należy jednak pamiętać, że ustawienie null=True
Na ManyToManyField nie ma wpływu na poziom bazy danych i wywoła następujące ostrzeżenie podczas migracji:
(fields.W340) null has no effect on ManyToManyField.
Dobra odpowiedź na to jest wyjaśniona w tym drugim wątku .
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-09-05 10:15:08