Apart from setting up a unique key (user, card, start, end), you may also 
want to check if the date range for that card overlaps with another date 
range; e.g. 
see 
https://stackoverflow.com/questions/9044084/efficient-date-range-overlap-calculation-in-python
 
--> do that check in the clean method and return an error if the system 
tries to allocate the card when its already (partly) used in the same time 
period.  If you have other restrictions (like not being being assign cards 
"in the past") that will also help cut down on invalid assignments.

On Monday, 8 October 2018 13:10:04 UTC+2, Shareef 617 wrote:
>
> Consider a project where users can have many cards (or anything else, card 
> is just an example), and the same card can belong to multiple users *but 
> only in different time periods*. So if a card is assigned to user from 
> 1.09.2014 to 1.10.2014 (dd/mm/yyyy), then the same card can be assigned to 
> another user but only for time period that ends before 1.09.2014 or starts 
> after 1.10.2014. I hope you get it.
>
> I've created the following models:
>
> class User(models.Model):
>     cards = models.ManyToManyField(Card, through="UsersCards")
>
> class UsersCards(models.Model):
>     user = models.ForeignKey(User)
>     card = models.ForeignKey(Card)
>     start_date = models.DateField()
>     end_date = models.DateField()
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/0657b0f6-eded-4566-929a-415459e346eb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to