Django: creating dynamic forms - python

I have these models and I want to build forms based on the data:
class Location(models.Model):
name=models.CharField(max_length=255)
location_id=models.CharField(max_length=255)
organization=models.CharField(max_length=255)
def __unicode__(self):
return self.name
class Beverage(models.Model):
name=models.CharField(max_length=255)
location=models.ForeignKey(Location)
fill_to_standard=models.IntegerField(max_length=10)
order_when_below=models.IntegerField(max_length=10)
def __unicode__(self):
return self.name
class Inventory(models.Model):
location=models.ForeignKey(Location)
beverage=models.ForeignKey(Beverage)
units_reported=models.IntegerField(max_length=10)
timestamp=models.DateTimeField(auto_now=True)
Here is what I want to happen, when a user goes to update inventory for a particular location, I want them to get a form that lists out all the possible beverages for that location (the beverages differ per location) and then create an Inventory form for it that will create a new line in the Inventory table for each Beverage for that Location. Each one needs a timestamp so we can have a history. I think I get that I need formsets, but I have not had any success figuring out how to implement them for this. So, this is kind of a two-parter:
Is my model design right for this problem?
How can I make forms that depend on the number of beverages to build themselves?

Question 1: model design - I would drop the location field from the Inventory model, since it is already in the Beverage model, and is redundant. Otherwise, they look good to me.
Question 2: formsets...
forms.py
from django import forms
from my_project.my_app.models import Beverage
class InventoryForm(forms.ModelForm):
units_reported = forms.IntegerField()
class Meta:
model = Beverage
fields = ('name', 'id')
views.py
from django.forms.models import modelformset_factory
from my_project.my_app.models import Beverage, Inventory
def update_inventory(request, location_id):
InventoryFormSet = modelformset_factory(Beverage, form=InventoryForm)
qs = Beverage.objects.filter(location=location_id)
formset = InventoryFormSet(queryset=qs)
if request.method == 'POST':
formset = InventoryFormSet(request.POST)
if formset.is_valid():
for form in formset:
beverage = form.save(commit=False)
units_reported = form.cleaned_data['units_reported']
Inventory(beverage=beverage, units_reported=units_reported).save()
...
modelformset_factory will create a form for every object (Beverage) in the queryset which is filtered by the desired location, and has an extra field in the customized ModelForm to report units.

Related

Django form not populating with POST data

