django creating models inside of form for manytomany field - python

I'm making recipes website and therefore I have recipe model for which I want to create form that looks like this:
class Recipe(models.Model):
....
products = models.ManyToManyField(ProductMeasurement)
....
And I have ProductMeasurement model and form already:
class ProductMeasurement(models.Model):
product = models.ForeignKey(Product)
measurement = models.ForeignKey(Measurement)
value = models.FloatField()
class FridgeForm(forms.Form):
product = forms.ModelChoiceField(queryset=None, widget=forms.Select(attrs={'class': 'form-control'}))
amount = forms.DecimalField(decimal_places=2, widget=forms.NumberInput(attrs={'class': 'form-control'}))
measurement = forms.ModelChoiceField(queryset=None, widget=forms.Select(attrs={'class': 'form-control'}))
def __init__(self, *args, **kwargs):
super(FridgeForm, self).__init__(*args, **kwargs)
self.fields['product'].queryset = Product.objects.all()
self.fields['measurement'].queryset = Measurement.objects.all()
What I want to do is create form for Recipe model. The problem is that there's not going to be already ProductMeasurements that user wants for his recipe. Therefore I need to find a way so that user can create ProductMeasurement inside of Recipe form. What are possible approaches here ?

django formsets was what I was looking for.

Related

Three-level inline formsets in the frontend

