Hi guys,
I'm writing a Project in Django where I've 5 kind of groups of Users:
- Group1
- Group2
- ...
Then I've a Model, Item which has many relation with users, the Item has one Owner (a User in Group1), a Customer (an User in Group2) and many RelatedUser (Users in Group3).
I'm wondering which is the correct way to write this relations. I'd love to write something like:
class Item(models.Model):
owner = models.ForeignKey(Owner)
customer = models.ForeignKey(Customer)
users = models.ManyToManyField(RelatedUser)
Having defined in some way Owner, Customer and RelatedUser classes.
I do not know how to achieve this. I do not want to use model inheritance, because I just want a table User. Even Managers does not seems to help me. Actually I'm using something like this:
try:
customer = models.ForeignKey(User,
related_name='cust',
limit_choices_to = {'groups__in': [Group.objects.get(name = 'customers')]})
except:
customer = models.ForeignKey(User,
related_name='cust')
Mostly because when starting form an empty database Group 'customers' does not exists and errors are raised.
Which is the right way to afford this?
Thanks in advance