Proposal: Meta.required_together

7 views
Skip to first unread message

Yo-Yo Ma

unread,
Sep 27, 2010, 2:39:05 AM9/27/10
to Django developers
You don't require health profiling information, but if they enter any,
they must enter it all.

Currently, the pattern is something like:

def clean(self):
if any((self.weight, self.height))
if not all((self.weight, self.height))
raise ValidationError("Uh oh!")

My proposal is to use:

class Meta:
required_together = ('weight', 'height')

This could be application-level of course and not enforced by the
database. The validation error could be something like: "If you enter
"weight", or "height", you must enter both.".

I'm not very well versed with SQL and DBs in general, but I have a
feeling that this has something to do with composite fields, if made
to be enforced at a DB level.

Florian Apolloner

unread,
Sep 27, 2010, 3:18:07 AM9/27/10
to Django developers
Please post usage questions to the users list. This is already doable
with model validation.

Florian

David P. Novakovic

unread,
Sep 27, 2010, 3:22:44 AM9/27/10
to django-d...@googlegroups.com
Is it? I read this as different to anything in the ORM.

This is about conditionally requiring a second field if one is filled
out. Normally it would be done at the JS level.

I think it's a good idea, assuming I haven't missed something that
already does this.

I can't help thinking this is part of a much larger problem though.
That problem is multifield validation. I think we'd have to address
that issue first before working on this specific case that is probably
the simplest.

Maybe this has been considered before, but was dropped because the
idea is too hard to encapsulate in a simple Meta style option?

David

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

Yo-Yo Ma

unread,
Sep 27, 2010, 3:22:21 AM9/27/10
to Django developers
This is a feature request. I'm not asking how to do it. If you read,
I've shown the current method of doing this in clean(). I'm proposing
the addition of an additional Meta option to allow for automatically
adding this behavior.

Florian Apolloner

unread,
Sep 27, 2010, 3:29:20 AM9/27/10
to Django developers


On Sep 27, 9:22 am, "David P. Novakovic" <davidnovako...@gmail.com>
wrote:
> Is it? I read this as different to anything in the ORM.
Well either way; he could have been more specific which stuff he is
talking about (remember the only classes having Meta are Modelform and
Model -- at least does are the two where I use it on a regular base)

> This is about conditionally requiring a second field if one is filled
> out. Normally it would be done at the JS level.
JS level + Server level

> I think it's a good idea, assuming I haven't missed something that
> already does this.
Yes, form.clean and the model validation can do it.

> I can't help thinking this is part of a much larger problem though.
> That problem is multifield validation. I think we'd have to address
> that issue first before working on this specific case that is probably
> the simplest.
Why is multifield validation a problem? I works (tm).

Florian

Yo-Yo Ma

unread,
Sep 27, 2010, 3:34:22 AM9/27/10
to Django developers
Thanks, David. I've read some about the "Custom validation error on
unique_together" ticket. I imagine that if people want customization
there, required_together would need the same. The only idea I have
that seems to work for both situations is:

