modelform

How to make Required: boolean field in model Django

試著忘記壹切 提交于 2019-12-08 03:04:27
问题 I have a model with a field called in is_student and is_teacher Student and Teacher forms is_teacher = models.BooleanField('teacher status', default=False) is_student = models.BooleanField('student status', default=False) I want to make sure this field is: Always Checked by the user True *Required Currently: is_teacher in TeacherApplications Model When unchecked - it saved 0 to the form and continues. (Not good) When checked gives me this error: ValueError at /register/teacher invalid literal

How to make Required: boolean field in model Django

不问归期 提交于 2019-12-06 11:59:16
I have a model with a field called in is_student and is_teacher Student and Teacher forms is_teacher = models.BooleanField('teacher status', default=False) is_student = models.BooleanField('student status', default=False) I want to make sure this field is: Always Checked by the user True *Required Currently: is_teacher in TeacherApplications Model When unchecked - it saved 0 to the form and continues. (Not good) When checked gives me this error: ValueError at /register/teacher invalid literal for int() with base 10: '' Currently: is_student in StudentProfile Model When checked gives this error

Django custom validation in model form for imagefield (max file size etc.)

强颜欢笑 提交于 2019-12-06 07:24:02
问题 I have a modelform that has an imagefield called 'banner' and I am trying to validate the file size and dimesions and provide an error if the image is too large. Here is the models.py: class Server(models.Model): id = models.AutoField("ID", primary_key=True, editable=False) servername = models.CharField("Server Name", max_length=20) ip = models.CharField("IP Address", max_length=50) port = models.CharField("Port", max_length=5, default='25565') banner = models.ImageField("Banner", upload_to=

Django ModelMultipleChoiceField object has no attribute to_field_name

◇◆丶佛笑我妖孽 提交于 2019-12-06 06:31:53
问题 I'm trying to create a custom field for a ModelForm. I'm extending from ModelMultipleChoiceField and then overriding render and render_options, however, I keep getting this exception when just trying to import my form: AttributeError: 'ModelMultipleChoiceField' object has no attribute 'to_field_name' I'm not sure what I'm missing. I've tried even adding a to_field_name attribute to my new class, but that doesn't help. Here is my code: class MultiSelect(ModelMultipleChoiceField): def __init__

Uploading Profile Image using Django ModelForm

六月ゝ 毕业季﹏ 提交于 2019-12-06 03:17:51
问题 I've looked around at related questions, but none of the answers seem to work. I'm trying to upload a profile image for a user and have it replace (overwrite) the current image. Upon saving the image I want to change the filename to the user id. In it's current form the image will upload, but it won't replace the existing image (e.g. it'll be saved as 2_1.png). class PhotoForm(forms.ModelForm): def save(self): content_type = self.cleaned_data['photo'].content_type.split('/')[-1] filename = '

How can I exclude a declared field in ModelForm in form's subclass?

这一生的挚爱 提交于 2019-12-05 04:47:16
问题 In Django, I am trying to derive (subclass) a new form from ModelForm form where I would like to remove some fields (or to have only some fields, to be more correct). Of course obvious way would be to do (base form is from django.contrib.auth.forms ): class MyUserChangeForm(UserChangeForm): class Meta(UserChangeForm.Meta): fields = ('first_name', 'last_name', 'email') But this does not work as it adds/keeps also an username field in the resulting form. This field was declared explicitly in

Django ModelForm fails validation with no errors

大憨熊 提交于 2019-12-04 18:39:35
问题 Ok, I have been staring at this for hours trying to figure out what's going on, to no avail. I am trying to create a ModelForm using the 'instance' keyword to pass it an existing model instance and then save it. Here is the ModelForm (stripped considerably from the original in my attempts to identify the cause of this problem): class TempRuleFieldForm(ModelForm): class Meta: model = RuleField and here is the code I'm running: >>> m = RuleField.objects.get(pk=1) >>> f = TempRuleFieldForm

Django ModelMultipleChoiceField object has no attribute to_field_name

对着背影说爱祢 提交于 2019-12-04 13:45:01
I'm trying to create a custom field for a ModelForm. I'm extending from ModelMultipleChoiceField and then overriding render and render_options, however, I keep getting this exception when just trying to import my form: AttributeError: 'ModelMultipleChoiceField' object has no attribute 'to_field_name' I'm not sure what I'm missing. I've tried even adding a to_field_name attribute to my new class, but that doesn't help. Here is my code: class MultiSelect(ModelMultipleChoiceField): def __init__(self, queryset, cache_choices=False, required=True, widget=None, label=None, initial=None, help_text

Django custom validation in model form for imagefield (max file size etc.)

不想你离开。 提交于 2019-12-04 11:52:00
I have a modelform that has an imagefield called 'banner' and I am trying to validate the file size and dimesions and provide an error if the image is too large. Here is the models.py: class Server(models.Model): id = models.AutoField("ID", primary_key=True, editable=False) servername = models.CharField("Server Name", max_length=20) ip = models.CharField("IP Address", max_length=50) port = models.CharField("Port", max_length=5, default='25565') banner = models.ImageField("Banner", upload_to='banners', max_length=100) description = models.TextField("Description", blank=True, max_length=3000)

Django: Make certain fields in a ModelForm required=False

点点圈 提交于 2019-12-04 08:51:59
问题 How do I make certain fields in a ModelForm required=False? If I have: class ThatForm(ModelForm): class Meta: widgets = {"text": Textarea(required=False)} Or if I have: class ThatForm(ModelForm): text = Textarea(required=False) Django returns: __init__() got an unexpected keyword argument 'required' 回答1: following from comments. Probably yes: class ThatForm(ModelForm): def __init__(self, *args, **kwargs): # first call parent's constructor super(ThatForm, self).__init__(*args, **kwargs) #