Additional variable passed to django formset - python

I have a quite peculiar issue, regarding creating a formset out of a certain form. The thing is, that the form has to has an undisclosed number of fields, so I want to pass a variable to that form, which will take a proper model from database, and create proper fields within form's init method.
StrangeForm(forms.Form):
def __init__(self, id, *args, **kwargs):
super(StrangeForm, self).__init__(*args, **kwargs)
# get the form's base entry
self.certain_entry = Certain_Entry.objects.get(id=id)
# create a hidden input to keep the id
self.fields["id"] = forms.HiddenInput()
self.fields["id"].initial = id
# initiate text field
self.fields["text_field"] = forms.CharField(widget=forms.TextInput(attrs={'style': 'width:95%', }),
required=False)
self.fields["text_field"].initial = self.certain_entry.text
# create and initiate fields for undisclosed number of subentries
subordinates = Certain_Entry_Subordinates.objects.filter(foreign_key=self.certain_entry)
for each in subordinates:
the_name = "%d_%s_%s" % (each.further.id, each.further.name)
self.fields[the_name] = forms.ChoiceField(choices=(("Ok", "Ok"), ("Not Ok", "Not Ok"), ("Maybe" "Maybe"),
required=False)
self.fields[the_name].initial = each.option
The models are as follows:
class Certain_Entry(models.Model):
text = models.TextField(max_length=128, null=True, blank=True)
class Certain_Entry_Subordinates(models.Model):
certain_entry = models.ForeignKey(Certain_Entry, null=False, blank=False)
option = models.TextField(max_length=8, null=True, blank=True)
further = models.ForeignKey(Further, null=False, blank=False)
class Further(models.Model):
name = models.TextField(max_length=32, null=True, blank=True)
So as you see, I need to pass this ID into the form. When it's a single form, it's all OK, but I can't find any information as to how create a formset with forms that require a variable. Any ideas?
PS: As to WHY... Don't ask, I just need to do it that way, trust me. I'm an engineer.

Related

Django - How to render a ModelForm with a Select field, specifying a disabled option?

I have the following models:
# Get or create a 'Not selected' category
def get_placeholder_categoy():
category, _ = ListingCategories.objects.get_or_create(category='Not selected')
return category
# Get default's category ID
def get_placeholder_category_id():
return get_placeholder_categoy().id
class ListingCategories(models.Model):
category = models.CharField(max_length=128, unique=True)
def __str__(self):
return f'{self.category}'
class Listing(models.Model):
title = models.CharField(max_length=256)
seller = models.ForeignKey(User, on_delete=models.CASCADE, related_name='listings')
description = models.TextField(max_length=5120, blank=True)
img_url = models.URLField(default='https://media.istockphoto.com/vectors/no-image-available-picture-coming-soon-missing-photo-image-vector-id1379257950?b=1&k=20&m=1379257950&s=170667a&w=0&h=RyBlzT5Jt2U87CNkopCku3Use3c_3bsKS3yj6InGx1I=')
category = models.ForeignKey(ListingCategories, on_delete=models.CASCADE, default=get_placeholder_category_id, related_name='listings')
creation_date = models.DateTimeField()
base_price = models.DecimalField(max_digits=10, decimal_places=2, validators=[
MinValueValidator(0.01),
MaxValueValidator(99999999.99)
])
With these, I have the following form:
class ListingForm(ModelForm):
class Meta:
model = Listing
exclude = ['seller', 'creation_date']
widgets = {
'title': TextInput(attrs=base_html_classes),
'description': Textarea(attrs=base_html_classes),
'img_url': URLInput(attrs=base_html_classes),
'category': Select(attrs=base_html_classes),
'base_price': NumberInput(attrs=base_html_classes)
}
One of the available categories I have is "Not selected", since I want to allow that if at some point a category were to be removed, items can be reassigned to that one, however, when rendering the form, I will do some validation on the view function to prevent it from being submitted if the "not selected" category is sent with the form.
Because of this, I want the HTML form on the template to assign the 'disabled' attribute to the option corresponding to that category, however, I have been searching for a couple of days now without finding anything that I was able to understand to the point where I could try it.
Ideally, another thing I'd like to achieve is to be able to modify the order of the rendered options on the form so that I can move to the top 'not selected' regardless of its primary key within the model.
I am aware I can just create a form instead of a model form, or just modify the template so I manually specify how to render the form itself, but I do feel like there is a simple fix to this either on the model or on the model form that I am just not finding yet.
Thanks in advance!
I would suggest you use (in model definition)
class Listing(models.Model):
..
category = model.ForeignKey(ListingCategories, on_delete=models.SET_NULL, null=True, related_name='listings')
..
and optionally in form definition
class ListingForm(ModelForm):
category = forms.ModelChoiceField(ListingCategories, empty_label='Not Selected')
..
While rendering model form, a required attribute will be automatically added, and in form validating, it is also required. It is only in database validation that the field can be left NULL

Django Forms: How to make form.fields by Model.QuerySet in dynamic?

I need to generate Django forms.Form object with fields not from Model.fields (Database Table Columns names), but by records in Model.Table.
I have table Model in models.py:
class MntClasses(models.Model):
type = models.CharField(max_length=2, blank=True, null=True)
class_subtype = models.CharField(max_length=45, blank=True, null=True)
text = models.CharField(max_length=45, blank=True, null=True)
explanation = models.CharField(max_length=45, blank=True, null=True)
name = models.CharField(max_length=45, blank=True, null=True)
views.py
# Form generate
class Form_classes(forms.Form):
def __int__(self, *args, **kwargs,):
super(Form_classes, self).__init__(*args, **kwargs)
print("some")
for fld_ in args:
self.fields[fld_.name] = forms.BooleanField(label=fld_.text)
#Main
def page_Category_Main(request, post):
db_table = MntClasses
form_fld = db_table.objects.all()
'''
This QuerySet 20 records returned of <MntClasses: MntClasses object (0-19)> type.
QuerySet Filds Names: 'name','type','expalnation', 'text'
''':
form_ = Form_classes(*form_fld)
exit_ = {
'form': form_,
}
return render(request, template_name="category.html", context=exit_)
It raise TypeError
init() takes from 1 to 12 positional arguments but 20 were given
So, i have no idea what does it mean this code taken from were: Auto-generate form fields for a Form in django:
def __int__(self, *args, **kwargs,):
super(Form_classes, self).__init__(*args, **kwargs)
What is this "*args", how to use it?
How can I generate Form.fields by QuerySet form_fld.name in that case?
About args
To understand what args is you can take a look at this post which will eventually direct you to https://docs.python.org/3/tutorial/controlflow.html#arbitrary-argument-lists.
Basically it's a special python syntax which allows a function to retrieve multiple arguments as a tuple in a single variable.
About Django's Models
Do you really want to generate multiple forms?
If this is the case you would need to loop over your table:
forms = []
db_table = MntClasses
for item in db_table.objects.all():
forms.append(Form_classes(item))
exit_ = {
'form': forms,
}
the trick is then how you deal with the multiple forms on the front end.
Also you probably want to switch to a ModelForm which would look something like:
from django import forms
from myapp.models import MntClasses
class FormMnt(forms.ModelForm):
class Meta:
model = MntClasses
...
In case you want to handle multiple instances of MntClasses in a single form, you should look at Django's formsets.

Save extra fields to django model apart from the fields which were present in django form(for the same model)

I have a Django model with few fields, In the django forms which is created using that django models. While using the forms.save method I also want to save the extra fields which were not present in Django forms but present in django models.
models.py
class NewProvisionalEmployeeMail(models.Model):
STATUS_CHOICES = (
(1, ("Permanent")),
(2, ("Temporary")),
(3, ("Contractor")),
(4, ("Intern"))
)
PAY_CHOICES = (
(1, ("Fixed")),
(2, ("Performance Based")),
(3, ("Not Assigned")),
)
POSITION_CHOICES = ()
for i, name in enumerate(Position.objects.values_list('position_name')):
POSITION_CHOICES += ((i, name[0]),)
email = models.EmailField(max_length=70, null=False, blank=False, unique=False)
token = models.TextField(blank=False, null=False)
offer_sent_by = models.CharField(max_length=50)
position_name = models.IntegerField(choices=POSITION_CHOICES, null=True, blank=True)
accepted = models.BooleanField(default=False)
name=models.CharField(max_length=30)
user_name=models.CharField(max_length=30)
pay = models.IntegerField(default=0)
title = models.CharField(max_length=25, null=True, blank=True)
pay_type = models.IntegerField(choices=PAY_CHOICES, default=3)
emp_type = models.IntegerField(choices=STATUS_CHOICES, default=1)
def __str__(self):
return str(self.offer_sent_by) +" to " + str(self.email)
def clean(self):
if(NewProvisionalEmployeeMail.objects.filter(email=str(self.email)).exists()):
NewProvisionalEmployeeMail.objects.filter(email=str(self.email)).delete()
def save(self, **kwargs):
self.clean()
return super(NewProvisionalEmployeeMail, self).save(**kwargs)
If you see it has following fields :
email, token, offer_sent_by, position_name, accepted, name, user_name, pay, title, pay_type, emp_type.
Now I only want the following fields in my forms :
email, position_name, name, user_name, pay, title, pay_type, emp_type and not token and offer_sent_by whose values will be determined in my views.py using some logic.
forms.py
class NewProvisionalEmployeeMailForm(ModelForm):
class Meta:
model = NewProvisionalEmployeeMail
fields = ['email', 'position_name',
'name', 'user_name', 'pay',
'title', 'pay_type', 'emp_type',
]
According to my logic in my views.py the other fields values are generated inside the function, but since to save the model we have to use formname.save, here it is NewProvisionalEmployeeMailForm.save(). However this will only save the fields which were coming from my template form, how do I also add other left fields while saving using this dunction.
views.py
def sendoffer(request):
context = {}
new_emp_form = NewProvisionalEmployeeMailForm();
context['form'] = new_emp_form
hostname = request.get_host() + "/dummyoffer"
if request.method=='POST':
new_emp_form = NewProvisionalEmployeeMailForm(request.POST)
if(new_emp_form.is_valid()):
token = VALUE COMES FROM LOGIC
offer_sent_by = VALUE COMES FROM LOGIC
# I also want to save the fields token, offer_sent_by in my models using this form save method
new_emp_form.save()
return render(request, 'mainapp/offer.html',context)
As you see new_emp_form save method will only save only those fields that are present in the form and not the fields token and offer_sent_by which is also part of the model. How do save the fields using form.save method?
Saving the form returns an instance of NewProvisionalEmployeeMail, so you can simply catch the returned object in a variable and set it's properties afterwards:
if(new_emp_form.is_valid()):
token = VALUE COMES FROM LOGIC
offer_sent_by = VALUE COMES FROM LOGIC
new_emp = new_emp_form.save(commit=False)
new_emp.token = token
new_emp.offer_sent_by = offer_sent_by
new_emp.save()
for the first time we can change it as following
new_emp = new_emp_form.save(commit=False)
so that i wont save to the database.

Django: MultiChoiceField does not show saved choices added after creation

I am currently trying to create a dynamic product model that will allow admins to create add their own "option sets" to products.
For example, Product A has flap valve with 400mm, 500mm and 600mm widths available.
To facilitate this I have created 3 models.
models.py
# A container that can hold multiple ProductOptions
class ProductOptionSet(models.Model):
title = models.CharField(max_length=20)
# A string containing the for the various options available.
class ProductOption(models.Model):
value = models.CharField(max_length=255)
option_set = models.ForeignKey(ProductOptionSet)
# The actual product type
class HeadwallProduct(Product):
dimension_a = models.IntegerField(null=True, blank=True)
dimension_b = models.IntegerField(null=True, blank=True)
# (...more variables...)
flap_valve = models.CharField(blank=True, max_length=255, null=True)
...and a form...
forms.py
class HeadwallVariationForm(forms.ModelForm):
flap_valve = forms.MultipleChoiceField(required=False, widget=forms.SelectMultiple)
def __init__(self, *args, **kwargs):
super(HeadwallVariationForm, self).__init__(*args, **kwargs)
self.fields['flap_valve'].choices = [(t.id, t.value) for t in ProductOption.objects.filter(option_set=1)]
def save(self, commit=True):
instance = super(HeadwallVariationForm, self).save(commit=commit)
return instance
class Meta:
fields = '__all__'
model = HeadwallProduct
This works fine for during the initial creation of a product. The list from the MultipleChoiceForm is populated with entries from the ProductOptionSet and the form can be saved.
However, when the admin adds a 700mm flap valve as an option to the ProductOptionSet of Product A things fall apart. Any new options will show up in the admin area of the existing product - and will even be persisted to the database when the product is saved - but they will not be shown as selected in the admin area.
If a Product B is created the new options work as intended, but you cannot add new options to an existing product.
Why does this happen and what can I do to fix it? Thanks.
Urgh... after about 4 hours I figured it out...
Changing:
class ProductOption(models.Model):
value = models.CharField(max_length=20)
option_set = models.ForeignKey(ProductOptionSet)
to
class ProductOption(models.Model):
option_value = models.CharField(max_length=20)
option_set = models.ForeignKey(ProductOptionSet)
Fixed my issue.

how to save Many to Many relationship in django

How to create an object for a Django model with a many to many field?
From above question i come to know we can save Many to Many field later only.
models.py
class Store(models.Model):
name = models.CharField(max_length=100)
class Foo(models.Model):
file = models.FileField(upload_to='')
store = models.ManyToManyField(Store, null=True, blank=True)
views.py
new_track.file = request.FILES['file']
new_track.save()
And file uploading working fine then later i modify my code to add store then i am here...
Now i am sure db return id's here. Then i tried with my below code but that's given me error only
x = new_track.id
new = Foo.objects.filter(id=x)
new.store.id = request.POST['store']
new.save()
ok so the error here is 'QuerySet' object has no attribute 'store'
And also i tried with add that's now working either.
So the question is how to save()
the right way of saving objects with manytomany relations would be:
...
new_track.file = request.FILES['file']
new_track.save()
new_store = Store.objects.get(id=int(request.POST['store']))
new_track.store.add(new_store)
As of 2020, here's my approach to saving ManyToMany Field to a given object.
Short Answer
class HostingRequestView(View):
def post(self, request, *args, **kwargs):
form = VideoGameForm(request.POST)
if form.is_valid():
obj = form.save(commit=False)
obj.updated_by = request.user
obj.save()
selected_categories = form.cleaned_data.get('category') #returns list of all selected categories e.g. ['Sports','Adventure']
#Now saving the ManyToManyField, can only work after saving the form
for title in selected_categories:
category_obj = Category.objects.get(title=title) #get object by title i.e I declared unique for title under Category model
obj.category.add(category_obj) #now add each category object to the saved form object
return redirect('confirmation', id=obj.pk)
Full Answer
models.py
class Category(models.Model):
title = models.CharField(max_length=100, null=True, unique=True)
class VideoGame(models.Model):
game_id = models.AutoField(primary_key=True)
name = models.CharField(max_length=50, blank=False, null=False)
updated_by = models.ForeignKey(settings.AUTH_USER_MODEL, blank=False, on_delete=models.CASCADE)
category = models.ManyToManyField(Category) #ManyToMany Category field
date_added = models.DateTimeField(auto_now_add=True, verbose_name="date added")
forms.py ModelForm
class VideoGameForm(forms.ModelForm):
CATEGORIES = (
('Detective', 'Detective'),
('Sports', 'Sports'),
('Action', 'Action'),
('Adventure', 'Adventure'),
)
category = forms.MultipleChoiceField(choices=CATEGORIES, widget=forms.SelectMultiple())
class Meta:
model = VideoGame
fields = ['name', 'category', 'date_added']
views.py on POST
class HostingRequestView(View):
def post(self, request, *args, **kwargs):
form = VideoGameForm(request.POST)
if form.is_valid():
obj = form.save(commit=False)
obj.updated_by = request.user
obj.save()
selected_categories = form.cleaned_data.get('category') #returns list of all selected categories e.g. ['Sports','Adventure']
#Now saving the ManyToManyField, can only work after saving the form
for title in selected_categories:
category_obj = Category.objects.get(title=title) #get object by title i.e I declared unique for title under Category model
obj.category.add(category_obj) #now add each category object to the saved form object
return redirect('confirmation', id=obj.pk)
URL path for redirect
urlpatterns = [
path('confirmation/<int:id>/', Confirmation.as_view(), name='confirmation'),
]
I hope this can be helpful. Regards
new.stores.all()
returns all stores linked to the object.
Maybe:
Change Foo to Tracks
Tracks.objects.filter(id=x) to Tracks.objects.get(id=x)
Let me know how it goes
why this confusion so much.. you are getting the id there then, call the store like
new_track.save()
new_track.store.add(request.POST['store'])

Categories