modelform

Django ModelForm fails validation with no errors

萝らか妹 提交于 2019-12-03 12:05:57
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(instance=m) >>> f.is_valid() False The model object ( m above) is valid and it saves just fine, but the form

Django ModelForm Imagefield Upload

做~自己de王妃 提交于 2019-12-03 07:01:32
问题 I am pretty new to Django and I met a problem in handling image upload using ModelForm. My model is as following: class Project(models.Model): name = models.CharField(max_length=100) description = models.CharField(max_length=2000) startDate = models.DateField(auto_now_add=True) photo = models.ImageField(upload_to="projectimg/", null=True, blank=True) And the modelform is as following: class AddProjectForm(ModelForm): class Meta: model = Project widgets = { 'description': Textarea(attrs={'cols

Django: Make certain fields in a ModelForm required=False

梦想的初衷 提交于 2019-12-03 04:24:27
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' following from comments. Probably yes: class ThatForm(ModelForm): def __init__(self, *args, **kwargs): # first call parent's constructor super(ThatForm, self).__init__(*args, **kwargs) # there's a `fields` property now self.fields['desired_field_name'].required = False you ought to add blank=True

Django, adding excluded properties to the submitted modelform

拟墨画扇 提交于 2019-12-03 02:36:17
I've a modelform and I excluded two fields, the create_date and the created_by fields. Now I get the "Not Null" error when using the save() method because the created_by is empty. I've tried to add the user id to the form before the save() method like this: form.cleaned_data['created_by'] = 1 and form.cleaned_data['created_by_id'] = 1 . But none of this works. Can someone explain to me how I can 'add' additional stuff to the submitted modelform so that it will save? class Location(models.Model): name = models.CharField(max_length = 100) created_by = models.ForeignKey(User) create_date = models

django: taking input and showing output in the same page

我的未来我决定 提交于 2019-12-02 18:34:27
I am quite new to django and struggling to do something very simple. I have a ModelForm for the following model: class Queries(models.Model): user_id=models.CharField(max_length=200) query=models.CharField(max_length=200) And I am showing the user a simple form that will help in doing the following: user will ask a question The question will be processed(a database query will be generated based on the question) Then the query result should be shown just beneath the form in the same page. This is how my views.py looks like: from django.http import HttpResponse from django.shortcuts import get

Add ModelForm Fields as Attribute to Object

不羁的心 提交于 2019-12-02 16:46:41
问题 I have a ModelForm for my Risk set up as: class RiskForm(forms.ModelForm): class Meta: model = Risk fields = '__all__' def __init__(self, *args, **kwargs): progid = kwargs.pop('progid') super(RiskForm, self).__init__(*args,**kwargs) dict_of_fields = {} all_char = Program.objects.get(id=progid).char_set.all() for char in all_char: c = [] for cat in char.cat_set.all(): c.append( (cat.label, cat.label) ) dict_of_fields[char.label] = c self.fields[char.label] = forms.ChoiceField(c) Where the Risk

How to handle the validation of the model form when the model has a clean method if the model form excluded some fields?

前提是你 提交于 2019-12-02 13:26:48
问题 I have this model: class IeltsExam(Model): student = OneToOneField(Student, on_delete=CASCADE) has_taken_exam = BooleanField(default=False,) listening = FloatField(choices=SCORE_CHOICES, null=True, blank=True, ) reading = FloatField(choices=SCORE_CHOICES, null=True, blank=True, ) exam_date = DateField(null=True, blank=True, ) non_empty_fields = \ { 'listening': 'please enter your listening score', 'reading': 'please enter your reading score', 'exam_date': 'please specify your exam date', }

Add ModelForm Fields as Attribute to Object

拈花ヽ惹草 提交于 2019-12-02 08:27:33
I have a ModelForm for my Risk set up as: class RiskForm(forms.ModelForm): class Meta: model = Risk fields = '__all__' def __init__(self, *args, **kwargs): progid = kwargs.pop('progid') super(RiskForm, self).__init__(*args,**kwargs) dict_of_fields = {} all_char = Program.objects.get(id=progid).char_set.all() for char in all_char: c = [] for cat in char.cat_set.all(): c.append( (cat.label, cat.label) ) dict_of_fields[char.label] = c self.fields[char.label] = forms.ChoiceField(c) Where the Risk Object is defined as: class Risk(models.Model): program = models.ForeignKey(Program, on_delete=models

How to handle the validation of the model form when the model has a clean method if the model form excluded some fields?

橙三吉。 提交于 2019-12-02 05:47:13
I have this model: class IeltsExam(Model): student = OneToOneField(Student, on_delete=CASCADE) has_taken_exam = BooleanField(default=False,) listening = FloatField(choices=SCORE_CHOICES, null=True, blank=True, ) reading = FloatField(choices=SCORE_CHOICES, null=True, blank=True, ) exam_date = DateField(null=True, blank=True, ) non_empty_fields = \ { 'listening': 'please enter your listening score', 'reading': 'please enter your reading score', 'exam_date': 'please specify your exam date', } def clean(self): errors = {} if self.has_taken_exam: for field_name, field_error in self.non_empty_fields

Django model form save (multi database) question

不羁的心 提交于 2019-12-01 14:43:55
another noob Django question. The below both work for me, is there any difference or anything I should be aware of ? I'm using Django 1.2.5. Thanks. o = Staff() form = StaffForm(request.POST, instance=o) if form.is_valid(): o.save(using='dbName') o = Staff() form = StaffForm(request.POST, instance=o) if form.is_valid(): f = form.save(commit = False) f.save(using='dbName') The first example does not work - it doesn't update the instance from the form. Use the second. 来源: https://stackoverflow.com/questions/6118884/django-model-form-save-multi-database-question