Hi Carl, 

The issue is not only with unique_together indeed. Please correct me if I'm 
wrong, but it seems there is no way currently to use model validation with 
fields dependent on each other when one of these fields is not POSTed 
directly by user. Example:

# models.py
class Ticket(models.Model):
    created_by = models.ForeignKey(User, related_name='created_tickets')
    responsible = models.ForeignKey(User, related_name='todo')

    def clean():
        # we don't want to allow tickets where created_by == responsible for 
some reason
        from django.core.exceptions import ValidationError
        if self.created_by == self.responsible:   
            raise ValidationError('Responsible is incorrect.') 

# views.py
class TicketForm(forms.ModelForm):
    class Meta:
        model = Ticket
        exclude = ['created_by']

@login_required
def add_ticket(request):
    form = TicketForm(request.POST or None)
    if form.is_valid():
        ticket = form.save(commit=False)
        ticket.created_by = request.user
        
        # todo: handle ticket.full_clean()

        ticket.save()
        return redirect('ticket_list')
    return TemplateResponse(request, 'tickets/add.html', {'form': form})

Model.clean method is always called on form validation (unlike field 
validators that are excluded if field is excluded from form). And we can't 
write validator for 'responsible' field or for 'created_by' field (so that 
it will be excluded from validation on form.is_valid) because this validator 
won't have an access to other model fields. So the only option here seems to 
move validation from model level to form level.

Please consider this use case while rethinking model validation. The 
proposed context manager seems to be a very good idea and I hope it will fix 
this issue as well.

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

Reply via email to