SOLUTION AT THE BOTTOM
Problem: Django form populating with list of objects rather than values
Summary: I have 2 models Entities and Breaks. Breaks has a FK relationship to the entity_id (not the PK) on the Entities model.
I want to generate an empty form for all the fields of Breaks. Generating a basic form populates all the empty fields, but for the FK it generates a dropdown list of all objects of the Entities table. This is not helpful so I have excluded this in the ModelForm below and tried to replace with a list of all the entity_ids of the Entities table. This form renders as expected.
class BreakForm(ModelForm):
class Meta:
model = Breaks
#fields = '__all__'
exclude = ('entity',)
def __init__(self, *args, **kwargs):
super(BreakForm, self).__init__(*args, **kwargs)
self.fields['entity_id'] = ModelChoiceField(queryset=Entities.objects.all().values_list('entity_id', flat=True))
The below FormView is the cbv called by the URL. As the below stands if I populate the form, and for the FK column entity_id choose one of the values, the form will not submit. By that field on the form template the following message appears Select a valid choice. That choice is not one of the available choices.
class ContactFormView(FormView):
template_name = "breaks/test/breaks_form.html"
form_class = BreakForm
My initial thoughts were either that the datatype of this field (string/integer) was wrong or that Django needed the PK of the row in the Entities table (for whatever reason).
So I added a post function to the FormView and could see that the request.body was populating correctly. However I can't work out how to populate this into the ModelForm and save to the database, or overcome the issue mentioned above.
Addendum:
Models added below:
class Entity(models.Model):
pk_securities = models.AutoField(primary_key=True)
entity_id = models.CharField(unique=True)
entity_description = models.CharField(blank=True, null=True)
class Meta:
managed = False
db_table = 'entities'
class Breaks(models.Model):
pk_break = models.AutoField(primary_key=True)
date = models.DateField(blank=True, null=True)
entity = models.ForeignKey(Entity, on_delete= models.CASCADE, to_field='entity_id')
commentary = models.CharField(blank=True, null=True)
active = models.BooleanField()
def get_absolute_url(self):
return reverse(
"item-update", args=[str(self.pk_break)]
)
def __str__(self):
return f"{self.pk_break}"
class Meta:
managed = False
db_table = 'breaks'
SOLUTION
Firstly I got this working by adding the following to the Entity Model class. However I didn't like this as it would have consequences elsewhere.
def __str__(self):
return f"{self.entity_id}"
I found this SO thread on the topic. The accepted answer is fantastic and the comments to it are helpful.
The solution is to subclass ModelChoiceField and override the label_from_instance
class EntityChoiceField(ModelChoiceField):
def label_from_instance(self, obj):
return obj.entity_id
I think your problem is two fold, first is not rendering the dropdown correctly and second is form is not saving. For first problem, you do not need to do any changes in ModelChoiceField queryset, instead, add to_field_name:
class BreakForm(ModelForm):
class Meta:
model = Breaks
#fields = '__all__'
def __init__(self, *args, **kwargs):
super(BreakForm, self).__init__(*args, **kwargs)
self.fields['entity_id'] = ModelChoiceField(queryset=Entities.objects.all(), to_field_name='entity_id')
Secondly, if you want to save the form, instead of FormView, use CreateView:
class ContactFormView(CreateView):
template_name = "breaks/test/breaks_form.html"
form_class = BreakForm
model = Breaks
In Django, the request object passed as parameter to your view has an attribute called "method" where the type of the request is set, and all data passed via POST can be accessed via the request. POST dictionary. The view will display the result of the login form posted through the loggedin. html.

How to check a form field for a specific attribute in that field's m2m key

I have a form that allows the user to pick several vans (many-to-many relationship). Each van has a boolean attribute named "available". I want to only show the vans whose "available" attribute is set to "True". How do I do this in the forms.py file?
I know that this could possibly be done in the template, but I did not want to create a new form-template with each individual field written out. I wanted to know if this functionality could be done in the forms.py file or in the class based view. I believe that doing it that way would be a bit cleaner. I've look into the validators but I don't think this is the way to go. Maybe I need to run a query set in the form file that checks the attribute before passing it to the form template?
views.py
def post(self, request):
"""Take in user data, clean it, and then post it to the database."""
form = self.form_class(request.POST) # pass in the user's data to that was submitted in form
if form.is_valid():
trip = form.save(commit=False) # create an object so we can clean the data before saving it
# now get the clean and normalize the data
first_name = form.cleaned_data['first_name']
last_name = form.cleaned_data['last_name']
trip_start = form.cleaned_data['trip_start']
trip_end = form.cleaned_data['trip_end']
van_used = form.cleaned_data['van_used']
trip.save()
forms.py
class TripForm(forms.ModelForm):
"""This class will be used to build trips."""
class Meta:
"""Specifying the database and fields to use."""
model = trips
fields = ['first_name', 'last_name', 'comments','trip_start', 'trip_end', 'van_used']
models.py
class trips(models.Model):
class Meta:
verbose_name_plural = "trips"
van_used = models.ManyToManyField(vans)
class vans(models.Model):
class Meta:
verbose_name_plural = "vans"
vanName = models.CharField(max_length=30, unique=True, blank=False)
available = models.BooleanField(default=True, blank=False)
# set up how the vans will be referenced in the admin page
def __str__(self):
return self.vanName
The final form that is rendered would only show the vans whose "available" attribute is set to True. Thanks in advance.
You have to override queryset for van_used field in form like this.
class TripForm(forms.ModelForm):
"""This class will be used to build trips."""
class Meta:
"""Specifying the database and fields to use."""
model = trips
fields = ['first_name', 'last_name', 'comments','trip_start', 'trip_end', 'van_used']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['van_used'].queryset = vans.objects.filter(available=True)

Django ManyToManyField not saving m2m relationships

