On Oct 11, 12:38 pm, Kristaps Kūlis  <kristaps.ku...@gmail.com> wrote:
> There can be multiple associated Model2 objects to Model1, but without one
> Model2  Model1 cannot exist.
>
> Kristaps Kūlis

The best bet is to define custom validation for your inline formset,
to ensure that there's at least one valid entry.

class Model2InlineFormset(forms.models.BaseInlineFormSet):
    def clean(self):
        # get forms that actually have valid data
        count = 0
        for form in self.forms:
            try:
                if form.cleaned_data:
                    count += 1
            except AttributeError:
                # annoyingly, if a subform is invalid Django explicity
raises
                # an AttributeError for cleaned_data
                pass
        if count < 1:
            raise forms.ValidationError('You must have at least one
order')

class Model2Inline(admin.StackedInline):
    formset = Model2InlineFormset


class Model1Admin(admin.ModelAdmin):
    inlines = [Model2Inline]


--
DR.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to