Re: Model Validation - Prevent Datetimefield overlaps?

2010-08-30 Thread Karen Tracey
On Mon, Aug 30, 2010 at 10:34 PM, Victor Hooi  wrote:

> Just playing around - I noticed that setting "unique=True" on a
> DateTimeField() doesn't seem to work? As in, I was able to do
> something like:
>
> b1 =
>
> BandwidthUsageEntry(start_of_usage_block=datetime.datetime(2007,05,05,12,05),
>
> end_of_usage_block=datetime.datetime(2007,05,12,12,10),bytes_in=10,bytes_out=10)
> b1.save()
> b2 =
>
> BandwidthUsageEntry(start_of_usage_block=datetime.datetime(2007,05,05,12,05),
>
> end_of_usage_block=datetime.datetime(2007,05,12,12,10),bytes_in=20,bytes_out=20)
> b2.save()
>
> Is this expected behaviour, or do I have something wrong with my
> setup?
>

Did you add the unique=True to the fields after you had already run syncdb
to create the table?  If so, the database constraints to require the field
to be unique won't be in place. (And re-running syncdb won't create the
constraints either, since syncdb won't do anything for models with tables
that already exist.)

Karen
-- 
http://tracey.org/kmt/

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.



Model Validation - Prevent Datetimefield overlaps?

2010-08-30 Thread Victor Hooi
heya,

I have a model that contains a whole bunch of bytes in/out for
datetime ranges. I.e.:

class BandwidthUsageEntry(models.Model):
start_of_usage_block = models.DateTimeField()
end_of_usage_block = models.DateTimeField()
bytes_in = models.IntegerField()
bytes_out = models.IntegerField()

I need to ensure that each entry doesn't overlap with any others
entries.

Just playing around - I noticed that setting "unique=True" on a
DateTimeField() doesn't seem to work? As in, I was able to do
something like:

b1 =
BandwidthUsageEntry(start_of_usage_block=datetime.datetime(2007,05,05,12,05),
end_of_usage_block=datetime.datetime(2007,05,12,12,10),bytes_in=10,bytes_out=10)
b1.save()
b2 =
BandwidthUsageEntry(start_of_usage_block=datetime.datetime(2007,05,05,12,05),
end_of_usage_block=datetime.datetime(2007,05,12,12,10),bytes_in=20,bytes_out=20)
b2.save()

Is this expected behaviour, or do I have something wrong with my
setup?

Anyhow, in terms of how to actually prevent overlaps, I assume that
the best way is to use a custom model validator, and check each start/
end against a query against all the entries?

(I saw this 
http://stackoverflow.com/questions/2243490/django-unique-time-interval-for-the-admin-panel,
but it seems to be going a different way that I don't follow).

How would I go about structuring this? Any particular tips here, or
any algorithms that could work well?

Cheers,
Victor

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.