Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

validate delete inline formset django

class RequiredImageInlineFormset(forms.models.BaseInlineFormSet):
    """ Makes inline fields required """

    def clean(self):
        # get forms that actually have valid data
        count = 0
        delete_checked = 0
        for form in self.forms:
            try:
                if form.cleaned_data:
                    count += 1
                    if form.cleaned_data['DELETE']:
                        delete_checked += 1
                    if not form.cleaned_data['DELETE']:
                        delete_checked -= 1
            except AttributeError:
                # annoyingly, if a subform is invalid Django explicity raises
                # an AttributeError for cleaned_data
                pass

        # Case no images uploaded
        if count < 1:
            raise forms.ValidationError(
                'At least one image is required.')

        # Case one image added and another deleted
        if delete_checked > 0 and ProductImage.objects.filter(product=self.instance).count() == 1:
            raise forms.ValidationError(
                "At least one image is required.")
Comment

PREVIOUS NEXT
Code Example
Python :: django.com 
Python :: dataframeclient influxdb example 
Python :: python-wordpress-xmlrpc get post id 
Python :: how to display text on boxplot in python 
Python :: pyqt message box set information text 
Python :: python - from most_similar to ldictionary 
Python :: print("python is good") 
Python :: mutliple inxed conditions py 
Python :: varianza en pandas 
Python :: Improve the Request Add Timeout to request 
Python :: wait until you press escape 
Python :: Python Creating a Tuple 
Python :: fancy index python 
Python :: Python sleep() in a multithreaded program 
Python :: keras imagenet 
Python :: In addition to indexing, slicing is also supported. While indexing is used to obtain individual characters, slicing allows you to obtain substring: 
Python :: non linear regression 
Python :: truncated float python 
Python :: different accuracy score for knn 
Python :: big python code 
Python :: python4 
Python :: Odoo Module ACL(Access Controls List) 
Python :: django null first 
Python :: R-squared and MNSE error computation 
Python :: a list of available font in figlet in python 
Python :: Nested pie chart graphing function - put legend in subplot 
Python :: new library in python3 
Python :: vscode python region folding 
Python :: how to get single element from arraylist in numpy arrayt 
Python :: when i was a young lad i was bitten by a turtle 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =