Milano
Milano

Reputation: 18745

Django - Multiple users assign different names for the same object

I'm trying to add attribute to relationship.

I have a model Scheduler, Product and User.

There are multiple Scheduler objects in database. Each can have multiple products. And each product has some of those schedulers.

The user can choose which scheduler to use with a product.

I want user to be able to name this scheduler (note that one scheduler can be used by many products by multiple users).

Use case:

User creates a product and choose from allowed schedulers. When they choose the scheduler they can assign some name to this scheduler.

For user1, the scheduler with id=5 has name='My scheduler - everyday'

For user2, the same scheduler (id=5) has name='Everyday schedule'

class Product(models.Model):

    user = models.ForeignKey(User, null=False, blank=False)

    scheduler = models.ForeignKey('Scheduler', null=True, blank=True, related_name='products')

class Scheduler(models.Model):
    weekhours = models.ManyToManyField('WeekHour', related_name='schedulers')
    identificator = models.TextField(null=True,blank=True)

Is it possible to do that in some simple way?

Upvotes: 0

Views: 270

Answers (2)

Alexander Tyapkov
Alexander Tyapkov

Reputation: 5087

I have suggested something similar to

SCHEDULER_TYPE = (
  (1, _('Type1')),
  (2, _('Type2')),

)

class Scheduler(models.Model):

    weekhours = models.ManyToManyField('WeekHour', related_name='schedulers')

    name = models.CharField(max_length=200, verbose_name=_('scheduler name'))

    type = models.PositiveIntegerField(_('scheduler type'), choices=SCHEDULER_TYPE, default=1)

The type of the scheduler will be saved in SCHEDULER_TYPE and won't be afftected if user will change the name of the Scheduler instance.

Also if you need more data you can separate SchedulerCatergory and add additional category field into Scheduler:

category = models.ForeignKey(SchedulerCategory, blank=True, null=True)

In my undestanding when user chooses the type of Scheduler, he created new Scheduler instance of specific type. This Scheduler instance will be unique and won't be affected by other users. It also means that you can easily save any name inside it.

In your words:

User creates a product and choose from allowed schedulers. When they choose the scheduler they can assign some name to this scheduler.

In my opinon: User create a product and choose not from allowed schedulers instances but from scheduler_types or scheduler_categories. Then new instance of Scheduler is created in which you can save name.

Upvotes: 0

markos.aivazoglou
markos.aivazoglou

Reputation: 174

What you can do, is have another model, that maps Users to Schedulers, and have a name attribute there. That model would have user_id, scheduler_id and scheduler_name as attributes.

Otherwise, you can do that in the Product model, and point to the desired Scheduler indirectly.

Upvotes: 1

Related Questions