Django ModelForm Validation - django

Trying to solve an interesting problem right now.
I have a Django model with an image field that's not required, but is set to a default value when a new model instance is created.
class Product(models.Model):
image = models.ImageField(upload_to='/image/directory/', default='/default/image/path/', blank=True)
I also have a ModelForm based on that model, that includes the image field, and that has some custom validation.
class ProductForm(forms.ModelForm):
class Meta:
model = Product
fields = ('image',)
def clean_image(self):
image = self.cleaned_data.get('image', False)
if image:
# validate image
return None
The problem is that per the docs, calling is_valid() on a ModelForm triggers model validation in addition to form validation, so when a user submits the model form without an image, my custom form validation code attempts to validate the default model image, rather than just doing nothing as it's supposed to.
How do I get it to not do anything unless the form itself has a value for the image field?

Just solved it in pretty simple way. Adding the answer here in case it's helpful to anyone else.
The Django docs state that
...a model form instance bound to a model object will contain a self.instance attribute that gives model form methods access to that specific model instance.
So rather than check if the ModelForm has an image value, I just check whether the image value has changed from the saved instance. The form validation now looks like this:
class ProductForm(forms.ModelForm):
class Meta:
model = Product
fields = ('image',)
def clean_image(self):
image = self.cleaned_data.get('image', False)
if not self.instance.image == image:
# validate image
return None
Problem solved!

Related

Create Django model where the default value is not shown in my form

Im trying to create a Django model with a default value, something like this:
class ExampleModel(models.Model):
image = models.URLField(max_length=200, default='https://example.com')
And a form for this model:
classExampleForm(forms.ModelForm):
class Meta:
model = ExampleModel
fields = ('image')
The thing is that by doing this when I display the form in an HTML template, the image field comes prepopulated with https://example.com, I want the filed to be empty and if the user doesn't input any value it takes the default one but that this is not shown in the form.
Remove the default value in your model And check if the field is empty in form.is_valid function in your view.
https://docs.djangoproject.com/en/4.0/ref/forms/validation/
Or you can keep the default value of your image and remove it in your form initiation.
https://docs.djangoproject.com/en/4.0/ref/forms/api/#django.forms.Form.initial

How to set a model field based on the current user in a Django CreateView [duplicate]

I have a model named Domain which looks like this:
class Domain(models.Model):
"""
Model for storing the company domains
"""
user = models.ForeignKey(
User
)
host = models.CharField(
null=False, verbose_name="Host", max_length=128, unique=True
)
I'd like to use Django's generic views for doing CRUD operations on this. There is one field in this model that needs user input but the foreign key field doesn't need any user input. How can I exclude that field from the form that my generic view generates but assign it the value of the current authenticated user.
Thanks.
Have a look at Russel's answer to a similar question on the django-users group earlier this week.
Quoting the answer*:
Forms and Views solve different problems.
The View is solving the problem of "how do I handle this request and
convert it into a response?". The Form is solving the problem of "How
do I convert the POST data in this request into a model object (or a
change to a model object)?".
Very roughly, a view is doing the following:
View gets a request
View works out whether this is a GET or a POST
If its a POST, View asks the Form to turn the Post into a model change
Form returns success or failure
View responds to the success or failure of the Form.
View returns a response.
The functionality of the Form is a complete subset of the
functionality of the View -- and for this reason, it's a completely
interchangable internal component.
Now, in simple situations, it's possible for a View to guess all the
defaults for the form -- all it needs to know is that you're dealing
with a Foo model, and it can construct a default Foo ModelForm.
However, if you have more sophisticated form requirements, you're
going to need a customized Form.
We could have implemented this by exposing all the options of
ModelForm on the View class; but in order to keep everything clean, we
kept the ModelForm isolated, and provided the View with a way to
specify which Form class it's going to use.
So - to cover your use case of excluding fields, you define a
ModelForm that excludes the fields, then let the CreateView know the
form you want to use:
class CampaignForm(forms.ModelForm):
class Meta:
model = Campaign
exclude = ('user', 'name', 'content_inlined')
class CreateCampaignView(CreateView):
form_class = CampaignForm
template_name = "forms/create.html"
I'm guessing when you say "fix a values for a field", you mean setting
the values of user, name and content_inlined before you save the new
Campaign instance; to do this, you need to inject some extra code into
the form processing logic of the form:
class CreateCampaignView(CreateView):
form_class = CampaignForm
template_name = "forms/create.html"
def form_valid(self, form):
form.instance.user = ... (something meaningful.. e.g., self.request.user)
return super(CreateCampaignView, self).form_valid(form)
This overrides the default behavior when the form is valid, and sets
the extra values. The super() implementation of form_valid() will then
save the instance.
For the record, this could also be done by overriding the save()
method on the ModelForm -- however, if you do that, you lose the
request object, which you will need if you're trying to set the
instance values to something that is request-sensitive.
*the original answer set self.object.user instead of form.instance.user. This gives an AttributeError so I have changed it above.