required_together = (('weight', 'height', 'You must provide a weight
and height, if you intend to use either.'),)

unique_together = (('account', 'sku', 'This sku is already in use by
your company.'),)

On Sep 27, 1:22 am, "David P. Novakovic" <davidnovako...@gmail.com>
wrote:
> Is it? I read this as different to anything in the ORM.
>
> This is about conditionally requiring a second field if one is filled
> out. Normally it would be done at the JS level.
>
> I think it's a good idea, assuming I haven't missed something that
> already does this.
>
> I can't help thinking this is part of a much larger problem though.
> That problem is multifield validation. I think we'd have to address
> that issue first before working on this specific case that is probably
> the simplest.
>
> Maybe this has been considered before, but was dropped because the
> idea is too hard to encapsulate in a simple Meta style option?
>
> David
>
> On Mon, Sep 27, 2010 at 5:18 PM, Florian Apolloner
>

hejsan

unread,
Oct 1, 2010, 9:59:20 AM10/1/10
to Django developers
Hi.
I just filed a feature request on the same or similar issue, and this
thread was brought to my attention:
http://code.djangoproject.com/ticket/14347

Basically the usecase is this:
Very often we have a "Published" field on our models (or "Published
date" or "Published status" etc..) It would be very nice to be able to
allow people to save instances without all the required fields being
filled in IF the article or whathaveyou is not published yet.

My suggestion was to allow the "required" field on the form field to
take a callable instead of a boolean.
In this callable we could check whether some other fields are filled
out or whatever we want.

This would be a very handy feature for a very common problem.

best,
Hejsan

TiNo

unread,
Oct 2, 2010, 12:17:40 PM10/2/10
to django-d...@googlegroups.com
Hi,

Isn't this covered by model validation [1]?

Tino

LookMa NoSQL

unread,
Oct 2, 2010, 11:58:26 PM10/2/10
to Django developers
+1 on proposal (for what it matters).

Tina, where did you see that Django does that? The docs link you sent
shows regular model validation. What Mamayo is looking for, I think,
is the ability to add a Meta option to a model that says
required_together=({fields: ('weight', 'height', 'width', 'length'),
error_message: "Sorry, in order to use weight, height, width, or
length, you have to include them all."}). At least I think that's what
he means. This would help me too.

On Oct 2, 10:17 am, TiNo <tin...@gmail.com> wrote:
> Hi,
>
> Isn't this covered by model validation [1]?
>
> Tino
>
> [1]http://docs.djangoproject.com/en/dev/ref/models/instances/#validating...
> > django-develop...@googlegroups.com<django-developers%2Bunsu...@googlegroups.com>
> > .
> > > > > For more options, visit this group athttp://
> > groups.google.com/group/django-developers?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Django developers" group.
> > To post to this group, send email to django-d...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > django-develop...@googlegroups.com<django-developers%2Bunsu...@googlegroups.com>
> > .

TiNo

unread,
Oct 3, 2010, 4:08:49 PM10/3/10
to django-d...@googlegroups.com
Doesn't this do what you want?:

class MyModel(models.Model):
    weight = ..
    height = ...
    width = ...
    length = ...

    def clean(self):
        from django.core.exceptions import ValidationError
        if self.weight or self.height or self.width or self.length and 
                not (self.weight and self.height and self.width and self.length):
            raise ValidationError("Sorry, in order to use weight, height, width, or"
                " length, you have to include them all.")
            
    def save(self, *args, **kwargs):
        self.clean()
        super(MyModel, self).save(*args, **kwargs)

Of course it does require you to write a little more code, but it is possible. 

Besides, does the required_together mean that all fields are required when one is filled out, or that some are required when the first is filled out? What I mean is that there are many possibilities for validating a model, and at the moment we have quite some good tools for them. Adding another Meta option for a small portion of the cases doesn't seem so necessary to me...

Anyway, that's just my 2c.

TinO


To unsubscribe from this group, send email to django-develop...@googlegroups.com.

LookMa NoSQL

unread,
Oct 3, 2010, 8:57:13 PM10/3/10
to Django developers
Tino, are you joking? Did you even bother to read the OP's proposal. I
think there is a real lack of patience when you spend the time writing
what the OP has written without even reading it, just to try to
dismiss it.

OP:

>def clean(self):
> if any((self.weight, self.height))
> if not all((self.weight, self.height))
> raise ValidationError("Uh oh!")

> > <django-developers%2Bunsu...@googlegroups.com<django-developers%252Buns...@googlegroups.com>
>
> > > > .
> > > > > > > For more options, visit this group athttp://
> > > > groups.google.com/group/django-developers?hl=en.
>
> > > > --
> > > > You received this message because you are subscribed to the Google
> > Groups
> > > > "Django developers" group.
> > > > To post to this group, send email to
> > django-d...@googlegroups.com.
> > > > To unsubscribe from this group, send email to
> > > > django-develop...@googlegroups.com<django-developers%2Bunsu...@googlegroups.com>
> > <django-developers%2Bunsu...@googlegroups.com<django-developers%252Buns...@googlegroups.com>

TiNo

unread,
Oct 4, 2010, 3:31:05 AM10/4/10
to django-d...@googlegroups.com
Sorry I didn't reread this thread, or remembered it after my previous post. I just replied to clearify my previous post. 

Tino

To unsubscribe from this group, send email to django-develop...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages