Re: cross-table unique_together?

2007-07-06 Thread Todd O'Bryan

On Fri, 2007-07-06 at 21:50 +, Nathan Ostgard wrote:
> You would either have to duplicate the Title foreign key in the Copy
> model or write a custom check in the save method.
> 
> To enforce uniqueness over several columns you would use the
> "unique_together" field of the Meta class for your Copy model. (see:
> http://www.djangoproject.com/documentation/model-api/#unique-together)
> This creates the appropriate unique index for your table. However,
> this means you can only enforce uniqueness for fields within an
> individual model.
> 
Thanks. That's what I was afraid of, but the idea of duplicating the
title ForeignKey never occurred to me.

On the other hand, I guess that would require overriding __setattr__
method to set the title whenever the purchase_group is set, so it might
just be easier to put the constraint in the save method.

Todd


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: cross-table unique_together?

2007-07-06 Thread Nathan Ostgard

You would either have to duplicate the Title foreign key in the Copy
model or write a custom check in the save method.

To enforce uniqueness over several columns you would use the
"unique_together" field of the Meta class for your Copy model. (see:
http://www.djangoproject.com/documentation/model-api/#unique-together)
This creates the appropriate unique index for your table. However,
this means you can only enforce uniqueness for fields within an
individual model.

Nathan

On Jul 6, 11:30 am, "Todd O'Bryan" <[EMAIL PROTECTED]> wrote:
> I'm writing an app to deal with checking out textbooks to students. The
> models are
>
> Title:
> the book
> PurchaseGroup:
> when purchased and how much they cost, foreign key to Title
> Copy:
> number, foreign key to PurchaseGroup
>
> I'd like to make it so that no two copies have the same number and
> title, i.e. copies #1 and #2 of The Django Book are legal as is copy #1
> of The Mythical Man-Month, but having two copy #1's of the same book
> would be forbidden.
>
> Will the ORM do that, or do I have to just write a check in the Copy
> class's save method?
>
> Thanks!
> Todd


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



cross-table unique_together?

2007-07-06 Thread Todd O'Bryan

I'm writing an app to deal with checking out textbooks to students. The
models are

Title:
the book
PurchaseGroup:
when purchased and how much they cost, foreign key to Title
Copy:
number, foreign key to PurchaseGroup

I'd like to make it so that no two copies have the same number and
title, i.e. copies #1 and #2 of The Django Book are legal as is copy #1
of The Mythical Man-Month, but having two copy #1's of the same book
would be forbidden.

Will the ORM do that, or do I have to just write a check in the Copy
class's save method?

Thanks!
Todd


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---