Questions tagged [django-forms]

1

votes
2

answer
441

Views

How To Edit ManyToManyField In Django Admin list Display Page?

I want to add Many2Many fields editable in Django admin list_display page. The model structure as below, class Genre(models.Model): name = models.CharField(max_length=250, unique=True) class Movie(models.Model): name = models.CharField(max_length=250) genre = models.ManyToManyField(Genre) And I tri...
1

votes
3

answer
4.4k

Views

Set attribute of a model to a certain value before saving

I have this: class OrderForm(ModelForm): class Meta: model = Order exclude = ('number',) def __init__(self, *args, **kwargs): super(OrderForm, self).__init__(*args, **kwargs) if self.initial.get('account', None): self.fields['customer'].queryset = Customer.objects.filter(account=self.initial.get('ac...
orokusaki
0

votes
0

answer
2

Views

how to override default Django form validation error

I try this but it doesn't work. default error message is shown only.please help from django import forms from SharedApps_Application.models import certificateDb from django.contrib.admin.widgets import AdminDateWidget from django.forms.fields import DateField my_default_errors = { 'required': 'Appl...
A500010
0

votes
0

answer
6

Views

How to autofill different values in multiple forms with form factory

I'm working on a salary app with django. In my page I replicate a form as much as Instructors. Like that every day I can choose an Instructor and chose his activity. It's working but I'm lazy so I don't want to select an instructor in each form on the page. I would like to display one form per instr...
Tartempion34
0

votes
0

answer
21

Views

Reduce number of queries made to render list of items with same in-line editable field

While rendering a table of items, if the value for field X is not defined, it is rendered as a select element. Django makes a query for each select element and these can add up and cause delays in large tables. What is the best way to reduce the number of queries? views.py from rest_framework impor...
silverstripes
1

votes
2

answer
464

Views

Where/how to replace default upload handler in Django CBV?

I am trying to specify a specific method of handling file uploads for a class based view. Per the docs this can be achieved by something like: from django.core.files.uploadhandler import TemporaryFileUploadHandler request.upload_handlers = [TemporaryFileUploadHandler(request=request)] If i specify t...
BartDur
1

votes
1

answer
41

Views

My Django form is not rendering when I use Bootstrap's modal

I have some trouble with rendering a Django form in a modal. I suspect it is because I need some Ajax to get the url in the browser but I don't know how. Form: class TrackedWebsitesForm(forms.ModelForm): class Meta: model = TrackedWebsites fields = '__all__' View: def web(request): if request.method...
Raf Rasenberg
1

votes
2

answer
27

Views

Django how to get the users who are not in many2many field?

'models.py' class StaffProfile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='user_profile') staff_user = models.ManyToManyField(User, null=True, blank=True, related_name='staff_user') mobile_number = models.CharField(max_length=14, validators=[RegexValidato...
1

votes
1

answer
58

Views

How can I get the current user's group in forms.py in Django?

I have a scenario where i need to pass the logged-in user's groud name and get the list users in that group. forms.py -- in the below code i need to pass the user's group instead of Banglore class UpateTaskMaster(forms.ModelForm): def __init__(self, *args, **kwargs): super(UpateTaskMaster, self).__i...
Mohamed Naveen
1

votes
2

answer
166

Views

I am having errors with Django authenticated built-in function

I have been woking in django for a while. Now i am facing some problems with built-in function in django. The error state that TypeError: 'bool' object is not callable. This kind of error happened because of statement 'print(request.user.is_authenticated())'. Here below is source code: def login_pa...
I Love Coding
1

votes
0

answer
87

Views

How to use formatvalue filter for field in form template?

In Django templates, we use the filter on value fields such as {{value | formatvalue}} I want to generate my form dynamically like the example below. {% for field in form %} {{ field }} {% endfor %} As you know, the field attribute will be generated as an html element. Is there a way to add a filter...
Harun ERGUL
1

votes
0

answer
49

Views

How to re-increment each id attribute in Formset after deleting a Form in Django?

I currently have a formset rendered to a template, where I do a for loop over it to display each form. I have a button which adds extra forms and I also have a delete button which removes any of the forms. My problem is when I delete one of the middle forms, the incrementing from the formset managem...
David Arce
1

votes
0

answer
171

Views

Django Form post method returning error

I'm making a single page chat app which will list all the message I post. The thing is in order to make the list and the form appear in single page, I have to define a single view(from what I understand. I'm pretty new to Django.) that point to a url. My django files are like this: chat/urls.py fro...
user8703143
1

votes
0

answer
28

Views

Django: How to add a foreignkey to a dependent form, to a multi-form submit?

Forgive the logic of the table structure below example. It only meant as a simple example of the situation that I am. So the situation is that I want to make an employee form page, where the department and line manager might or might not exist already. So I replace the drop-down box with the form fi...
TheSadPrinter
1

votes
0

answer
245

Views

Using multiselect widget for a ForeignKey field Django

Good day i seem to be lost in how i want to implement something. so here is how it goes ive got a carowner model class carowner(models.Model): ..... carowner = models.CharField(primary_key=True) and a cars model class car(models.Model): .... carowner_id = models.ForeignKey(carowner) it has a forei...
Blue
1

votes
1

answer
27

Views

Widget attribute does not work

I have two forms and a formset. I have given __init__ for both the forms. However in my template only my POBodyForm is rendered with the attributes. Looks like __init__ of POHeaderForm is not working. I am not sure what is the error. Can someone please advise? I have been using the similar __init__...
Thaha
1

votes
0

answer
148

Views

“AnonymousUser” Error When Non-Admin Users Log In - Django

When I try to login users registered through my AbstractBaseUser model I get the error: 'AnonymousUser' object has no attribute '_meta' Which highlights the code: login(request, user) However, if the user is an admin there is no problem, leaving me to believe that the problem isn't with the 'login_...
Will Dpard
1

votes
1

answer
157

Views

Django Multiple Record Insert CreateView ( not enough values to unpack (expected 2, got 1))

i am getting the following message while saving the record. not enough values to unpack (expected 2, got 1) I am getting a Acquisition ID and attaching qoutations(single or multiple ) and once saving the record the system pops out the error. there is no error if i select the ACQID from createview...
Najeebullah Usmani
1

votes
0

answer
47

Views

django create N number of forms in template

I was wondering if it was possible to write a more generic form to use as a template so that during the render it will create as many forms as necessary? If not how would one write code to create N number of forms, where the number is Dependant on the result of some query set, and also the options i...
noone392
1

votes
0

answer
27

Views

Djaongo: ModelChoiceField and Widget - Could not display non-foreign key field

I want to create a select field that displays a list of records from a column that is not a foreign key. I am able to do it using widget=ForeignKeyRawIdWidget(rel=Slum._meta.get_field('electoral_ward').rel where 'electoral_ward' is a foreign key in 'Slum' model. But, I wish to have the same behavi...
Abhijit Ghate
1

votes
1

answer
133

Views

Form validation error not showing up in template

I was creating a form to register users, I added the validation error to check if the email already exists in the database. However, when you feed in an email which is already in the database, the form doesn't show up the error, it just resets. forms.py: class SignUpForm(UserCreationForm): institute...
Karan Sehgal
1

votes
0

answer
236

Views

How to Upload a text file in django using templateview based view and model based forms

I am new to django and stuck on uploading a simple file. I am using TemplateView as views and model based form. I am stuck here. I have no idea what to do. I am using django 2.0. I have attached the code what I have done so far. setting.py import os BASE_DIR = os.path.dirname(os.path.dirname(os.path...
Arshad
1

votes
0

answer
35

Views

Django-floppyforms - validation and sending do not work

I have problem with django-floppyforms. On the page, the form appears as it should - it is a dedicated front-end. However, the validation and sending of the form does not work. I do not know why. My form with django-floppyforms: from events import models import floppyforms.__future__ as forms class...
user9192656
1

votes
0

answer
166

Views

Creating a multiple choice form for each primary key in model with Django

I am trying to create a class registration app in Django. To simplify my problem, I have a model for students, blocks (time periods), and classes (lessons). There are multiple classes for each block, and students must register in exactly 1 class for each block. There will be some customization allow...
ialm
1

votes
0

answer
66

Views

Django admin userlinks is not showing up

I extended base_site.html template and it's working correctly. {% extends 'admin/base.html' %} {% load i18n %} {% block title %} {% trans 'Test Admin' %} {% endblock %} {% block userlinks %} {{ block.super }} {% endblock %} {% block nav-global %}{% endblock %} And now I've created a template for my...
Dark
1

votes
1

answer
91

Views

Copy ManyToMany Value

I am currently trying to copy a many-to-many-field from one model to another but running into a bit of trouble. I have been able to create the model fine with a many-to-many-field with a ModelMultipleChoiceField, and the model saves the way I want it to. When I try to copy it to another model, I d...
Steve Smith
1

votes
0

answer
150

Views

Saving Many-To-Many Field with self-reference in django-admin

I am trying to save a many-to-many field with self reference through django-admin. This is my model: class QuestionSetCategory(models.Model): parent_categories = models.ManyToManyField('self', blank=True) When I save the relation with django-admin, it outputs the following error: '' needs to have a...
Dominik Spiertz
1

votes
0

answer
272

Views

Override default Django field with custom widget

I'm using Django 1.11.9 on Ubuntu, with an app structure as such: VisitorTrackingApp | ---> admin.py ---> forms.py ---> models.py ---> static | ---> textboxes.css ---> templates | ---> index.html ---> signin.html ---> views.py ---> widgets.py In my models.py I've defined a model Visitors with an att...
Pat Jones
1

votes
0

answer
23

Views

Django : changing a specific form in modelformset

Here is the riddle. This is a simplified version of the problem I am dealing with, just so we can capture the core problem. So, for the sake of simplicity and relevance, not all fields and relationships are shown here. So, I have a modelformset and I would like to access each individual form to chan...
EarlyCoder
1

votes
1

answer
531

Views

Django ModelForm with ManyToManyField showing all possible objects

I need your help with this. I tried to find the solution for a couple of days, but no luck so far. Here is the set up: There can be several projects, tasks and people can be assigned to projects, a person can be assigned to several tasks, and the same task can be assigned to different people. No pr...
Peter B
1

votes
0

answer
36

Views

Many To Many Field Multiple Users

I'm slowly gaining my knowledge with Django, but this one has me stumped. I am trying to create a form that multiple users need to approve. In doing this, I have created a CreateView with a modelform and that works fine. I have also created an UpdateView, and this works fine as well, with the cav...
Steve Smith
1

votes
0

answer
114

Views

How RelatedFieldWidgetWrapper (django admin) loads the created content

I'm building a webpage that use ModelForm with ManyToMany relations. The Admin of django has the useful RelatedFieldWidgetWrapper. It creates a + next to relationfields that, when pressed, opens a popup to create a related-object. Once the creation is made the main webpage (the one where we press th...
EsseTi
1

votes
1

answer
107

Views

Save filled form data in Django if user is not logged in?

I search Django-way to do some non tipical feature (I think). My env is Django 2.0.2, PostgreSQL 9.6 and Python 3.6.4. So, I have model and form like: # ./app/models.py from users.models import User # custom user model class SubscribeModel(models.Model): user = models.ForeignKey(User, on_delete=mode...
Vic Shóstak
1

votes
1

answer
27

Views

Cycle thru a ModelForm fields and identify FileField and ImageField

I'm creating a generic save method for a form. In the save I want to iterate over the field and if the field is an instance of FileField to to some operations def save(self, commit=True): obj = super().save(commit=False) for field in self.fields: if isinstance(field, forms.FileField) and obj: This i...
user3541631
1

votes
0

answer
203

Views

MultiValueDictKeyError when submitting ModelFormSet

When trying to submit my ModelFormSet I am getting a MultiValueDictKeyError. The error message is not very descriptive so I'm not sure why the error is being thrown. Here is the view: def admin_tools(request): ElectionFormSet = modelformset_factory(Election, exclude=('Complete',), formset=BaseElecti...
cbuch1800
1

votes
0

answer
155

Views

How to raise a custom form invalid error in wizardview from django formtools

I am using django-formtools to split the registration form. I have added Google ReCaptcha for captcha verification. I would wish to produce error in the form like the one in django UpdateView. Is there any method in django form wizard like messages.error(self.request, 'Invalid reCAPTCHA. Please try...
Sudheer K
1

votes
1

answer
69

Views

Am I able to create a form that is able to add a new django.contrib.auth User without logging in to the admin panel?

Have created a form but unsure if is right and also unable to add a user, it will show TypeError/ This is how the form I want it to look like The following is my coding: class Form_Add_User(forms.Form): name=forms.CharField(label='Name', max_length=50) dateofbirth=forms.DateField(label='Date of Bi...
Snow
1

votes
0

answer
168

Views

trying to delete inactive users in django

I am writing a site that sends confirmation emails on signup. Until the user clicks the link, the is_active field is set to false. How can I automatically delete any users who do not click the activation link within a certain period of time? Is this even a viable option or is there a better way to w...
Austin Sutton
1

votes
0

answer
75

Views

How can I output formset as a result of submition of another form?

I am trying to output formset as a result of another form submission on the same page. This formset contains forms that should be submitted too. Here is my func view: def manage_articles(request): dyn_form = type('DynForm', (forms.BaseForm,), {'base_fields': {'title': forms.CharField(), 'name': for...
Aleksandr Baranov
1

votes
1

answer
240

Views

django-import-export form <input> not rendering in custom admin template w/ grappelli

after implementing django-import-export on a project using django-grappelli for admin UI, I've discovered there are some CSS incompatibilities between the two libs: https://github.com/sehmaschine/django-grappelli/issues/435 to address this, I created custom admin template overwrites so import-export...
Chris B.

View additional questions