Django restrict options of ManyToMany field in ModelForm based on model instance

I want to show only options already stored in models' ManyToManyField.
I have model Order which I want to have a Model based form like this:
class OrderForm(ModelForm):
class Meta:
model = Order
fields = ['amount', 'color']
Now I do not want to display all colors as choices, but instead only color instances saved in ManyToManyField of another model. The other model is Design:
class Design(models.Model):
color = models.ManyToManyField('maker.Color')
# ...
Is this at all possible while using ModelForm?
Attempt
I have tried doing it by having a ModelForm of Design and setting instance:
class ColorForm(ModelForm):
class Meta:
model = Design
fields = ['color']
And then in view:
color_form = ColorForm(instance=design)
But I don't exactly understand what setting instance does, and I think instance is not what I am looking for as it still lists all colors.
The instance setting has nothing to do with limiting the choices. In essence, it simply populates the form's values with the ones from a specific record. You usually provide an instance in an edit operation, whereas you skip it in an add operation.
The representation of a models.ManyToManyField in the ModelForm is a forms.ChoiceField for which you can simply override its queryset property, and specify the queryset you desire.
Therefore, in your view:
form = OrderForm()
form.fields['color'].queryset = Design.object.all() # for example

Edit related object using ModelFormSet

I was using a model formset to generate a table of forms for a list of objects.
Forms:
class UserTypeModelForm(ModelForm):
account_type = ChoiceField(label='User type',
choices=ACCOUNT_OPTIONS, required=False)
class Meta:
model = get_user_model()
fields = ('account_type',)
UserTypeModelFormSet = modelformset_factory(get_user_model(),
form=UserTypeModelForm,
extra=0)
View:
formset = UserTypeModelFormSet(queryset=users, prefix='formset')
Now my client wants to be able to modify a related field: user.employee_profile.visible.
I tryed to add a field to the form, and then passing "initial" and "queryset" to the formset, but It looks like it just takes one.
How would you guys do this?
Thanks
with model formsets, the initial values only apply to extra forms, those that aren’t bound to an existing object instance.
Django docs
The queryset provides the selected/entered values for the bound fields, the initial for the extra fields (in your case 0).
But you can override the initial value in e.g. your views when you created a field called employee in this case:
for form in forms:
# Don't override a selected value.
if not form.fields['employee'].initial:
form.fields['employee'].initial = my_init

How can I create a form for that is populated with the current data in the database in django?

I am new to Django and I am using 1.7. I am looking for an example, on how to create a form that is populated with the current values which are stored. This way the user can see the forms current values and just modify the the ones they want. Then click save.
Assuming you're talking about values stored on a model, you can prepopulate a ModelForm using instance:
#######forms.py#############
class ArticleForm(ModelForm):
class Meta:
model = Article
fields = ['title']
#######views.py################
#create a normal ModelForm without any pre-populated values
blank_form = ArticleForm()
#create a ModelForm pre-populated with a specific object. first, get that object
article = Article.objects.get(pk=1)
form = ArticleForm(instance=article)
Taken from https://docs.djangoproject.com/en/1.7/topics/forms/modelforms/