I have a model defined like so:
class Vote(models.Model):
text = models.CharField(max_length=300)
voters = models.ManyToManyField(CustomUser, blank=True)
game = models.ForeignKey(Game, on_delete=models.CASCADE)
I want a vote to automatically add all players of its associated game to its list of voters, when it is created. The Game model has a method that returns its players, so I have overridden the save method of the Vote model:
def save(self, *args, **kwargs):
super().save(*args, **kwargs) #As the docs state it must be saved before m2m elements can be added
queryset = self.game.get_players
for player in queryset:
self.voters.add(player.id)
This does not work. It does not throw an error, and happily saves the model instance through the admin site. It does not, however, seem to add any of the players to the voters field, (and the vote_voters table remains empty in the db).
Obvious troubleshooting: the queryset is definitely not empty, the save method is definitely being called.
Your models.py
class Vote(models.Model):
text = models.CharField(max_length=300)
voters = models.ManyToManyField(CustomUser, blank=True)
game = models.ForeignKey(Game, on_delete=models.CASCADE)
in forms.py
from django import forms
from your_app.models import Vote
class VoteForm(forms.ModelForm):
class Meta:
model = Vote
fields = ('text', 'game')
And a class based create view
from django.views.generic.edit import CreateView
from your_app.models import Vote
from your_app.forms import VoteForm
class VoteCreate(CreateView):
model = Vote
form_class = VoteForm
def form_valid(self, form):
obj = form.save(commit=True)
obj.voters = obj.game.get_players
# or maybe this
# obj.voters.add([game.player for game in obj.game.get_players])
obj.save()
return super().form_valid(form)
Not tested but the idea in the create view is that you first create the object and then save the m2m. Check the form_valid method
It turns out that this was an issue with the admin section. Using the exact save method shown in the question worked perfectly when submitted through a form. #Selcuk's link to this answer this answer solved the problem

Django M2M with addititional data with through

My problem is as follows. I am saving data for patients from a form on a webpage. The form is generated from model definitions in models.py. The information that I save is name, surname amongst others. I have a field for diagnosis which is selected using a multichoiceField and I save it using manytomany.
When the data is saved, a separate table is created for the diagnosis assigned to each patient as expected. The table contains a diagnosis and the ID of the patient it applies to. Each diagnosis is saved as a separate record.
In addition to selecting the diagnosis, I also save the date that the diagnosis is made. You will see what I mean in the models.py and form.py code below.
I would like to have the date for which the diagnosis was made also saved in the table but I can't figure out how to do this. I have tried following the docs: https://docs.djangoproject.com/en/dev/topics/db/models/#intermediary-manytomany as well as some other posts on SO, but cannot figure out how to do it. I can't figure out how the views, forms and models need to be set up in order to achieve. Is it possible to do this and if so how? I have tried using an intermediate model with manytomany and 'through', but I do not understand it. Any help with this would be greatly appreciated.
Below is a simplified version of my code:
models.py:
class diagnosisChoices(models.Model): #This represents the list in the drop down menu for the different diagnosis.
diagnosis = models.CharField(max_length = 50)
def __str__(self):
return self.diagnosis
class PatientData(models.Model):
Name = models.CharField(max_length=100)
Surname = models.CharField(max_length=100)
dateOfBirth = models.DateField(default = datetime.datetime.now())
diagnosis = models.ManyToManyField(
'diagnosisChoices',
#on_delete=models.CASCADE,
)
views.py:
def patientDataView(request):
uId = request.user.id
if request.method == "POST":
form = PatientDataForm(request.POST)
if form.is_valid():
model_instance = form.save(commit=False)
model_instance.timestamp = timezone.now()
model_instance.save()
#model_instance.add(uId)
form.save_m2m()
return HttpResponseRedirect('/dataBase')
else:
form = PatientDataForm()
return render(request, "dataBaseTest.html", {'form': form})
date_of_diagnosis = models.DateField(default=datetime.datetime.now())
forms.py
from django.forms import ModelForm
from django import forms
from .models import PatientData
from .models import diagnosisChoices #This is the list of diagnosis in the dropdown
from django.forms import extras
import datetime
from functools import partial
class PatientDataForm(ModelForm):
class Meta:
now = datetime.datetime.now()
thisYear = now.year
DateInput = partial(forms.DateInput, {'class': 'datepicker'})
widgets = {
}
model = PatientData
fields = ['Name',
'Surname',
'dateOfBirth',
'diagnosis',
'date_of_diagnosis',
]
Thanks,
Thomas
The main thing that you are not getting is on the models.py, so I will focus on it.
You need three tables to do what you have described: diagnosisData, PatientData and a 'membership' table which I call diagnosisPatient. Then you build your model like this:
class diagnosisChoices(models.Model):
diagnosis = models.CharField(max_length = 50)
class PatientData(models.Model):
Name = models.CharField(max_length=100)
Surname = models.CharField(max_length=100)
dateOfBirth = models.DateField(default = datetime.datetime.now())
diagnosis = models.ManyToManyField('diagnosisChoices',through='diagnosisPatient')
class diagnosisPatient(models.Model):
patient = models.ForeignKey('PatientData')
diagnosis = models.ForeignKey('diagnosisChoices')
dateOfDiagnosis = models.DateField()
Once you have your model built this way, you should save your PatientData and your diagnosisChoices instances as usual. FOr the many to many relation, you should save it manualy on the diagnosisPatient table using the apropriate foreign keys and date. You can query the many to many relation from the PatientData model as usual with objects.all() function.
The thing here to keep in mind is that ManyToMany relations in django are always creating a new membership table for you behind the scenes. So when you do not need to insert extra information on the relationship the diagnosisPatient table is just made of two foreign keys, and it is hidden. The through argument on this relationship is just bringing this table to light and giving you control back to put whatever new relationship you like.

