same card can belong to multiple users but only in different time periods

33 views
Skip to first unread message

Shareef 617

unread,
Oct 8, 2018, 7:10:04 AM10/8/18
to Django users

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()

Jason

unread,
Oct 8, 2018, 7:22:29 AM10/8/18
to Django users
What is the question you have?

Nelson Varela

unread,
Oct 8, 2018, 10:25:56 AM10/8/18
to Django users
Django has DateRangeField but that is only available if you are using postgres

Derek

unread,
Oct 8, 2018, 1:17:29 PM10/8/18
to Django users
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.

Devender Kumar

unread,
Oct 9, 2018, 5:48:10 AM10/9/18
to django...@googlegroups.com
Hi,
I think finding solution to this can consume time. Simply make a function which will change the user reference of the card over a given period of time.


Regards
Dev

--
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...@googlegroups.com.
To post to this group, send email to django...@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 all
Reply to author
Forward
0 new messages