initialize modelformset with manytomanyfield - django

All,
I am trying to initialize a modelformset with a manytomanyfield. A catch is that I do not know in advance the name of the manytomanyfield (nor the class it is bound to).
Here are my models and forms:
class Book_model(models.Model):
title = models.CharField(max_length=BIG_STRING)
authors = models.ManyToManyField("Author_model",)
class Author_model(models.Model):
name = models.CharField(max_length=BIG_STRING)
class Book_form(ModelForm):
class Meta:
model = Book_model
class Author_form(ModelForm:
class Meta:
model = Author_model
Author_formset = modelformset_factory(Author_model,form=Author_form)
And elsewhere in my code I am trying to display a Model_form along with an Author_formset. When it comes time to initialize that formset, though, I'm not sure what to do. At that point I know the name of the m2m field ("authors"), the parent model instance (Book_model), the parent form instance (Book_form), and the formset class (Author_formset). I assume that I just need to do something like this:
m2m_field = getattr(book,"authors")
qset = field.filter(<only authors for which there is a m2m relationship from this book>)
formset = Author_formset(queryset=qset)
But, I don't know the right terms to put in the filter.
Any suggestions?

You're on the right track.
book.authors is the queryset of "authors for which there is a m2m from this book". So that is perfectly valid to pass into the formset init.
formset = AuthorFormset(queryset=m2m_field.all())

I think I have solved this.
In theory this is the correct way to do things, as Daniel suggests:
formset = Author_formset(queryset=book.authors.all())
But I can't do that directly, because I am trapped in some generic code that could be called for any model/form/formset. So I'm forced to do this instead:
# these 4 lines are just for clarity's sake
# I don't actually know what these map to in my code
MyModelClass = Book_model
MyFormClass = Book_form
MyFormSetClass = Author_formset
fieldName = "authors"
def DoStuff(model_id=None):
if (model_id):
model = MyModelClass.objects.get(pk=model_id)
else:
model = MyModelClass()
form = MyFormClass(instance=model)
if model.pk:
m2mModels = getattr(model,fieldName)
formset = MyFormSetClass(queryset = m2mModels.all())
else:
m2mModelClass = MyFormSetClass.form.Meta.model
formset = MyFormSetClass(queryset = m2mModelClass.objects.none())
This seems a bit ugly, but it works.

Related

Model with more than one ForeignKey, ForeignKey can be more than one for one Model

So to make the above possible I have found out that I have to have ManytoMany Field that is not a problem.
That field is in the form as follows:
class Form(forms.ModelForm):
class Meta:
model = MyModel
fields = ['notes', 'scan']
widgets = {
'scan': forms.CheckboxSelectMultiple(),
}
In the view I have this then:
form = Form(request.POST)
if from.is_valid():
inst = from.save(commit=False)
inst.something = something
inst.save()
Now what do I do, to save the test or scan from the form?
I tried :
inst.test.add(form.cleaned_data['test'])
But that doesn't work for test or scan.
The Model looks like this:
class MyModel(models.Model):
id = models.AutoField(primary_key=True)
notes = models.TextField(default='')
scan = models.ManyToManyField(Scan)
....
Please help I wasn't able find anything in the Internet about this
Thanks!
The documentation of the Form's save method tells it all: If you have a ModelForm that contains the model's ManyToManyField like this:
class MyForm(forms.ModelForm):
class Meta:
model = MyModel
fields = ['__all__'] # or fields = ['scans'] assuming scans is the M2M field in MyModel
Then you have two ways to save the relationships:
Directly, using form.save()
Calling save_m2m() is only required if you use save(commit=False). When you use a simple save() on a form, all data – including many-to-many data – is saved without the need for any additional method calls.
Or indirectly because you want to manipulate the instance before saving:
if form.is_valid():
instance = form.save(commit=False)
instance.some_field = some_value
instance.save()
form.save_m2m() # this saves the relationships

Setting the initial value for a readonly Choice (ForeignKey) field

I'm trying to make a CreateView have a readonly field with a set value, but I'm unable to make that work.
I have a model with a ForeignKey to another model:
class CompanyNote(TimeStampedModel):
company = models.ForeignKey(Company)
note = models.TextField(blank=True)
And I have a CreateView:
class CompanyNoteCreateView(CreateView):
model = models.CompanyNote
form_class = CompanyNoteForm
That uses a custom ModelForm:
class CompanyNoteForm(forms.ModelForm):
company = forms.ChoiceField(
widget=forms.widgets.Select(attrs={'readonly': 'readonly'}))
class Meta:
model = models.CompanyNote
As you see, the widget for the field in question is readonly. This is because I pick up the company as a part of the URL, as in company/1/note/add . I have no trouble picking up the "1" and finding the right company object, but I don't know how to set the readonly field.
I tried:
def get_initial(self):
initial = super(CompanyNoteCreateView, self).get_initial()
initial['company'] = self.get_company().id
return initial
But that didn't work. The Widget is empty, which may be the problem. Perhaps I'm barking up the wrong tree here. Any ideas welcome.
Have you tried setting the attribute in the Form's Meta class?
I experienced an issue where Form attributes were not applied for Model Fields if set in the base class definition, but they worked correctly in the Meta class:
class CompanyNoteForm(forms.ModelForm):
class Meta:
model = models.CompanyNote
widgets = {'company': forms.widgets.Select(attrs={'readonly': True,
'disabled': True})}
Otherwise check this answer out.
Worst case scenario, make company a hidden field?
Use a ModelChoiceField
class CompanyNoteForm(forms.ModelForm):
company = forms.ModelChoiceField(queryset=models.Company.objects.all(), widget=forms.widgets.Select(attrs={'readonly': 'readonly'}))
I could not find this answer anywhere, that I could actually get to work. But I found a different approach. Set the field to be hidden with forms.HiddenInput() widget. Then the value you pass in from the view will be assigned but the user cannot access it.
widgets = {'field_name': forms.HiddenInput()}
I'm using ModelForm class so my syntax might be different from yours.

Django - creating custom .save() for modelformset_factory

If I create a formset using modelformset_factory like this:
IngredientFormSet = modelformset_factory(RecipeIngredients, form=RecipeIngredientsForm)
formset = IngredientFormSet(request.POST)
and my form looks like this
class RecipeIngredientsForm(forms.ModelForm):
Ingredient = forms.CharField(max_length= 100)
class Meta:
model = RecipeIngredients
exclude = ('weightmetric','recipe')
Where would I put my custom .save() method? Would I put it under the RecipeIngredientsForm?
[a potential solution]
In your view do something like this:
if formset.is_valid():
for form in formset:
obj = form.save(commit=False) #obj = RecipeIngredient model object
try:
ingredient_in_db = Ingredient.objects.get(name = form.cleaned_data.get('ingredientform'))
except:
ingredient_in_db = None
if ingredient_in_db:
obj.ingredient = ingredient_in_db
else:
new_ingredient = Ingredient.objects.create(name = form.cleaned_data.get('ingredientform'))
obj.ingredient = new_ingredient
obj.recipe = recipeobj
obj.save()
Incidentally, I think this method would also allow me to do a custom .save(), given that I take each form in the formset and do a form.save(commit= False) on it. It was easier though, to just do it in my view, since I needed access to the recipe object.
I'm not sure of the best way to override the existing save() method on the BaseModelForm. My guess is that you actually would want to either:
Create your own field that has it's own to_python() method which will take the text and find/create the associated object.
class IngredientField(forms.CharField):
def to_python(self, value):
# Lookup model or create new one
ingredient = models.Ingredient.objects.get(name=value)
if not ingredient:
ingredient = models.Ingredient.create(name=value)
return ingredient
Use save(commit=False) on the formset, do the ingredient lookup/creation, and then commit afterwards

field choices() as queryset?

I need to make a form, which have 1 select and 1 text input. Select must be taken from database.
model looks like this:
class Province(models.Model):
name = models.CharField(max_length=30)
slug = models.SlugField(max_length=30)
def __unicode__(self):
return self.name
It's rows to this are added only by admin, but all users can see it in forms.
I want to make a ModelForm from that. I made something like this:
class ProvinceForm(ModelForm):
class Meta:
CHOICES = Province.objects.all()
model = Province
fields = ('name',)
widgets = {
'name': Select(choices=CHOICES),
}
but it doesn't work. The select tag is not displayed in html. What did I wrong?
UPDATE:
This solution works as I wanto it to work:
class ProvinceForm(ModelForm):
def __init__(self, *args, **kwargs):
super(ProvinceForm, self).__init__(*args, **kwargs)
user_provinces = UserProvince.objects.select_related().filter(user__exact=self.instance.id).values_list('province')
self.fields['name'].queryset = Province.objects.exclude(id__in=user_provinces).only('id', 'name')
name = forms.ModelChoiceField(queryset=None, empty_label=None)
class Meta:
model = Province
fields = ('name',)
Read Maersu's answer for the method that just "works".
If you want to customize, know that choices takes a list of tuples, ie (('val','display_val'), (...), ...)
Choices doc:
An iterable (e.g., a list or tuple) of
2-tuples to use as choices for this
field.
from django.forms.widgets import Select
class ProvinceForm(ModelForm):
class Meta:
CHOICES = Province.objects.all()
model = Province
fields = ('name',)
widgets = {
'name': Select(choices=( (x.id, x.name) for x in CHOICES )),
}
ModelForm covers all your needs (Also check the Conversion List)
Model:
class UserProvince(models.Model):
user = models.ForeignKey(User)
province = models.ForeignKey(Province)
Form:
class ProvinceForm(ModelForm):
class Meta:
model = UserProvince
fields = ('province',)
View:
if request.POST:
form = ProvinceForm(request.POST)
if form.is_valid():
obj = form.save(commit=True)
obj.user = request.user
obj.save()
else:
form = ProvinceForm()
If you need to use a query for your choices then you'll need to overwrite the __init__ method of your form.
Your first guess would probably be to save it as a variable before your list of fields but you shouldn't do that since you want your queries to be updated every time the form is accessed. You see, once you run the server the choices are generated and won't change until your next server restart. This means your query will be executed only once and forever hold your peace.
# Don't do this
class MyForm(forms.Form):
# Making the query
MYQUERY = User.objects.values_list('id', 'last_name')
myfield = forms.ChoiceField(choices=(*MYQUERY,))
class Meta:
fields = ('myfield',)
The solution here is to make use of the __init__ method which is called on every form load. This way the result of your query will always be updated.
# Do this instead
class MyForm(forms.Form):
class Meta:
fields = ('myfield',)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Make the query here
MYQUERY = User.objects.values_list('id', 'last_name')
self.fields['myfield'] = forms.ChoiceField(choices=(*MYQUERY,))
Querying your database can be heavy if you have a lot of users so in the future I suggest some caching might be useful.
the two solutions given by maersu and Yuji 'Tomita' Tomita perfectly works, but there are cases when one cannot use ModelForm (django3 link), ie the form needs sources from several models / is a subclass of a ModelForm class and one want to add an extra field with choices from another model, etc.
ChoiceField is to my point of view a more generic way to answer the need.
The example below provides two choice fields from two models and a blank choice for each :
class MixedForm(forms.Form):
speaker = forms.ChoiceField(choices=([['','-'*10]]+[[x.id, x.__str__()] for x in Speakers.objects.all()]))
event = forms.ChoiceField(choices=( [['','-'*10]]+[[x.id, x.__str__()] for x in Events.objects.all()]))
If one does not need a blank field, or one does not need to use a function for the choice label but the model fields or a property it can be a bit more elegant, as eugene suggested :
class MixedForm(forms.Form):
speaker = forms.ChoiceField(choices=((x.id, x.__str__()) for x in Speakers.objects.all()))
event = forms.ChoiceField(choices=(Events.objects.values_list('id', 'name')))
using values_list() and a blank field :
event = forms.ChoiceField(choices=([['','-------------']] + list(Events.objects.values_list('id', 'name'))))
as a subclass of a ModelForm, using the one of the robos85 question :
class MixedForm(ProvinceForm):
speaker = ...

How to save fields that are not part of form but required in Django

I have a model with a field that is required but not entered by the user and i have a hard time saving the model without errors. My model definition looks like this:
class Goal(db.Model):
author = db.UserProperty(required=True)
description = db.StringProperty(multiline=True, required=True)
active = db.BooleanProperty(default=True)
date = db.DateTimeProperty(auto_now_add=True)
class GoalForm(djangoforms.ModelForm):
class Meta:
model = Goal
exclude = ['author', 'active']
And i use django-forms in appengine to create and validate the form. When i try to save the result of this form however....
def post(self):
data = GoalForm(data=self.request.POST)
if data.is_valid():
goal = data.save(commit=False)
goal.author = users.get_current_user()
goal.put()
self.redirect('/')
I get "ValueError: The Goal could not be created (Property author is required)"
Now i would think that by having commit=False, then adding the property for Goal, and then saving the object would allow me to do this but obviously it's not working. Any ideas?
Note that save() will raise a ValueError if the data in the form doesn't validate
You can find what you need about the save() method here:
http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-save-method
Edit: Instead of goal.put(), do a goal.save()
Edit2: This should solve your problem:
goal = Goal(author='Mr') #example
data = GoalForm(data=self.request.POST, instance=goal)
I realize this is an old question, but for the sake of others searching for a similar answer, I'm posting the following:
Unless there's a reason I missed for not doing so, but I believe this is what you need:
class Goal(db.Model):
author = db.UserProperty(auto_current_user_add=True)
...
...
For reference:
Types and Property Classes:class UserProperty()
Your GoalForm should inherit from django.forms.ModelForm and be defined such that it only requires some fields:
class GoalForm(django.forms.ModelForm):
class Meta:
model = Goal
fields = ('description', 'etc')
Not sure if this is totally working in AppEngine though.
You should also save the form (still not sure about AppEngine):
data = GoalForm(data=self.request.POST)
if data.is_valid():
data.author = users.get_current_user()
data.put()