Django How can I save two different class which are connected each other by OneToOne relationship in a form at once?

Let me explain what my problem is in more detail.
First I have a class 'UserInfo' which connected to User class of django.contrib.auth.models like below
models.py
class UserInfo(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
phone = models.CharField(max_length=15,blank=True,unique=True)
position = models.CharField(max_length=15,blank=True,unique=True)
class Meta:
default_permissions = ()
def __str__(self):
return self.position
then I wanted to use ModelForm because I can write less codes. The reason why I made CreateNewUser class is that I wanted to let user can see only [username, email, groups, user_permissions] and control those. (to prevent them to select superuser or staff or inactive options)
forms.py
class CreateNewUserInfo(forms.ModelForm):
class Meta:
model = UserInfo
fields = '__all__'
class CreateNewUser(forms.ModelForm):
class Meta:
model = User
fields = ['username', 'email', 'groups', 'user_permissions']
problem happened in here. I wanted to use FormView to use generic view with class typed view so that I can write less codes(concise code). there is attribute named 'form_class' and I couldn't put two different class with it. I wanted to put different two class to one form with generic view.
views.py
class TestView(FormView):
form_class = CustomForm
template_name = 'manager/alltoall.html'
def form_valid(self, form):
At the end, I made new class in forms.py and wrote every field which I need like below.
forms.py
class CustomForm(forms.Form):
username = forms.CharField(initial='testname',max_length=150)
email = forms.EmailField()
phone_number = forms.CharField(max_length=15)
position = forms.CharField(max_length=15)
views.py
class TestView(FormView):
form_class = CustomForm
template_name = 'manager/alltoall.html'
def form_valid(self, form):
username = form.cleaned_data['username']
email = form.cleaned_data['email']
phone_number = form.cleaned_data['phone_number']
position = form.cleaned_data['position']
with transaction.atomic():
user = User.objects.create(username=username,email=email)
userinfo = UserInfo.objects.create(user=user,phone=phone_number,position=position)
userinfo.save()
user.save()
return super(TestView, self).form_valid(form)
Is there anyway to use ModelForm and FormView to show two different class in a form at the same time? Additionally, I used transaction like above because I wanted to save data in database with two different class. Is it right approach or Is there any other way to do that more conveniently(or efficiently) with built in functions in django?
Thank you for taking your time to read all. I wonder if it is too long, but I wanted to deliver what I wanted to know exactly. Thank you!

Categories

Resources