I'm trying to accomplish a three-level stacked inline form in Django. Suppose these models:
class Anuncio(models.Model):
title = models.CharField(max_length=200)
delivery = models.CharField(max_length=100)
class Product(models.Model):
anuncio = models.ForeignKey(Anuncio, on_delete=models.CASCADE)
name = models.CharField(max_length=200)
quantity = models.PositiveIntegerField(default=1)
price = models.PositiveIntegerField()
class Image(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
image = models.ImageField()
There is a relation Anuncio-Product and another relation Product-Image. With this Django package, I accomplished exactly what I want in the Django admin: when creating an Anuncio object, I can add as many Products as I want, and those products can have as many Images as I want. I'm trying to accomplish this in the front end.
I think the way to go is with Django formsets, but I'm facing some problems. All the resources I've been able to find online are only 'two-level' formsets or in 'three-level' cases all the foreign keys point to the same parent model.
With this forms.py file:
class ProductForm(ModelForm):
class Meta:
model = Product
fields = ['name', 'quantity', 'price']
class ImageForm(ModelForm):
class Meta:
model = Imagen
fields = ['image']
class AnuncioForm(ModelForm):
class Meta:
model = Anuncio
fields = ['title', 'delivery']
And this views.py function:
def anunciocreateview(request):
form = AnuncioForm(request.POST or None)
ProductFormSet = inlineformset_factory(Anuncio, Product, form=ProductForm)
ImageFormSet = inlineformset_factory(Product, Image, form=ImageForm)
if all([form.is_valid(), ProductFormSet.is_valid(), ImageFormSet.is_valid()]):
parent = form.save(commit=False)
parent.anunciante = request.user
parent.save()
for form in ProductoFormSet:
child = form.save(commit=False)
child.anuncio = parent
child.save()
for form in ImagenFormSet:
imagen = form.save(commit=False)
imagen.product = form.product
imagen.save()
context = {
'form_1' : form,
'form_2' : ProductFormSet,
'form_3' : ImageFormSet,
}
But I think I'm missing important points when it comes to add the proper relations between models. This set-up gives an AttributeError of: 'ProductForm' object has no attribute '__name__'
The, for example, 'add (extra) Product' that appears in AdminStackedInLine I guess it can be accomplished with JavaScript, playing with hidden forms and changing attributes on click events.
Anyone has experience doing something similar or can guide me through the correct direction? Also on how to manage the data and the relations of the submitted forms?
I think your problem is you have tried to validate a class Form instead of instanciate your formset and validate them.
Your code would be look like to something like that :
def anunciocreateview(request):
ProductFormSet = inlineformset_factory(Anuncio, Product, form=ProductForm)
ImageFormSet = inlineformset_factory(Product, Image, form=ImageForm)
anuncio_form = AnuncioForm(request.POST or None)
product_formset = ProductFormSet(request.POST or None)
image_formset = ImageFormSet(request.POST or None)
if all([form.is_valid(), product_formset.is_valid(), image_formset.is_valid()]):
...
The function inlineformset_factory just create a Form class, not a instance of form.
More information and example on the documentation : https://docs.djangoproject.com/fr/4.1/topics/forms/formsets/

Django modelformset with customized model initialization

I have the following models in django:
class tag(models.Model):
...
user = models.ForeignKey(user)
shop = models.ForeignKey(shop)
name = models.CharField()
...
class user(models.Model):
...
name = models.CharField()
...
class shop(models.Model):
...
user = models.ForeignKey(user)
name = models.CharField()
...
Now I need to create a modelformset of tag model that will allow me to store bunch of different tags per user. What I am looking for is a way to initialize such formset with a shop field filtered out for a given user.
This way my forms would have a selection of shops that are assigned to the user.
Any ideas are very welcome!
Thanks
You can use inlineformset_factory to create a formset for related models. To create a formset for your tag and user model you can do the following:
tag_formset = inlineformset_factory(user, tag, fields=['shop', 'name'])
formset = tag_formset(instance=request.user)
To filter the queryset for the shop field you can either create a form dynamically or pass formfield_callback to the factory function.
If you use formfield_callback, this callback can be used to return a custom field for your form
def cb(field, **kwargs):
if field.name == 'shop':
kwargs['queryset'] = shop.objects.filter(user=request.user)
return field.formfield(**kwargs)
tag_formset = inlineformset_factory(user, tag, fields=['shop', 'name'], formfield_callback=cb)
formset = tag_formset(instance=request.user)
Or you can define a form class dynamically in your view
class TagForm(forms.ModelForm):
shop = forms.ModelChoiceField(shop.objects.filter(user=request.user))
class Meta:
fields = ['shop', 'name']
tag_formset = forms.inlineformset_factory(user, tag, form=TagForm)
formset = tag_formset(instance=request.user)

Is there any way to customize the order of multiple fields for forms.Form class with a field initiated by __init__?

When I render the form to a HTML template,
a certain field of form which is initiated by init is ordered always at bottom of table, even though I defined the field at the middle of form class.
Is there any way or method to customize the order of fields in the form where a initiated field exists by init.
I wanna put the field in the middle of form table in HTML template.
A screenshot of the rendered template:
In the screenshot,
the field "category_name" is ordered at the bottom of tag
I wanna change the order to the middle of table.
I am using Django 2.2 and python 3.7 on Windows 10.
Thanks
from django import forms
from .models import Category_name
class MakePurchaseLog(forms.Form):
buy_date = forms.DateField(input_formats=['%Y-%m-%d'])
shop = forms.CharField(max_length=50)
def __init__(self, user, *args, **kwargs):
super(MakePurchaseLog, self).__init__(*args, **kwargs)
self.fields['category_name'] = forms.ChoiceField(
choices = [(item.category_name, item.category_name) \
for item in Category_name.objects. \
filter(owner=user)])
goods_name = forms.CharField(max_length=50)
price = forms.IntegerField(min_value=0)
memo = forms.CharField(max_length=50, required=False)
field_order = ['category_name']
The fact that the __init__ is placed in the middle of the class, will not make any difference, since this is a function, and the evaluation is thus "postponed" until you actually create a MakePurchaseLog form object.
I think the most elegant solution here is to just already define the field in your form, and then in the __init__ function alter the choices, like:
class MakePurchaseLog(forms.Form):
buy_date = forms.DateField(input_formats=['%Y-%m-%d'])
shop = forms.CharField(max_length=50)
category_name = forms.ChoiceField()
goods_name = forms.CharField(max_length=50)
price = forms.IntegerField(min_value=0)
memo = forms.CharField(max_length=50, required=False)
def __init__(self, user, *args, **kwargs):
super(MakePurchaseLog, self).__init__(*args, **kwargs)
self.fields['category_name'].choices = [
(item.category_name, item.category_name)
for item in Category_name.objects.filter(owner=user)
]
We thus populate the choices of the category_name field in the __init__ function, but we define the field already at the class level.

filter a field in a dropdown lit in django admin

i have a very simple django model:
class Car(models.Model):
carname = models.CharField(max_length=100)
carmodel = models.CharField(max_length=100)
carcountry = models.CharField(max_length=100)
caryear = models.CharField(max_length=100)
cardesc = models.TextField()
and a admin.py that shows the all records:
class UserForm(forms.ModelForm):
cardesc = forms.CharField(widget=forms.Textarea(attrs={'cols': 120, 'rows': 20}))
class Meta:
model = Cars
class ModelAdmin(admin.ModelAdmin ):
form = UserForm
admin.site.register(Cars,ModelAdmin)
it works fine. now, my question is that:
i want to have a drop down list and i can select car model and then my results filtered based my choice...
how i can do that? i have to edit Admin.py file? Django has a builtin feature for this? or i have to create an HTML template? how? please help me.
Try this .
class CustomForm(forms.ModelForm):
class Meta:
model = Cars
cars_list = Cars.objects.values_list('carmodel', flat=True).order_by('carmodel').distinct()
choice_list = []
for car in cars_list:
choice_list.append((car,car,))
CAR_CHOICES = choice_list
#Used 'ChoiceField' as you want dropdown list for carmodels stored in text fields
carmodel = forms.ChoiceField(widget=forms.Select, choices=CAR_CHOICES)
class CarsModelAdmin(model.ModelAdmin):
form = CustomForm
list_filter = ['carmodel']
admin.site.register(Cars,CarsModelAdmin)
You would have to add
list_filter = ['carmodel', ]
to class ModelAdmin.
From the documentation:
ModelAdmin.formfield_for_foreignkey(db_field, request, **kwargs)ΒΆ
The formfield_for_foreignkey method on a ModelAdmin allows you to override the default formfield for a foreign keys field. For example, to return a subset of objects for this foreign key field based on the user:
class MyModelAdmin(admin.ModelAdmin):
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "car":
kwargs["queryset"] = Car.objects.filter(owner=request.user)
return super(MyModelAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
This uses the HttpRequest instance to filter the Car foreign key field to only display the cars owned by the User instance.

How do I filter values in a Django form using ModelForm?

I am trying to use the ModelForm to add my data. It is working well, except that the ForeignKey dropdown list is showing all values and I only want it to display the values that a pertinent for the logged in user.
Here is my model for ExcludedDate, the record I want to add:
class ExcludedDate(models.Model):
date = models.DateTimeField()
reason = models.CharField(max_length=50)
user = models.ForeignKey(User)
category = models.ForeignKey(Category)
recurring = models.ForeignKey(RecurringExclusion)
def __unicode__(self):
return self.reason
Here is the model for the category, which is the table containing the relationship that I'd like to limit by user:
class Category(models.Model):
name = models.CharField(max_length=50)
user = models.ForeignKey(User, unique=False)
def __unicode__(self):
return self.name
And finally, the form code:
class ExcludedDateForm(ModelForm):
class Meta:
model = models.ExcludedDate
exclude = ('user', 'recurring',)
How do I get the form to display only the subset of categories where category.user equals the logged in user?
You can customize your form in init
class ExcludedDateForm(ModelForm):
class Meta:
model = models.ExcludedDate
exclude = ('user', 'recurring',)
def __init__(self, user=None, **kwargs):
super(ExcludedDateForm, self).__init__(**kwargs)
if user:
self.fields['category'].queryset = models.Category.objects.filter(user=user)
And in views, when constructing your form, besides the standard form params, you'll specify also the current user:
form = ExcludedDateForm(user=request.user)
Here example:
models.py
class someData(models.Model):
name = models.CharField(max_length=100,verbose_name="some value")
class testKey(models.Model):
name = models.CharField(max_length=100,verbose_name="some value")
tst = models.ForeignKey(someData)
class testForm(forms.ModelForm):
class Meta:
model = testKey
views.py
...
....
....
mform = testForm()
mform.fields["tst"] = models.forms.ModelMultipleChoiceField(queryset=someData.objects.filter(name__icontains="1"))
...
...
Or u can try something like this:
class testForm(forms.ModelForm):
class Meta:
model = testKey
def __init__(self,*args,**kwargs):
super (testForm,self ).__init__(*args,**kwargs)
self.fields['tst'].queryset = someData.objects.filter(name__icontains="1")
I know this is old; but its one of the first Google search results so I thought I would add how I found to do it.
class CustomModelFilter(forms.ModelChoiceField):
def label_from_instance(self, obj):
return "%s %s" % (obj.column1, obj.column2)
class CustomForm(ModelForm):
model_to_filter = CustomModelFilter(queryset=CustomModel.objects.filter(active=1))
class Meta:
model = CustomModel
fields = ['model_to_filter', 'field1', 'field2']
Where 'model_to_filter' is a ForiegnKey of the "CustomModel" model
Why I like this method:
in the "CustomModelFilter" you can also change the default way that the Model object is displayed in the ChoiceField that is created, as I've done above.
is the best answer:
BookDemoForm.base_fields['location'] = forms.ModelChoiceField(widget=forms.Select(attrs={'class': 'form-control select2'}),queryset=Location.objects.filter(location_for__fuel=True))

Categories