Questions tagged [modelform]

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
32

Views

ModelForm __init__ being called a ridiculous amount of times

Just going to put some code and explain at the bottom. # modelforms.py # class MyModelModelForm(forms.ModelForm): def __init__(self, *args, **kwargs): self.request = kwargs.pop('request', None) super(MyModelModelForm, self).__init__(*args, **kwargs) print '__init__' class Meta: model = MyModel exclu...
zyeek
1

votes
0

answer
55

Views

Django Form object, change HTML attribute outside python code/models

I'm looking to understand a way to change attribute of my html code generate by the django's template system, especially for Form html code. So I found some stuff to change class from some models like that class AuthorForm(ModelForm): class Meta: model = Author fields = ('name', 'title', 'birth_dat...
rSim
1

votes
1

answer
54

Views

Unable to use data of one app in another in Django

I have three apps (Internship, UserProfile and Infrastructure) in my django project. I have made models Profile and StudentProject in UserProfile Model. The StudentProject Model contains two foreign key-> user and Lab (this model is defined in the Infrastructure model). In a template(details.html fi...
Nandit Mehra
1

votes
2

answer
210

Views

How to get help_text on a custom Django ModelChoiceField

I'm creating a custom ModelChoiceField so I can display custom labels for my foreign key, but in doing so Django no longer displays the help_text on the form. How can I get the help text back? models.py class Event(models.Model): title = models.CharField(max_length=120) category = models.ForeignKey...
43Tesseracts
1

votes
0

answer
22

Views

Testing DataField to accept only data in past. Clean(), Django, AssertionError

I am trying to write the test to check if my DataField accepts only data in the past but It doesn't work properly. forms.py class BookForm(PermissionRequiredMixin, ModelForm): class Meta: model = Book fields = ['title', 'author', 'summary', 'tag', 'genre', 'language', 'book_format', 'read_date'] def...
Daniel Milewski
1

votes
0

answer
36

Views

Django for loop overwrites saved modelform

The following code saves to database but the values inputted to the form2 overwrites values inputted to Form1 in database. However the Assumptions.Name is not overwritten and has both values Form1 and Form2. Also, if I refresh the page form is expanding and have more rows with previously saved value...
Martin
1

votes
2

answer
97

Views

Django ForeignKey Model Form

I'm newbie on Django. I have two model and one of this model have Foreign Key. I'm using Model Form in forms and when I fill the form my foreign key field return null. What I want is when I fill the form foreign key field, fill according to the pointed out by the foreign key. Models: class customerI...
Kartal
1

votes
1

answer
160

Views

django forms from two tables referencial integrity

i have a class named cv,and a class named university, and each user that completes his cv, should choose a University he studyes at. My problem is: one student can study at one or 2 or three universities, or may be a user that is not student. I need to take this data into a form, and i use ModelFor...
dana
1

votes
1

answer
228

Views

Problem with validating ModelForm

I use ModelForm to create my form. All works fine except 1 thing - validating the unique field. Code: class Article(models.Model): ... title = models.CharField(max_length=255, unique=True, error_messages={'max_length' : 'max translation', 'unique' : 'unique translation', 'required' : 'req translatio...
robos85
1

votes
1

answer
679

Views

exclude in ModelAdmin vs ModelForm

Is the exclude list in a ModelForm any different from an exclude list in a ModelAdmin? If my ModelForm is tied to the ModelAdmin, where do I need to specify the exclude list ideally; in the ModelForm Meta class or in the ModelAdmin subclass?
gamadeus
1

votes
1

answer
248

Views

How to display modelforms from dropdown menu django

In my app I'm trying to implement a feature that helps the user to insert certain 'frequent' data. # models.py class ExamSet(models.Model): set = models.CharField(max_length=40) class Exam(models.Model): examset = models.ForeignKey(ExamSet) value = models.CharField(max_length=40) # forms.py class E...
Flavio Marcato
1

votes
2

answer
356

Views

Django: List item.id as field attr?

In my template I have : {% for link in solution_list %} {{mark_solved_form.is_solution}} {% csrf_token %}...
user1462141
1

votes
1

answer
129

Views

Django ModelForm or Form

i am totally new to both Python and Django,so please excuse me if this question is a bit simpleton. I am writing a small app to track the scores of a Billiards match. I don't need to explain all the details, but the basic objects involved are: Team has Players Match is between two Teams (home and aw...
Paul Hermans
1

votes
1

answer
257

Views

Django not displaying template formatted time value

I have set up a model like so: class Reoccurring(models.Model): Username = models.ForeignKey(User, to_field='username') Description = models.CharField(max_length=100) Time = models.TimeField() DayOrDate = models.CharField(max_length=100) Interval = models.ForeignKey(IntervalChoices, to_field='choice...
ljrh
1

votes
1

answer
103

Views

NameError when overriding __init__ of ModelForm

I hope you can help me to figure out what's wrong with this code: from django.forms import ModelForm from models import Persona class PersonaForm(ModelForm): class Meta: model = Persona exclude = ('cuenta',) def __init__(self, *args, **kwargs): cuenta = kwargs.pop('cuenta') super(PersonaForm, self)....
aldo_vw
1

votes
1

answer
2.2k

Views

Save custom field in Django ModelForm

I am having trouble saving a custom field in a ModelForm. The field in question is a ModelChoiceField. I have added a save() method as shown in this question. However, when I use it I get an error: ImproperlyConfigured No URL to redirect to. Either provide a url or define a get_absolute_url metho...
ZG101
1

votes
1

answer
567

Views

How to submit django ModelForm from template

I am trying to create instances of class Message(models.Model): text = models.TextField() created = models.DateTimeField(auto_now_add=True) sender = models.ForeignKey(User, related_name='messages_sent') receiver = models.ForeignKey(User, related_name='messages_received') when user...
codyc4321
1

votes
2

answer
521

Views

How to get a Django Prepopulated Model Form for User to *not* populate password

I have a ModelForm for the User model that I want to use to allow the user to update some of their settings (first name, last name, email, and, of course, password). It's a very basic form and I did the UserForm(instance=user) so the user would see their current values. However, it's populating the...
akajb
1

votes
1

answer
88

Views

Django modelform will not save

I have made a form which allows users to create a job listing, I have declared all the fields in my model and have created a model form and a view. Adding things to the model through the admin panel works fine and the form displays perfectly on the website. Pressing the submit button also throws no...
eZ_Harry
1

votes
1

answer
58

Views

Django ModelForm Unittesting: Populate fields with custom data

I am trying to populate with data a model form with a custom field. However, the foreign key is throwing the error: Select a valid choice. That choice is not one of the available choices. I cannot understand what am I doing wrong: models.py: from django.db import models class Name(models.Model): fir...
raratiru
1

votes
1

answer
197

Views

Django model formset performance

I have little perfomance issue with Django 1.4.2 and PostgreSQL 9.1. I want to create model fromset with form created like this: forms.py class AcknowledgeForm(forms.ModelForm): class Meta: model = Attendance fields = ['acknowledge', ] widgets = {'acknowledge': forms.CheckboxInput()} AcknowledgeForm...
John Refuse
1

votes
1

answer
651

Views

Django/AJAX - Chained modelform selects - Fixing 'Select a Valid Choice' error without preloading of all possible choices

I have these models: class Subject(models.Model): title = models.CharField(max_length=255) class Teacher(models.Model): name = models.CharField(max_length=255) subjects = models.ManyToManyField(Subject) class Exam(models.Model): classroom = models.CharField(max_length=20) teacher = models.ForeignKey...
Луди Мачак
1

votes
1

answer
435

Views

create form from 2 models python django: [Errno 2] No such file or directory

I am trying to create a form from 2 models using python and django. Below shows my models: # Create your models here. class Action(models.Model): name = models.CharField('Action name', max_length=50) keywords = models.CharField('Keywords', max_length=50) object = models.CharField('Object', max_leng...
RebeccaRol
1

votes
2

answer
74

Views

ModelForm displays ForeignKey table values, not choices

I'm creating a Django app where each site has a single category, selected from a range of possible categories. So I have two models: Site, and Category. I want a many-to-one relationship between the Site and Category models. The Category table contains a selection of different categories, and each...
RubyNoob
1

votes
1

answer
565

Views

Django model form doesn't populate when instance=object is set

I'm trying to populate a ModelForm with existing data if it exists or create a new instance if not. I've read the django docs and several questions here on Stack Overflow but I can't figure out why my form doesn't populate with the existing data. I'm sure I'm missing something simple, any help would...
jm.gunter
1

votes
1

answer
1.4k

Views

HTML Select dropdown with dynamic list in Django ModelForm

In the view : I need to display a dropdown list of years starting from a fixed value (say 2005) to the current year. Now, this list, though dynamic, does not come from the database. Also, I want the list to expand as time goes by. In the model : I want this field to be like an IntegerField(?) which...
kshubham07
1

votes
1

answer
136

Views

ModelForm in Django - select is not populating with appropriate data?

I've got a model form in Django that displays nicely - but it's not pulling the appropriate information. The select dropdowns appear, but are not populated with the options and I am struggling to figure out why. My models look like this: class Mileage(models.Model): start_location = models.ForeignKe...
Hanny
1

votes
2

answer
230

Views

ModelForm disable imageField if empty

I have a form that allows users to upload an image. If they don't upload an image a default image is displayed. I want it that if they don't upload an image, the image field is disabled. I thought the if statement at the end of the form would work - but it didn't. Here's the form forms.py: class...
Steve
1

votes
1

answer
427

Views

Django modelformset_factory deleting objects marked by can_delete

Hello I have got question I have made modelformset_factory and in options I have choosen can_delete = True and now I don't know how to delete marked objects as 'DELETE' : True to delete them from database. I was trying to do this in some ways and it didnt work. I was looking for it also in django fo...
Ppyyt
1

votes
1

answer
381

Views

Django: ModelForm not binding to request.POST

I have the following model: class Tracking(models.Model): person = models.ForeignKey(Person, on_delete=models.CASCADE) week = models.IntegerField(choices=settings.WEEK_CHOICES, blank=True) term = models.IntegerField(choices=settings.TERM_CHOICES, blank=True) year = models.IntegerField(choices=settin...
Sam
1

votes
1

answer
1.1k

Views

How to make Required: boolean field in model Django

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...
Shaz
1

votes
2

answer
22

Views

Django Form Does Not Find Right ID int Increment on Production vs LH

For some reason on Localhost (LH) everything works fine but on my Production server, my form does not add a new user submission properly. The error I am getting is: duplicate key value violates unique constraint '...' DETAIL: Key (id)=(8) already exists. Is there some sort of production 'sudo syste...
Alex Winkler
1

votes
1

answer
43

Views

Display a different default value in Django CreateView according to current logged in user?

Long story short, I am creating a ModelForm which will be passed to a generic CreateView. This form will allow a user to post an event with a location. I have already collected the user's base address in my user creation form, and my Event model has a foreignkey to the author of the event. I would l...
cmanbst
1

votes
2

answer
47

Views

Django, Best way to add data to Foreignkey field with ModelForm

I am playing around with django, ModelForms and i created PostModel and CommentModel with a foreign key. That is all post have a comment box which allow users to comment,here is my view which works perfectly i just want to know the best way i could have added the data to comment field def comment(r...
ade desmond
1

votes
1

answer
105

Views

Django formset with no initial data

I am a Django rookie and I am developing a small app to register time (duration) and quantity of activities per user per day. Sort of like a work log. My problem is this: My “add entry” view displays and updates old records rather than adding new records to the db. I need a view to add new recor...
Stigster
1

votes
1

answer
38

Views

AttributeError: 'PasswordReset' object has no attribute 'cleaned_data'

Model: class UserInfo(AbstractBaseUser, PermissionsMixin): username = models.EmailField(_('邮件'), max_length=50, default='', unique=True) first_name = models.CharField(_('姓'), max_length=50, default='', blank=True, null=False) last_name = models.CharField(_('名'), max_length=50, default='')...
james yao
1

votes
1

answer
0

Views

Filling a choice field with objects from a query

I have a ModelForm, which I'm trying to have a dynamic select in it. My ModelForm in forms.py: class AuxiForm(forms.ModelForm): class Meta: model = Auxi fields = ['tipAux'] widgets = { 'tipAux': forms.Select(attrs={'class': 'form-control', 'placeholder': 'Tipo'}), } labels = { 'tipAux': 'Tipo', } an...
Gonzalo D'ambra
1

votes
1

answer
0

Views

Django hidden input field problems

I have following model form where I have to add a hidden field. class AddEditGroupForm(forms.ModelForm): id_sel_comp = forms.CharField( label='selected company', initial=0, required=True, widget=forms.HiddenInput(attrs={'id': 'id_sel_comp'}) ) class Meta: model = Group fields = ('name', 'id_sel_comp...
cwhisperer
1

votes
1

answer
0

Views

Django ModelForm with ModelChoiceField and Query Filter

I need to create a list of cases in dropdown list from data passed into the form for the individual firm. I pass the id into the form, but how do I use it in the CaseChoiceField query? Just using id or self.id in the query does not work. First in the view: form = NewProjectCaseForm ( id = f.id ) The...
diogenes

View additional questions