Disclaimer: I can wipe out the database anytime. So while answering this, please don’t care about migrations and stuff. Imagine me having a model with multiple values: Now I want to add a comment to be stored for every value of that model. For example I want to add a comment “measured in winter…
Tag: django-forms
django getting all objects from select
I also need the field (commentGroupDesc) from the foreign keys objects. models.py views.py Here I get the commentGroup fine but I also need commentGroupDesc to put into my form. Answer At first, it’s not a good thing to name same your model field as model name which is commentGroup kindly change field n…
Django IntegrityError (1048, “Column ‘item_id_id’ cannot be null”)
Here is my models.py Items model from different App: My views.py Forms.py My template And the form when i run the server is this: Answer In you model the field is named item_id but in your ModelForm the field is named itemID. The field names should be the same. Rename the fields in your ModelForm so they matc…
Django forms: not adding attributes
I’m aware that there is many post like this, but can you spot the typo or error in my code? I’m adding custom classes to my forms but when I inspect the element on the web page there are none. The same goes for placeholder. Code: Views.py: Template file: Forms.py: Answer I think, you already speci…
Django – CreateView specifying a form to use from forms.py
I’m trying to use a form I have defined in forms.py within my CreateView, as I have css classes and field labels I want to apply. Documentation states declaring a form_class field within your CreateView, but trying to set it as my ModelForm gives me the error TypeError ‘AssessmentForm’ objec…
Add multiple values to a django model’s attribute
I want to add an attribute to a django model as much as the user wants to. For example I want to add a few academic degrees with this code We can just add one degree and if a person has more, he or she can’t add them. I need a way to add as much degrees as i want
Saving related model objects in single view
I am beginner and I am working on Django project – risk assessment application. I have a trouble to achieve saving on related objects for my application risk record. I am using MultiModelForm to achieve the following. I am successfully creating Whatif instance and connecting it with GuideWordi instance …
Django is_active field is not changing
I’m using django 4.0 when i change the is_active to False from django admin, it doesn’t make any changes to the object, I have override the save method in models. models.py admin.py Can anyone advise ? The problem started when i added the save() in models.py Answer The .save() method should always…
reset form to initial data in invalid_form and display error in Django
I have a profile form that shows email, user name and first name. user only allowed to change first name field and the others are read only, if user change HTML value in email and username then submit it, it returns error but fill the fields with invalid value entered. I tried create a new instance of form an…
Django Rendering Form in HTML Issue
I’m attempting to render a form, in html and have tried the normal {{ form }}. However when I go to the site set up by: python manage.py runserver. I get the following where the form should go (highlighted section on screen-capture) webpage This is the code for the form in question. forms.py This is the…